Simulink model rebuilds every time when no changes are made - matlab

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.

Related

step by step simulation in command line for Matlab Simulink model

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

How can I pass Bus signal input to simulink model during runtime

I have a simulink model which takes Bus signal as input. I have passed the bus signal data using Configuration->Data import/export ->input
Now I want to vary this signal from workspace when model is running and see the output during runtime.
But model is taking new data from workspace only when you stop the model and run again. Is there any way to feed the input to model during runtime?
By default Simulink looks in the Workspace for data at initialization, not at every time step. Hence the behaviour you are seeing.
To make it look in the workspace during the simulation you need to force it to do so. This can be done by using set_param to change a dialog parameter.
Once you've made a change to the variable in the workspace, in your case, something like set_param(gcs,'ExternalInput',get_param(gcs,'ExternalInput')) should work.
This is just getting the string that is in the dialog box and poking the (same) string back into the dialog.
This tells Simulink that something has changed and it'll go and re-read the variables.

My simple Stateflow model shows error

I'm almost new to Simulink and Stateflow. I am trying to model some simple state machines. But when trying to run the model, it shows errors (on the main Matlab screen).
Warning: Input port 1 of 'sample/If' is not connected.
Warning: Output port 1 of 'sample/If' is not connected.
Parsing failed for machine: "sample"(#90)
1) Is there anything wrong with this?
2) One more question: How can I add a timer on S2? e.g. we can not stay more than 2 minutes on S2. So as soon as we enter S2, a timer starts, and when it reaches 2 minutes, then should transition to S3.
P.S. For some reasons, the stateflow thumbnail on simulink scheme is not showing the updated model; there is no condition and if_outfput variables anymore!
Well, the error message is pretty self-explanatory: you haven't connected the input Condition of your Stateflow chart to anything, hence the error. Connect it to a signal in your Simulink (whatever represents your condition signal). Likewise, you haven't connected the output of the chart to anything either. You say these variables aren't there anymore, but Stateflow doesn't seem to think so. Have you deleted them from the Model Explorer? If not, Stateflow will still think they're part of the chart. See Use the Model Explorer with Stateflow Objects in the documentation for more details.
For the timer, yes it's possible. At the moment, you exit S2 to S3 when input ==1. You can change the transition to be [input == 1] || after(2000,sec) (I think). You may need to enable support for absolute time in the model configuration parameters. See Control Chart Execution Using Temporal Logic in the documentation for more details.

Set Filename of ToFile Block after Model has Finished

I have a series of ToFile blocks in my Simulink model that each have a unique filename (e.g. "Pulse.mat". I want to store the results of my simulation in timestamped folders based roughly on when I hit the run button / use the sim command.
My solution was to write two scripts, one called during the InitFcn callback and one for the StopFcn/CloseFcn callback (and PreSaveFcn callback).
The InitFcn callback would find all ToFile blocks and change the filename from "Pulse.mat" to something like "../runs//Pulse.mat", and the StopFcn/CloseFcn/PreSaveFcn to revert them to their original ".mat" names. This worked on my small test model, however when I attempted to integrate it into my actual model I receive the following error:
Error evaluating 'StopFcn' callback of block_diagram '<model_name>'.
Caused by:
Cannot change parameter 'Filename' of '<ToFile Block>' while simulation is running. The block was made virtual as it was optimized for simulation
From my research I discovered that the StopFcn actually executes before the simulation is "done" (for whatever reason), but why did it work in my initial test model? Do my ToFile blocks in my actual model have some property set that causes this error to occur?
The block has been virtualised in your larger model; which seems to be changing the execution point. One option might be to untick 'Block Reduction' in the 'Optimisation' pane of the model configuration parameters dialogue.
Alternatively, there might be another approach that you could try -
Setting the model properties 'StartFcn' to something like :
evalin('base','resultTimTag = datestr(clock, ''yyyymmdd_HHhMM'');')
and setting the model properties 'StopFcn' to :
targetDir = evalin('base','resultTimTag');
mkdir(targetDir);
outputs = dir('*.mat');
for i=1:length(outputs)
movefile(outputs(i).name,targetDir);
end
evalin('base','clear resultTimeTag');
It's possibly a little more brute-force than your approach, but seems to work quite nicely...

Integrating the code generated from simulink in the project

I have a simulink model and I generate the code using simulink coder for Generic Real-Time target.
I then copy the generated files (*.c and *.h) into my project in MPLAB.
Here I have the main.c file and here I use MdlStart() for initializing the model one time and then in infinite loop I use MdlOutputs() and MdlUpdate() for running the model. Everything works fine.
Now actually I want to speed the execution of the model and therefore I want to split my model into sub-systems and execute the sub-system only when required (e.g. I want to execute some sub-systems everytime and some only every few ms instead of everytime). I could split the model into subsystems and also made every sub-system atomic so that I get the different function for every sub-system. Now these different sub-system's functions are called from MdlOutputs() and MdlUpdate(). So they execute everytime the model is executed.
Actually I want to use some timer in my MPLAB project and then execute few sub-systems lets say at every 100ms. I also do not want to modify the auto-generated *.c and *.h files.
So instead of executing these sub-system's functions everytime through MdlOutputs() and MdlUpdate(), Is it possible to call the sub-system's functions from my main.c file without touching the auto-generated *.c and *.h files?
I hope that I have explained it well.
waiting for your help.
You should try to model everything you are trying to do with subsystems inside Simulink. For example, you can use "Enabled subsystems" or "Triggered subsystems" to create subsystems which run only on some time steps. I am not sure what kind of timer you are referring to. If it is Simulink time then you would need to run different subsystems at different sample rates which would give you a multirate model. If your timer is just another external source, then try triggered or enabled subsystems. That is the best way to avoid modifying generated files.
Doc for these subsystems are at http://www.mathworks.com/help/simulink/conditional-subsystems-1.html.