Is there a way to log variables from a Matlab Function Block (in Simulink), that is not output?
조회 수: 18 (최근 30일)
이전 댓글 표시
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
Paul
2024년 7월 10일
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?
채택된 답변
Paul
2024년 7월 11일
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).
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!