MATLAB Answers

Setting Simulink parameter values from script

79 ビュー (過去 30 日間)
Simon Aldworth
Simon Aldworth 2018 年 10 月 25 日
コメント済み: Fangjun Jiang 2018 年 10 月 29 日
Until recently I was running a Simulink model from my main script. The Simulink model uses variables in the blocks and would run perfectly when I used statements such as:
t_step = Simulink.Parameter(1);
I am now running the model from within a gui script and I can't get it to run. My first thought was that there were no Simulink parameters in the model so I put them in, thinking that these had to be there for the above statements to work. This didn't work, however.
I then tried putting the variables into a struct using:
modelparams.t_step = Simulink.Parameter(1);
and then:
This didn't work either.
Then I tried:
And received the response, "block_diagram does not have a parameter named 't_step'".
It is evident that I don't know what I'm doing, so:
1. Several blocks use the same variable so do I need to set each block parameter one at a time, or can I create a Simulink parameter from my GUI script?
2. Do I need the Simulink parameter to pre-exist within the model?
3. Previous responses to other questions suggest that relying on the base workspace to store variables is not good practice, but now I read that when running Simulink models this is the norm. What should I do in this case?
4. Why has it stopped running properly just because I moved my code into the gui script?
Many thanks,

  0 件のコメント



Fangjun Jiang
Fangjun Jiang 2018 年 10 月 25 日
4. Why has it stopped running properly just because I moved my code into the gui script?
When you run "t_step = Simulink.Parameter(1);" in your GUI script, it created the variable "t_step" in your GUI callback function workspace, not the base workspace.'
There are many ways to solve your problem. The most direct way is to use assignin() function.

  4 件のコメント

表示 1 件の古いコメント
Fangjun Jiang
Fangjun Jiang 2018 年 10 月 26 日
I can think of making your model use Simulink Data Dictionary or model workspace. Then your GUI script can change your desired parameters directly through proper API.
web(fullfile(docroot, 'simulink/ug/store-data-in-dictionary-programmatically.html'))
web(fullfile(docroot, 'simulink/ug/change-model-workspace-data.html'))
Simon Aldworth
Simon Aldworth 2018 年 10 月 29 日
Hi Fangjun,
Thanks for your response.
I have chosen to use a data dictionary for now. It remains to be seen whether it was a good choice though.
As far as the idea of modifying variable values directly in the model workspace is concerned, it would seem to be a fairly straight forward task and is tempting. However, it uses assignin, and this seems to be a function that is strongly opposed, despite being proposed in the Matlab documentation.
I'm not sure if there are times when assignin is acceptable but there is so much written in criticism of its use that I feel I should avoid it.
Regards, Simon.
Fangjun Jiang
Fangjun Jiang 2018 年 10 月 29 日
The function assignin() exists for a reason. For occasional use, I would not rule it out. It brings convenience and wouldn't slow down process in most of the cases. In any case, Simulink data dictionary is a better way to go. It separates the data from the software and makes them both become more manageable.


その他の回答 (1 件)

Sara Nadeau
Sara Nadeau 2018 年 10 月 26 日
Hi Simon!
Have you considered using Dashboard blocks to interface with your Simulink model? They offer similar functionalities to a MATLAB GUI with easier connectivity to Simulink models.
I am not sure what your GUI and model look like or what version of MATLAB/Simulink you're working with. Apologies if this suggestion isn't helpful.

  1 件のコメント

Simon Aldworth
Simon Aldworth 2018 年 10 月 29 日
Hi Sara,
I hadn't, but maybe I will in the future. I'm a long way down the line with my GUI now so I'll stick with it.
Thanks anyway.





Community Treasure Hunt

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

Start Hunting!

Translated by