check referenced models do exist

조회 수: 7 (최근 30일)
you mess
you mess 2020년 7월 2일
댓글: you mess 2020년 7월 6일
I have a model which contains multiple models referenced to it sequentially.
Eg. Main_model has 2 mdlrefs sub_mdl1 and sub_mdl2. sub_mdl1 has 4 mdlrefs and sub_mdl2 has 5 mdlrefs.
I'm trying to configure a setting in all the referenced model within Main_model.
The script seems to work correctly if all the ref models(within Main_model) do exist in the directory.
But suppose if one(sub_mdl1_num2) of the 4 models of sub_mdl1 is renamed or missing(for that matter), then the command
modelRefNames = find_mdlrefs('sub_mdl1', 'ReturnTopModelAsLastElement', false)
results in an error:
No system or file called 'sub_mdl1_num2' found.
Error in load_model
This made me think. As I'm dealing with a model which is dependent on other models(being generated at a later stage, assume Stage 2),
so running this script at Stage 1 will result in this error, due to unavailability of sub_mdl1_num2, which is meant to be generated at Stage 2.
Keeping a check if all the referenced models are present or not before the configuring a setting would be great.
So, this is where I'm stuck. Do we have any feature/function within find_mdlrefs, which can warn us before trying to load absent model ?
Or anything of this sort. I just want the existing and easiest way to do so,
because adding another maneuver to perform this prior check would mean another 5-6 seconds added to script execution.
Thanks in advance, umesh.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 7월 2일
You can do this.
MdlRef=find_system(Model,'BlockType','ModelReference');
for k=1:length(MdlRef)
SubModel=get_param(MdlRef{k},'ModelFile');
if ~exist(SubModel,'file')
error([SubModel,' does not exist'])
end
end
To make this work for nested model reference, you need to make the above code a recursive function.
Keep in mind, the only way to find what's inside a model is to load or open the model. In the nested situation, if 'sub_mod1.slx' does not even exist, there is no way to know how many model references exist in 'sub_moe1'.
  댓글 수: 1
you mess
you mess 2020년 7월 6일
Thanks for your valuable answer. Actually, I was hoping for a simpler solution, maybe a parameter within the find_system() or find_mdlrefs().
Anyway, I've used the given snippet and I've noticed that if the model doesn't exist in the dir(s) then SubModel will return an empty string. So instead of exist() I'm simply checking if it's not empty.
Thanks for your help ! appreciate it !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Model References에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by