selecting and re-assignment of non-existing parameters in SimBiology does not trigger any warning?
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Florian Augustin
2022-10-5
Hi,
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
else
set(componentObjs, 'Value', 0.1);
end
I hope this helps.
Best,
Florian
Arthur Goldsipe
2022-10-5
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Extend Modeling Environment 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!