Using Simulink block parameters in a Matlab function

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Eduardo
Eduardo il 27 Gen 2023
Due to not having a clean way to pass down the parameters, another configuration for the model was used.
Kudos to @Fangjun Jiang for his proposal.

Più risposte (1)

Fangjun Jiang
Fangjun Jiang il 6 Gen 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 Commenti
Fangjun Jiang
Fangjun Jiang il 8 Gen 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.
  1. 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.
  2. 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"
  3. 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.
Eduardo
Eduardo il 11 Gen 2023
Mmmm that´s a bummer.
However I think I found a way to accelerate the simulations for the case where I use an external function.
The NLMPC block allows the use of a prebuild .mex function to I think I can compile one with the parameters beforehand. I will give it a go

Accedi per commentare.

Categorie

Scopri di più su General Applications in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by