Run Matlab FUNCTION at the end of Simulink simulation

Hello,
in a simulink simulation I compute some signals.
At the end of the simulation, when the system reach the steady-state condition, I want to use the final value of such signals to create a custom 3D plot. To do that I already built a Matlab FUNCTION block.
I don't want to run such Matlab FUNCTION block during the simulation because the computational time grows so much, and moreover I am interested only in the final value (steady-state).
How can I run such "Matlab FUNCTION block" only at the end of the simulation?
Thanks and best regards

2 commentaires

Why not use To block and then use those results in a script file?
Hello Madhan,
my idea was to create a custom simulink block (with all the instructions for the plot) and add it to my library.
Therefore I want to have all the instructions inside the custom simulink block. I don't want to have a piece of code in a separate script file.
Do you have suggestion to do that?

Connectez-vous pour commenter.

 Réponse acceptée

Sai Sri Pathuri
Sai Sri Pathuri le 30 Mai 2020

0 votes

You may use StopFcn callback in your model. Refer the following link for Model callbacks. Expand Model Simulation Callback Parameters in the below documentation for StopFcn.

7 commentaires

Alberto Mora
Alberto Mora le 31 Mai 2020
Modifié(e) : Alberto Mora le 31 Mai 2020
Hello Sai, thanks for your replay.
I try your suggestion but it does not work. Below you can find an image of the simulink model.
In particular I put my custom plot function in a block "MATLAB function", with the required inputs for the plot (constant and signals).
There are also two "special" inputs: ifPlot is a scalar that I set manually 0 or 1 to decide if I want to plot the results, "FlagEndSim" is a value used for evaluate the end of the simulation (also in this case 0 or 1).
Then I created a callbacks "InitFnc" with a constant FlagEndSim=0 in order to don't execute the "MATALB function" at the beginning of the simulation. Then I also created a "StopFnc" callbacks with a constant FlagEndSim=1 that is set at the end of the simulation.
Inside my "MATLAB function" there is a if condition where I check simply that both conditions are 1:
if ifPlot && FlagEndSim
% my plot function
end
However the matlab function is not executed at the end of the code, even if the FlagEndSim became 1 at the end of the code.
How can I solve it?
Previously, I did not understand your question correctly. If you need only final value, you may try the following.
  • You may use this in the InitFcn or create through command line and use input port for FlagEndSim instead of constant block. lengthOfOutput is the length of yout (or) tout.
FlagEndSim = [zeros(lengthOfOutput,1);1];
  • Create vector t. Run the simulation once (without below settings) and use tout as t.
  • Check Input in Load from workspace section of Data Import/Export tab. Change input to [t,FlagEndSim].
Alberto Mora
Alberto Mora le 1 Juin 2020
Modifié(e) : Alberto Mora le 1 Juin 2020
Hello Sai, is there a easyer solution? My idea was to create a subsystem of the simulink model (and obviously also with the MATLAB function for the plot) and give this subsystem to my colleagues.
Therefore it would be better if all the instructions were contained within that subsystem, without making any changes as you suggest (create time vector t, load it from the workspace modifying the "input" in load from workspace).
If you think that there is a easyer solution, please tell me. Maybe creating a "Matlab function" in simulink and run it at the end of the simulation is not the best approach. Honesty I prefer this approach in order to have all the instructions embedded inside this subsystem, but maybe this is not the best solution... Maybe there is a better way to do that (e.g. export signals in the workspace and run a function?)...
You may try this. When you are plotting through MATLAB Function block, check that hold is off. Use below command in your function.
hold off
Due to this, you will have only current simulation results plotted in each time step.
So, at last time step, the results of last time step, i.e. steady state (as you described) are plotted.
You do not need the constant block for FlagEndSim.You may just plot based on ifPlot
if ifPlot
% my plot function
end
This is a good suggestion, but the problem is that if I continue to "overwrite" (i.e. update) the plot at each time step of the simulink simulation, the computational time of the simulink simulation grow so much, because the plot is updated many thousand of times!
This is why I prefer to avoid to plot during the simulation and I prefer to plot only the last step (at the end of the simulation).
Okay, In order to avoid overwriting and plot only last time step data, you may try this.
In constant block you used for FlagEndSim, use constant value as
[zeros(lengthOfOutput-1);1]
and Sample time as below. This is to retain previous sample rate after conversion by buffer
stepSize*lengthOfOutput
After the constant block, use buffer with output buffer size of 1.
With these changes, you can plot the results of only last simulation.
This is an example I tried at my end. I used a StopTime of 10 and a variable solver. For this, the output is of length 51 and step size is 0.2 (obtained from tout)
So, I have used the following settings
% Constant value
[zeros(50);1]
% SampleTime
0.2*51
At the end I decided to follow partially your suggestion, comparing the time of the simulation (using clock) and a time defined by the user that set like few ms before the end of the simulation.
It is a pity that does not exist a in matlab way to call a matlab function exacly at the end of the simulation... But it's ok, I solve the problem so does not matter.
Thank for the help and best regards.
Alberto

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by