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?

답변 (2개)

Florian Augustin
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
  댓글 수: 1
emjey
emjey 2022년 10월 5일
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.

댓글을 달려면 로그인하십시오.


Arthur Goldsipe
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.
  댓글 수: 1
emjey
emjey 2022년 10월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Extend Modeling Environment에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by