Persistent variables from Simulink to Matlab

조회 수: 7 (최근 30일)
Baf85
Baf85 2021년 7월 5일
편집: Walter Roberson 2021년 9월 3일
Hi, I have an Entity Server with a persistent matrix that changes with every entity entering. After i've run the simulation i want to open the matrix in the Matlab workspace (or also in Excel if that's possible). How does that work?
Here's the code that's called after the entities enter the server. Regards!
persistent cnt
if isempty(cnt)
cnt=0;
end
persistent x
if isempty(x)
x=zeros(100,3);
end
cnt=cnt+1;
x(cnt,1) = entity.Attribute1;
x(cnt,2) = entity.Attribute2;
x(cnt,3) = entity.Attribute3;

답변 (3개)

Vimal Rathod
Vimal Rathod 2021년 7월 8일
Hi,
You could refer to the following link to know mroe about how to export the simulink data from the model to the worksapce.

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2021년 9월 3일
I think the best way to do this is to create a Simulink function that includes no blocks, but its output port is connected to a "To Workspace" block. After the termination of the simulation, results are sent to the base workspace. You can also create a "PostSimFcn" to automatically export this data to Excel, but you need to use SimulationInput methodology.

Walter Roberson
Walter Roberson 2021년 9월 3일
편집: Walter Roberson 2021년 9월 3일
It is not possible to access persistent variables inside a function that is not executing (or at least there is no documented way to do so.)
If you were to write in the form of a Level-2 S function, you could implement a Terminate https://www.mathworks.com/help/simulink/sfg/terminate.html that emitted the values some-how.
If you were to write in the form of a Level-1 S function, you would look for flag value 9 for Terminate; https://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html
Or you could rewrite the persistent variables in the form of global variables, and make sure to register the global block at the appropriate model level https://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html

카테고리

Help CenterFile Exchange에서 Discrete-Event Simulation에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by