selecting and re-assignment of non-existing parameters in SimBiology does not trigger any warning?
2 views (last 30 days)
Selection by name and re-assignment of model parameters can be done like this
tempVar = sbioselect(modelObj1,'Name','p1'); set(tempVar,'Value',0.1)
BUT if parameter 'p1' does not exist in the model, no error message is produced.
Is there a bulletproof way to perform such assignment?
Florian Augustin on 5 Oct 2022
sbioselect returns all objects in the SimBiology model that matches the search query; in your case all model components whose Name property match p1. If the model does not contain such a component, then sbioselect returns . If the model contains multiple components that match the query, then an array of those components is returned. To guard against those cases, I would use an extra check:
componentObjs = sbioselect(modelObj, 'Name', 'p1');
if numel(componentObjs) ~= 1
% handle case where there is no such component or multiple components
set(componentObjs, 'Value', 0.1);
I hope this helps.
Arthur Goldsipe on 5 Oct 2022
I can think of several ways to answer your question, depending on exactly how you want to handle this situation. sbioselect can return zero components, one component, or many components. Do you want an error only when there are no matches, or also when there are many matches? For now, I'll assume that you want an error any time there isn't one match.
Perhaps the most straightforward thing you could do is verify that tempVar is a scalar. One way to do that is to add a check like assert(isscalar(tempVar)).
This is mostly an issue of style, but you might also consider replacing your call to the set method with a direct set of the property on tempVar (that is tempVar.Value = 0.1). One difference between these two approaches is that the set call will fail if tempVar contains more than one component. If you want to handle multiple matches, you could instead write [tempVar.Value] = deal(0.1). Note however that both of these suggestions will still succeed if tempVar is empty, and tempVar will be a struct with the field Value.
Lastly, I see that you also mentioned that p1 is a parameter. If you really want to make this code "bullletproof" I would also update the sbioselect call accordingly to sbioselect(modelObj1,'Name','p1','Type','parameter'). Otherwise, you might end up selecting some other type of component named p1. Note that this still could return multiple parameters, since you could have several reactions each with a reaction-scoped parameter named p1.