MATLAB: Setting Simulink parameter values from script

blockguiMATLABparametersimulink

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:
sim('pitch_bounce_model',modelparams);
This didn't work either.
Then I tried:
set_param('pitch_bounce_model','t_step',t_step);
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,
Simon.

Best Answer

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.
Related Question