Initialization in Simulink
Show older comments
Is it possible to run a Simulink model with model variables initialized directly from a stored file accessed by Simulink instead of being them being initialized within MATLAB? Whenever I run a Simulink model it seems that I first have to assign values to any MATLAB workspace variables by going into MATLAB- it would be easier if they could be automatically initialized within Simulink. Is that possible?
Answers (3)
Andreas Goser
on 7 Feb 2011
3 votes
I am not sure why you find that easier, but yes this is possible. You can apply a callback function, e.g. the "Pre Load Function" that loads data from a file.
Menu File -> Model Properties -> Callbacks. In the "PreLoadFcn", you can use a load command. Variables are still stored in MATLAB workspace.
1 Comment
mohammad seifi
on 27 May 2012
If your model have an algebraic loop,and you want to solve it by initial condition what do you do?how i can assign a value to a varaible when simulink want to start?
Kaustubha Govind
on 7 Feb 2011
0 votes
You can also store data in the Model Workspace if you'd like to restrict the scope of your variables.
Huy Hoang
on 24 May 2012
0 votes
Dear Andreas and Kaustubha,
It is lucky for me to find this topic. I am getting stuck with the re-initialization problem. I have a model (for a wastewater treatment system), in which I have to use an integrator that I have to re-initialize after every cycle of the treatment process. The values are stored in the worksapce along with the simulation. Thhose obtained by the end of each cycle will be used to reinitialize the system (integrator).
I do not know how to (i), take the last values at the end of each cycle from the matrix data that is stored during the simulation and (ii) repeat the re-initialization of the system with those values. Can you give me some ideas?
4 Comments
Kaustubha Govind
on 24 May 2012
Huy: Does a "cycle" here involved a full simulation-loop? In other words, the model simulation finishes, and you hit play again to start the next cycle? Or do you just have multiple cycles within one simulation run?
Huy Hoang
on 29 May 2012
Hi, thanks for your reply. Indeeed this is a simulation loop with repeating cycles. Each cycle last for 8 hours and the last value of previous cycle will be used to initialize the next one. I have set the integrator in my model to reset after every 8 hrs and using the external output (last values of very cycle) for the initial condition of every cycle. My problem is I am able to store last values to the workspace (matrix of time and 13 variables) but I do not know how to call those values to reset my system :-(
Kaustubha Govind
on 29 May 2012
Not sure which block/subsystem you are using to perform the reset every cycle, but why not just connect the signal (which you are logging to workspace) directly to the external IC port? I think it will only be read when it is time for the reset.
Huy Hoang
on 30 May 2012
Do you think it will pick up only the signal at the time the integrator is reset?
I have tried wiht your suggestions. I just connected the signal to the IC port but it could run only for mor then a day (3 cycle in my case) then an error appeared on the screen saying:
"'Derivative input 2 of 'Pilot_2405/Reaction + Filling - 6hrs/Integrator' at time 1.000000000000032 is Inf or NaN. Stopping simulation. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)''. I will check why this happens. Thanks a lot.
Categories
Find more on Event Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!