Is there a way to log variables from a Matlab Function Block (in Simulink), that is not output?

18 vues (au cours des 30 derniers jours)
I would like to log variables from a Matlab Function Block (in Simulink) similar to the functionality of a "To Workspace" block saving them in a SimulinkOutput object. As those variables are only internal I cannot connect them to said block. I know, I could simply define them as outputs and only connect them to the "To Workspace" block, but I would like to avoid this work around to keep my model clean (it would be around 10 variables). I have browsed through docu, but could not find something that would solve my issue. So, I am wondering if there is an elegant way to log also variables from a Matlab Function Block (in Simulink) as a an array, like it is possible for signals (connecting them to a "To Workspace" block)?
  5 commentaires
Paul
Paul le 10 Juil 2024
I'm pretty sure that global data in Matlab Function blocks in Simulink is quite different than in base Matlab. Here is one relevant doc page: Use Global Data in MATLAB Function Blocks. Not sure if there is a clean way to get a globar variable into the base workspace from a Matlab Function block.
Do you want to log the value of the local variable(s) each time the Matlab Fucntion block executes? Or only the value of the local variable at the time the simulation ends?
Marko Marković
Marko Marković le 11 Juil 2024
@Paul I would like to have values of the variable of each time step, but not necessarily in "real-time". I do not need access to those variables during the simulation.

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 11 Juil 2024
Here's one option based on the idea of @Aquatris to specify the local variables that you want to log from the Matlab Function as global. Don't know if doing so introduces other risks.
However, as best I can tell this approach uses an undocumented feature, so BUYER BEWARE.
Open the example model used at this doc page. You might want to consider saving the model under a different name.
Open the Model Explorer and navigate to the Model Workspace of the model. There, on the right side of the Model Explorer window you will find the Matlab code that populates the model workspace with the global signal object that's used in the Matlab Function block.
A = Simulink.Signal;
A.DataType = 'double';
A.Dimensions = 1;
A.Complexity = 'real';
A.SamplingMode = 'Sample based';
A.InitialValue = '5';
Add the following line. The LoggingInfo property has other fields that you may want to take a look at. I can't find the LoggingInfo property of a Simulink.Signal in the doc. BUYER BEWARE.
A.LoggingInfo.DataLogging = true;
Simulate the model
slout = sim(....);
and the variable 'A' should show up in slout.dsmout. The variable name 'dsmout' is specified under Model Settings -> Data Import/Export in the Save to Workspace or File list (as long as Data Stores is checked).

Plus de réponses (0)

Catégories

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

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by