step by step simulation in command line for Matlab Simulink model - command-line

I want a solution for command line implementation for Matlab Simulink. In simulink model there is simulation switch called "step forward". With this I am able to run step by step and able to see the outputs in scope for each sample time.
I am also able to change some inputs for constant input blocks at any sample period time. However, I want to do this in command line, because I have a huge model and I want to reuse this model verification script.
I am able to do some thing like this using "sim" command. But the sim command is just simulating the entire model at once, and I could not observe/change the intermediate outputs/inputs. The "sim" command with a certain stop time is running for certain sample periods, but later if I give the next "sim" command, it is running from the beginning and not starting from where it stopped. But, is there any exact Matlab command to step and pause (for one sample period time) the simulation, like that in simulink gui.

You want to use SimState to save the states of the model at the end of one simulation, and restore them at the beginning of the next simulation. How to do this is described in the documentation under Save and Restore Simulation State as SimState.
You cannot make any structural changes to the model (e.g. add or remove block) between each call to sim. There are also a few blocks which don't support SimState, see the documentation for more details. Note that this is for the latest release (R2015a), more and more blocks were added to support SimState over the past few years, so you want to check the release notes depending on the release you are using.

You can try this:
sldebug('gcs');
you are now in debug mode, you can type in help to get an overview of the available commands. To run through the model type in:
next
Each time you type next, you move the simulation one step forwards.
The following link will provide you with further details on how stepping through a simulation works.
http://www.mathworks.com/help/simulink/ug/how-stepping-through-a-simulation-works.html
For more details on using the debugger to run through the simulation step by step you can go to:
http://www.mathworks.com/help/simulink/ug/running-a-simulation-step-by-step.html

Related

Abort execution of parsim

For the use case of being able to abort parallel simulations with a MATLAB GUI, I would like to stop all scheduled simulations after the user pressed the Stop button.
All simulations are submitted at once using the parsim command, hence something like a callback to my GUI variables (App Designer) would be the most preferable solution.
Approaches I have considered but were not exactly providing a desirable solution:
The Simulation Manager provides the functionality to close simulations using its own interface. If I only had the code its Stop button executes...
parsim uses the Simulink.SimulationInput class as input to run simulations, allowing to modify the preSimFcn at the beginning of each simulation. I have not found a way to "skip" the simulation at its initialization phase apart from intentionally throwing an error so far.
Thank you for your help!
Update 1: Using the preSimFcn to set the the termination time equal to the start time drastically reduces simulation time. But since the first step still is computed there has to be a better solution.
simin = simin.setModelParameter('StopTime',get_param(mdl,'StartTime'))
Update 2: Intentionally throwing an error executing the preSimFcn, for example by setting it to
simin = simin.setModelParameter('SimulationCommand','stop')
provides the shortest termination times for me so far. Though, it requires catching and identifying the error in the ErrorMessageof the Simulink.SimulationOutput object. As this is exactly the "ugly" implementation I wanted to avoid, the issue is still active.
If you are using 17b or later, parsim provides an option to 'RunInBackground'. It returns an array of Future objects.
F = parsim(in, 'RunInBackground', 'on')
Please note that is only available for parallel simulations. The Simulink.Simulation.Future object F provides a cancel method which will terminate the simulation. You can use the fetchOutputs methods to fetch the output from the simulation.
F.cancel();

Read simulink signal data into matlab during simulation

I want to continuously read simulink signal data into the command line while the simulation is running. get_param() seems to be blocking so that doesn't quite work when put in an infinite while loop.
I'm now trying to use a UDP send block but I can't seem to receive data. My UDP block sends data to localhost over remote port 25000 and local port 25001.
In matlab I use the following code but it simply times out with no data
u=udp('127.0.0.1', 25001,'LocalPort',25000);
fopen(u)
fread(u)
fclose(u)
delete(u)
What are my options to continuously read out simulink signal data into Matlab CLI?
Control Simulation Using the "set_param()" command like follows:
set_param('sys','SimulationCommand','WriteDataLogs')
For a working example, type "sldemo_varsize_basic" in the matlab command window. Then above command becomes
set_param('sldemo_varsize_basic','SimulationCommand','WriteDataLogs')
If you set the simulation time to sufficiently large and start the simulation, the "simout, simout1", "tout" and "xout" variables are created/updated in the workspace every time you issue the command above.
Unfortunately, I was not able to find a good quality documentation of this feature.
Are you trying to store the value of your model outports DURING simulation? This is not possible because the variables 'simout, simout1", "tout","xout" etc are created only once simulation is OVER.
In order to read/store the value of outports during simulation, you will have to attach an 'Runtime Object' to the outports.
Refer 'Access Block Data During Simulation' in the Simulink documentation or see this link: http://in.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html?s_tid=gn_loc_drop
Hope it helps :)
This question has already been answered here using RunTime Objects as I have described above:
https://stackoverflow.com/a/17006419/6580313

Simulink model rebuilds every time when no changes are made

I've been having this issue since I started using this Simulink model. No matter what I try, Simulink rebuilds the model every time I run it. I've taken a look at the checksums using the following procedure:
>> [cs1, cs1det] = Simulink.BlockDiagram.getChecksum(<model name>);
>> (Here I hit run on the model without changing anything)
>> [cs2, cs2det] = Simulink.BlockDiagram.getChecksum(<model name>);
>> cs1 == cs2
1
1
1
1
Why would Simulink rebuild the model if the checksums are the same? I did create another model with some simple logic in it and after compiling once I was able to run it multiple times without Simulink recompiling it.
Additionally, when I hit run when in Rapid Accelerator mode I notice that an asterisk pops up next to the model name in the title. Since this means that the model has unsaved changes, could running it cause any changes to be made? I've checked the callback methods and there's nothing being run when the sims starts (in fact there's nothing in any of the callback methods).
Is there some kind of basic compilation / initialization / build that Simulink runs through before it runs every time? I'm trying to run my model in Rapid Accelerator mode without having it recompile since it takes roughly 45 minutes to compile every time.
Have I overlooked some configuration parameter of the model? Is there some global MATLAB/Simulink variable I need to change?
Is it building or is it updating?
Typically, when you hit the Run button, MATLAB has to propagate signals, check your model for errors, initialize variables . . .
It may seem like building, but to actually confirm it is building, check to see of you have yourmodelname.c file in the folder.
Also check File Menu preferences, and go through the functions there and see if something is getting called.
Could you try to set Configuration Parameter -> Code Generation -> Interface -> Code replacement library to None (In case that your demand is not related to GNU library ...) on reference model?
By the way, it is working for me.

How to export simulink data to workspace during simulation?

I want to retrieve the data from simulink during simulation, and use serial network function to send these data to another program. Because I need to use another program to do some tricks and send command back to simulink, so I have to get data from simulink during runtime so that another program can make the right command.
I've tried using To Workspace block to export the data.
However, I can only got value in the very beginning of the simulation.
And I've also tried using scope and change some properties: check Save Data To Workspace and Uncheck Limite data to Last.
First, I started simulation, and I found the ScopeData didn't appear in the Workspace. Only when I stop simulation, ScopeData would appear in workspace.
And after that, I can use ScopeData.signals.values to get values.
But what I want is: when I start simulation, ScopeData would appear in workspace so that I can send these data to other program.
Does anyone know how to achieve this?
I found this page might be helpful, but I still don't know how to continuously export data during simulation.
Use get_param to read data from just at the current time. Also to send the data back to Simulink with set_param of a gain or another block.
An example of get_param
First load and start the simulation:
load_system('myModel')
set_param('myModel','SimulationCommand','Start');
To read data on any line of your simulink model:
Get a simulink block object (let's try a Clock with the name Clock):
block = 'myModel/Clock';
rto = get_param(block, 'RuntimeObject');
Then get the data on its first (or any) output port (or input) of that block.
time = rto.OutputPort(1).Data;
You could do the reading, in a timer callback.
Also this might be helpful: Command Line Functionality for Simulink
During simulation Simulink stores logged data in an internal buffer and only writes the data to the Workspace when the simulation is paused or stopped.
It sounds as if you really need to write an S-function (which will get signal values on a timestep-by-timestep basis) and communicate with Proteus that way.
Of course Simulink is a non-realtime simulator, so if you are talking about doing anything resembling real-time control then you are most likely taking the wrong approach altogether.
At any time during simulation you can force Simulink to write the simulation output data to the workspace:
set_param(bdroot,'SimulationCommand','WriteDataLogs');
I've found that this command is quite unstable in my Matlab 2010a for Win64. In particular I have to avoid it when simulation is stopped (i.e. first check
get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and asks to be restarted.

Running simulink model step-by-step

I am trying to link an external app to a Simulink model and run the simulation step-by-step. In the model command description it says 'other MATLAB program-based tools to run a simulation step-by-step', what it does not say is how you do this.
The process looks to be:
compile model
then for each time step:
call outputs = model(t,x,u,'outputs');
call dstates = model(t,x,u,'update');
call derivs = model(t,x,u,'derivs');
What appears to be missing is the call to calculate the continuous states; does any one know how to do this?
There is no simple call for calculating continuous states.
You must write a code to calculate them.
More about that on this link:
http://www.mathworks.com/matlabcentral/answers/7267-what-is-the-best-way-to-execute-a-simulink-model-step-by-step-interactively-from-matlab
There is also a set of files with example.