Due to not having a clean way to pass down the parameters, another configuration for the model was used.
Using Simulink block parameters in a Matlab function
4 views (last 30 days)
Show older comments
Right now I am trying to use the value of a parameter of one of my simulink blocks in a Matlab function (independent .m file in Matlab´s path) that is called multiple times during my simulations.
The function is called for example during the execution of an NLMPC block.
I have tried multiple things like defining a global variable within the initialization of my blocks but I still do not get access to the value within the function.
Another try was to use the "get_param" or the "evalin" functions, but these are not supported during code generation.
Is there a practical way of doing this?
0 Comments
Accepted Answer
More Answers (1)
Fangjun Jiang
on 6 Jan 2023
If this value is something you type in at the dialog of the Simulink block, then
Specify it as 'MyValue' at the dialog, then
In MATLAB base workspace, assign it MyValue=WhatEverValue
Then in the MATLAB Function block, click "Edit Data", add 'MyValue' as a data first and then specify it as 'parameter'
This way, the Simulink block and the MATLAB Function block share or use the same parameter 'MyValue'
5 Comments
Fangjun Jiang
on 8 Jan 2023
A callback function in the nlobj needs to read the parameter value of a block in the model, and the function needs to support code generation. I don't see a straightforward method.
Using variant subsystem might be a stretch way to achive it.
Let's say the block has three pre-defined values, "ele1", "ele2", "ele3", you can specify three variants.
- In variant 1, the block with "ele1" pre-selected is used. A nlobj with function FcnEle1 is used. Construct similar variant 2 and variant 3.
- FcnEle1, FcnEle2 and FcnEle3 don't need to duplicate any code. It could be calling Fcn(Par) where Fcn() is the core function, Par value could be "ele1", "ele2" or "ele3"
- If you don't like the FcnEle1, FcnEle2 and FcnEle3 approach, then you can add a Constnat block inside Variant1, its value is 1 and is feeded to the nlobj. In Variant2, its value is 2 and is also feeded to the same nlobj.
Overall, I think it is achieveable using Variant SubSystem. Look it up if you are not familar with it.
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!