MATLAB: How exactly does SimBiology treat model objects

copyMATLABmodel objectpointerSimBiology

I need to create a series of SimBiology models with similar attributes, so I am creating a new model equal to my original model so that I can make some small tweaks to the new object, using a MATLAB script. I'll call the original model model and the new model model2:
model2 = model;
Now suppose I want to simply change the name of one of the species in model2, say, from E to E1:
model2.species(1).Name = [model2.species(1).Name num2str(1)];
And certainly species 1 of model 2 has a 1 appended on to the end of its name. My problem is that my code makes this change for my original model as well, even though I have made no mention of it in my code. So if I call 'model.Species' and 'model2.Species', I obtain exactly the same result:
Index: Compartment: Name: InitialAmount:
1 unnamed E1 0
Can anyone provide any issue into this issue; specifically, how do I prevent the original model's attributes from changing when I change model2's? It seems that model2 is somehow both a SimBiology object and a pointer to model??

Best Answer

  • Hi Sam,
    As you discovered, a SimBiology model is a "handle" object. This means that your variables model and model2 both refer to the same model, and any change you make via model2 also affects the variable model. In fact, all of the objects in a SimBiology model (species, parameters, reactions, etc.) are handle objects. You can get more information about handle versus value objects in MATLAB here.
    You have to do something special whenever you want to copy a handle object. To copy a SimBiology model, you need to do the following:
    model2 = copyobj(model)
    You can read more about copyobj in SimBiology here.
    Best of luck, and post again if you have more questions!