selecting and re-assignment of non-existing parameters in SimBiology does not trigger any warning?

1 vue (au cours des 30 derniers jours)
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?

Réponses (2)

Florian Augustin
Florian Augustin le 5 Oct 2022
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
  1 commentaire
emjey
emjey le 5 Oct 2022
Thanks Florian, yes, it looks like what you proposed is a fail-safe method. I was simpy expecting that I get error if I assing a non-extisting component, but now I will be more careful when dealing with assignments.

Connectez-vous pour commenter.


Arthur Goldsipe
Arthur Goldsipe le 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.
  1 commentaire
emjey
emjey le 5 Oct 2022
Thanks Artur, as explained above, I would normally expect an error, both when there is not such compoment I try to assign or if there are many.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Extend Modeling Environment dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by