Variable scope in Simulink .mdl files
7 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Guy Rouleau
2011-4-12
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 个评论
Jarrod Rivituso
2011-4-12
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.
更多回答(1 个)
Arnaud Miege
2011-4-11
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 个评论
Arnaud Miege
2011-4-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programmatic Model Editing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!