Borrar filtros
Borrar filtros

Using Simulink block parameters in a Matlab function

4 visualizaciones (últimos 30 días)
Eduardo
Eduardo el 6 de En. de 2023
Respondida: Eduardo el 27 de En. de 2023
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?

Respuesta aceptada

Eduardo
Eduardo el 27 de En. de 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.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 6 de En. de 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 comentarios
Fangjun Jiang
Fangjun Jiang el 8 de En. de 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 el 11 de En. de 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

Iniciar sesión para comentar.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by