How to pass a parameter structure from m-file to Simulink?

66 ビュー (過去 30 日間)
Marius
Marius 2014 年 4 月 15 日
コメント済み: Felipe Padua 2022 年 7 月 16 日
Hello there,
I set up a model in Simulink I would like to pass parameters to. The parameters are
to be defined in an m-file. Passing single parameters from Matlab to Simulink is no
problem, but I have not yet managed to figure out how to pass parameters collected in
a structure from Matlab to Simulink.
E.g., my m-File to call my Simulink model with would look something like this:
function callMySimulinkModel
ParamsToPassToSimulink.Param1 = 10;
ParamsToPassToSimulink.Param2 = 5;
....
...
.
ParamsToPassToSimulink.ParamsN = 10;
sim('myModel', [0 10.0]);
where to add what command in order to pass the
ParamsToPassToSimulink-Structure to Simulink in this
function/script ???
end
I've searched the internet for some time and found nothing but some advice on how to do it in previous Matlab releases:
Unfortunately, this does not seem applicable anymore. I also tried the shared workspace command - without success so far.
Does anybody know a solution to this?
Thanks in advance! Marius

採用された回答

Marius
Marius 2014 年 4 月 15 日
Hello Ilham - thanks for your reply!
Using a script works as well, but does not (as it seems to me) change the problem.
  2 件のコメント
Ilham Hardy
Ilham Hardy 2014 年 4 月 15 日
please see my comment above.
Marius
Marius 2014 年 6 月 5 日
Thanks for your reply and effort - I missed it until today.
I found a solution myself in the meantime and also found this Matlab page by conincidence that relates to my problem:
Hope this helps others with the same problem.

サインインしてコメントする。

その他の回答 (3 件)

Ilham Hardy
Ilham Hardy 2014 年 4 月 15 日
Do you need to use a function? In my opinion, an m-file script would be sufficient.
To change m-file function to m-file script, just delete the
function callMySimulinkModel (at begin and)
end (at the last line)
  1 件のコメント
Ilham Hardy
Ilham Hardy 2014 年 4 月 15 日
When you use a function, a 'special' workspace is used. (let's say 'function workspace'). This function workspace only available during the execution of the function. It will be 'cleared' when the function is finished.
Simulink looks at (always, as far as i know) the 'normal' workspace (or 'base workspace').
There is a way to 'push' the desired variables from one workspace to another. To push variables from fucntion workspace to the base workspace, use
assignin('base',a,b);
In which:
  • a = name/label of the desired parameter.
  • b = the desired parameter to be pushed to base workspace
So, in your case, it would be something like:
assignin('base','ParamsToPassToSimulink',ParamsToPassToSimulink);

サインインしてコメントする。


Felipe Padua
Felipe Padua 2022 年 7 月 16 日
By default, Simulink looks at the base workspace for searching Matlab variables used as parameters. But you can "force" Simulink to look at the current workspace (the workspace of the function that calls the simulation) by passing the Name-Value pair 'SrcWorkspace' to your 'sim' command, as follows
function callMySimulinkModel
ParamsToPassToSimulink.Param1 = 10;
ParamsToPassToSimulink.Param2 = 5;
....
...
.
ParamsToPassToSimulink.ParamsN = 10;
sim('myModel', [0 10.0], 'SrcWorkspace', 'current');
  2 件のコメント
Paul
Paul 2022 年 7 月 16 日
Does SrcWorkspace still work? I don't think it's documented any more as a valid input to the sim command.
Further discussion here
Felipe Padua
Felipe Padua 2022 年 7 月 16 日
It's present in R2022a documentation.

サインインしてコメントする。


Chiemela Victor Amaechi
Chiemela Victor Amaechi 2020 年 5 月 4 日
編集済み: Chiemela Victor Amaechi 2020 年 5 月 4 日
The answers above are very correct and valid.
In addition to using the Call Param() and function callMySimulinkModel, you can also setup the parameters on Simulink blocks. See this video if it helps on defining parameters in Simulink - MATLAB's subsystem Block
The other method which is shared here involves changing the parameter and pausing/breaking, in which case you can do it as follows:
You can pause the simulation at any instant and go to the particular block parameters and change the values and play the simulation again. But you have to tolerate the pain of pausing the simulation at stipulated times and running again each time you have to change parameters. This is the easiest though. This doesn't use m file.
Otherwise if your parameters are to be changed and you know the parameter values and time intervals beforehand, you can code them in Embedded MATLAB Function block. (Don't forget to include "eml.extrinsic('set_param');" in this block).
Or (the best method) you can create a GUI if you are required to change the parameters continuously without pausing the simulation and when you don't know the time and parameter values beforehand. This is more versatile and less painful. You will have to code the GUI m file.
Hope this helps you or someone here!

カテゴリ

Help Center および File ExchangeInteractive Model Editing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by