Variable scope in Simulink .mdl files

2 views (last 30 days)
I am trying to address a Simulink .mdl file in a call to linmod from a Matlab function, and I get an "undefined variable" error. I used a "load" command prior to calling the linmod function that contained the variable and then used a "whos" to verify that the variable was in the calling space (it was), but I still get the error. In an older version of the code I declared the variable as global which worked, but since I am eventually going to define it as a distributed processing job, I need to change it (global variables are not permitted). How would I get the Simulink model to recognize the upstream variables?

Accepted Answer

Guy Rouleau
Guy Rouleau on 12 Apr 2011
I recommend looking at these 2 solutions for 2 different options:
The first one will give you an example script to transfer your variables from your function to the model workspace, as suggested by Arnaud.
The other one uses the old SIM syntax (still supported but no more documented) to set the workspace used by Simulink.
the reason why the SIM command syntax has been modified in R2009b is especially to make it compatible with the parallel computing toolbox. This is why I recommend this approach.
Note that there are many ways to define the variables in the model workspace. This doc link explains other good options:
  1 Comment
Jarrod Rivituso
Jarrod Rivituso on 12 Apr 2011
Note that the last doc link Guy provided has a section about setting the data source of the Model Workspace to a MAT-file, which sounds very much like what you had asked Arnaud.

Sign in to comment.

More Answers (1)

Arnaud Miege
Arnaud Miege on 11 Apr 2011
linmod uses data that is in the base workspace, not in the function workspace. For a workaround, see: http://www.mathworks.com/support/solutions/en/data/1-15MSCA/?solution=1-15MSCA
I am not sure however, if that would work as part of a distributed processing job.
HTH,
Arnaud
  3 Comments
QinetiQ378 Scanlon
QinetiQ378 Scanlon on 11 Apr 2011
Can I load in a .mat file from the .mdl file? I am using a pre-built .mdl (created by someone else) and I don't want to change it too much. I note that the command structure is a little different from a .m file - can I use commands like 'whos' in the .mdl file to verify that the data has been successfully loaded in?
Arnaud Miege
Arnaud Miege on 11 Apr 2011
What I was suggesting was to load the MAT file in the MATLAB workspace, then using Model Explorer, copy the data from the base workspace into the model workspace (select all, CTRL+C and CTRL+V), before saving the model.
An alternative might be to insert the code that loads the MAT file as a model callback (InitFcn or PreLoadFcn) and making sure that the MAT file is included in the file dependencies when distributing the jobs to the workers.

Sign in to comment.

Categories

Find more on Interactive Model Editing 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!