How to select a subsystem for convertToModelReference with an unknown subsystem name?

1 view (last 30 days)
Greetings, Gurus!
Forgive me, I'm a novice.
I'm writing a function that will eventually be used to automatically generate C code for a given model and build reusable functions for them. Each model will be formatted so that at level 1 there is only one subsystem with the desired inputs and outputs before it is passed to this function. Other than that, the number of inputs and outputs may vary. The function will be handed only the model and no other paramaters using MATLAB nodisplay.
*** I read on the set_param help page that set_param cannot be used to run the sim when using nodisplay, so I'm using sim() to run and modify the configuration parameters instead. (incase that's relevant)
"If you use matlab -nodisplay to start a session, you cannot use set_param to run your simulation. The -nodisplay mode does not support simulation using set_param. Use the sim command instead." ***
If I understand what I'm doing, I next need to take that lonely level 1 subsystem (whose name I wont know) and pass it to convertToModelReference, which I haven't been able to do. I've tried using find_system as well as some others like Simulink.findBlocksOfType.
>> find_system(args,'SearchDepth',1)
ans =
4×1 cell array
{'TestGEN_Model' }
{'TestGEN_Model/In1' }
{'TestGEN_Model/TestGen'}
{'TestGEN_Model/Out1' }
>>
How would I do this properly, so the return is only {'TestGen'} ? Is this the right way to go about this?
My goal again is to use this return with convertToModelReference to make this level 1 subsystem the model reference;
Simulink.SubSystem.convertToModelReference(args,'UseConversionAdvisor',true)
[tf,h] = Simulink.SubSystem.convertToModelReference(subsys,args)
...after that, set the subsystem parameters to treat as atomic and make rusable function with set_param?
I'll finsih by saying "Thank you!" I'm new - and know I'm likely approaching this problem incorrectly. I will happily accept any advice, direction, or detailed and brutal critisim regarding my approach.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 5 Aug 2022
Since you have only one Subsystem block in the root level of the model, this will return this Subsystem.
find_system(args,'SearchDepth',1,'BlockType','SubSystem')
  2 Comments
Andrew
Andrew on 7 Aug 2022
Thank you for your answer. I'm afraid that returns an empty cell. I beleive the issue is the incoming subsystem is not named SubSystem, but some unknown name. Perhaps find_system isn't the right avenue?
Fangjun Jiang
Fangjun Jiang on 8 Aug 2022
Edited: Fangjun Jiang on 8 Aug 2022
'SubSystem' is the value for property 'BlockType', not the block name. The command is absolutely correct. The only issue is that you need to check the value of variable 'args'.
Drop a SubSystem block to a new model to test it out, specify the name of the model instead
find_system('ModelName','SearchDepth',1,'BlockType','SubSystem')

Sign in to comment.

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!