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

106 次查看(过去 30 天)
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 个评论
huneza khan
huneza khan 2023-5-4
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.

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-6-28
Use the InitFcn callback.
If you want the M-script to run every time you run the simulation, InitFcn is the right one.
  4 个评论
Kaustubha Govind
Kaustubha Govind 2011-6-29
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by