How do I automatically run a MatLab Script when I start my simulation in simulink

I have a simulink model and it uses a bunch of variables. I also have a script that sets initial values for the variables. I currently run the script before I start my simulation by typing "run scriptname" at the MatLab prompt (or by righ clicking the script file and selecting "run"). I would like this script to automatically run when I start my simulations. I've read the documentation on callbacks in the model properties dialog. I have placed the callback under the StartFcn callback (I've tried just putting the script name, both with and without the .m suffix. )...It didn't run. I've tried it in the InitFcn callback. I've tried loading it in the PreLoadFcn callback. Nothing seems to cause it to run. Does anyone know how to use these callbacks or how I would run a .m file prior to starting my simulation (besides doing it by hand and typing it in at the MatLab prompt?

6 commentaires

Is there an error or warning displayed about the callback not being able to run?
No error, but no indication that it ran either (if I run it manually, there is a line in the script that prints out when complete). Maybe I put it in the InitFcn wrong. I just have "run scriptname" without the .m extension (although I have tried all different ways of putting it in there.)
Try to put just the "scriptname" in the InitFcn callback.
I did and that didn't work, but while doing that it occurred to me that I was probably putting the callback in the wrong area. What I should have also put in this post is that I was using a library component (that another co-worker) created that had a submodel that needed all these values set and they were set in the script. I was putting the script call in my initFcn for the top level model. When I added it to his component, it worked. Thanks for your help....it made me realize I had to look deeper to the other model.
just put the name without m in the InitFcn.
hi ...though replying on a old post but just for info,,,error might be because of the fact that your current matlab working directory is not set to the path where you have saved your model and script file.

Connectez-vous pour commenter.

 Réponse acceptée

Use the InitFcn callback.
If you want the M-script to run every time you run the simulation, InitFcn is the right one.

4 commentaires

File -> Model Properties -> Callbacks
That is what I used. Could I be calling it wrong? I used the Model Properties and clicked the Callback tab,I selected the InitFcn and on the right box I put "run scriptname" (minus the quotes). Then I clicked Apply. When I start the simulation, I get the error that I have undefined function or variable.
just put in "scriptname" without the double quotes.
From the error, it looks like the function is not on the path. Make sure that the function is in the current directory, or added to the MATLAB path.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by