Can a structure variable be updated within MATLAB Function block in Simulink?

2 vues (au cours des 30 derniers jours)
I am running a simulation of a physical system in Simlink using a MATLAB function block. In this regard, I have some parameters that are defined in a structure in MATLAB workspace. I passed that structure by defining it as a parameter to the MATLAB function block. However, the code in the block attempts to add more fields to that structure, but the Simulink won't allow me to do that, giving the following error:
new fields cannot be added when structure has been read or used
Is there a way I can dynamically update the structure within the Function block?
Thanks

Réponse acceptée

Suman
Suman le 24 Juil 2024
Hi Afaq,
There are some limitations to using variables in MATLAB function block as opposed to the way it would run in a MATLAB environment. These restrictions are likely imposed to allow effiecient C code generation.
One of the restrictions is that you cannot add fields to the struct on the go. Please read about other restrictions while using struct in MATLAB function block here: https://www.mathworks.com/help/simulink/ug/how-working-with-structures-is-different-for-code-generation.html
You can create a struct with all the fields and assign it to zero at the beginning and then go about assigning the fields.
function a = b(t)
struct = struct('f1', 0, 'f2', 1);
struct.f1=t;
a=struct.f2;
end
If you need to add fields to the struct, please refer to using Variable-sized block variables: https://www.mathworks.com/help/simulink/ug/declare-variable-size-inputs-and-outputs.html
I hope that helps!
  1 commentaire
afaq ahmad
afaq ahmad le 24 Juil 2024
Hi Suman,
Thanks for your response. Actually, I had found a way around by using the S-function block instead of Matlab function block. S-function block is slow, but it gets the job done.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by