If I have a variable that contains a SimBiology parameter object, and I change it, the object reflects that change:
par1 = addparameter(rx1kl,'kf');>> par1.Value = 0.3par1 = struct with fields: Value: 0.3000
This is expected behavior. If I change an object, I should see that change reflected in the object.
This does not happen with variant arrays. If i create a model mc with several variants, I can a Variant Array object, thus
mcv = mc.Variants;
Supposing I want to delete the second variant in the array, which is named 'Var2'. I should be able to do this in a few ways, with the variant array object.
% Either
delete(mcv(2)); % Method 1, or
removevariant(mc,mcv(2)) % Method 2, or
removevariant(mc,'Var2') % Method 3
However, if afterwards I look at the mcv object, the array is the same size as before the deletion. And Method 1 changes the mcv object from a Simbiology Variant Object to a modified array using handles. That is, if I display the object using
mcv
I get different results than if I refresh the mcv variable:
mcv = mc.Variant
the variant is seen to have been deleted from the model object, but not from the variant object denoted mcv.
So in one case (parameters) modifying the object shows up as expected. In another case(variant arrays) certain modifications do not have the expected outcome and modify the named object ('mcv') in different and unexpected ways. And given that the mcv and mc.Variant objects are different after the delete() and remove() functions I suspect that creating mcv creates a handle to a copy of the object. But the model object itself appears to have been modifed as expected. I don't understand it, anyway.
The workaround is to do an mcv=mc.Variant command after every delete() or removevariant() command. I suspect this is not as intended. What's going on with this? Or am I doing something wrong?
I've created a script to show the behavior
%% Demo weird variant behavior
dm = createnewmodel; % See function below
dcv = dm.Variants; % create a handle for the variant array
disp('Result of removing variant 2 with removevariant() function')disp('Original Array, using handle:');dcv % display original variant array
removevariant(dm,dcv(2)); disp('Modified Array, using handle:');dcv % display original variant arraydisp('Not deleted in handle!');disp('Showing that this removal affects the actual model');dcv = dm.Variants % display variant array
disp('Result of removing variant 2 with delete() function')dm2 = createnewmodel;dcv2 = dm2.Variants;disp('Another model, original variant array:');dcv2delete(dcv2(2)); % use delete to delete variant per documentation
disp('Modified Array, using handle:');dcv2disp('Supposedly deleted variant');dcv2(2) disp('Showing effect on model')dcv2=dm2.Variants % only shows array of handles
function dm = createnewmodel();dm = sbiomodel('demomodel'); % demo model
dc = addcompartment(dm,'democomp'); % add one compartment
dc.Capacity = 5; dc.CapacityUnits = 'liter';ds1 = addspecies(dc,'dspec1'); % add two species
ds2 = addspecies(dc,'dspec2');set(ds1,'InitialAmount',100); % set ICs
set(ds2,'InitialAmount',0);rx1 = addreaction(dm,'dspec1','dspec2'); % add one reaction
rx1kl = addkineticlaw(rx1,'MassAction');par1 = addparameter(rx1kl,'kf');par1.Value = 0.1;par1.ValueUnits = '1/minute';v1 = addvariant(dm,'var1'); % add three variants
addcontent(v1,{'species','dspec1','InitialAmount',111})addcontent(v1,{'parameter','kf','Value',0.2})v2 = addvariant(dm,'var2');addcontent(v2,{'species','dspec1','InitialAmount',90})addcontent(v2,{'parameter','kf','Value',0.3})v3 = addvariant(dm,'var3');addcontent(v3,{'species','dspec1','InitialAmount',95})addcontent(v3,{'parameter','kf','Value',0.23})addcontent(v3,{'compartment','democomp','Capacity',7})end
Best Answer