How does Simulink recognize variables from an m-file?

9 次查看(过去 30 天)
I am trying to run a .slx file named train.slx from the Simulink window, and have it use variables defined in a .m file (named system.m). Should both the .m file and the .slx have the same prefix (i.e., "train") in order for the train.slx Simulink file to recognize that it should be using the variables from the specific m-file that I want it to read?
Thanks in advance! :) I'm very confused about this. I know it's probably a very simple operation but I am still learning Simulink and am unsure how this works.

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-4-27
编辑:Azzi Abdelmalek 2014-4-27
Don't use the same name, also avoid the name system. Before running your simulink model, you have to run your m-file
If you want to run automatically your m-file when run your simulink model, you can add to the initfcn of your model callback the names of your m-files
  1 个评论
Kelsey
Kelsey 2014-4-27
Thank you very much! Now, another question--I understand that, once the Simulink model is ran, one double-clicks the "Scope" block in order to see the graphical output. I am now working on a more complicated system (I attached the model, as an .mdl file). It should work when I run it, as I've defined all variables used (see code for m-file "controlsystem.m" below), and this is also a sample model that I found on a university's academic website. However, when I run it, I get this error message:
Warning: Using a default value of 20.0 for maximum step size. The simulation step size will be equal to or less than this value. You can disable
this diagnostic by setting 'Automatic solver parameter selection' diagnostic to 'none' in the Diagnostics page of the configuration parameters dialog
> In controlsystem at 8
Error using controlsystem (line 8)
Derivative input 1 of 'train/Integrator2' at time 576.92657805275451 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)
Code for corresponding m-file:
open_system('train.mdl')
M1=1; %kg
M2=0.5; %kg
g=9.8; %m/s^2
mu=0.02;
k=1; %N/s
F=1; %N
sim('train.mdl')
I am currently going from time 0 to 1000. Even when I reduce the time to something lower than 576.925, say 500, I get nothing as an output. Any idea what could be wrong??

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by