Basic Slider Switch question

1 view (last 30 days)
I was curious if it was possible to make a slider switch dashboard element control more than one block simultaneously. Alternately, can I use a switch to somehow set a global parameter that the whole sim can access?

Accepted Answer

Benjamin Thompson
Benjamin Thompson on 8 Feb 2022
Looks like you can only connect the Slider Switch to one block. But if that was a Constant block connected to a Data Store Write, then you could put Data Store Reads throughout your model and in that way redistribute your parameter change to more points.
Benjamin Thompson
Benjamin Thompson on 8 Feb 2022
You can also use set_param to change parameters on blocks. If you go to Model Properties you can add M-code to be called at certain times, like during model init or simulation start. Perhaps there, you could read your data value from the workspace or something like that, then use it to set block parameters in different places in the model. This would not change when you move your slides as you originally wanted.
If you can put all blocks that depend on the slider in a masked subsystem, then your slider could just change a parameter in the mask. Then through the parameter promotion mechanism you can assign block parameters within the mask to one parameter that is set by the user in the mask.

Sign in to comment.

More Answers (2)

Matthew Mishrikey
Matthew Mishrikey on 9 Feb 2022
I think I'm getting closer to an answer here.
I setup slider switch to control a constant block, which feeds a data store write, which writes to a data store memory.
A data store read then writes this value to the base workspace.
To access this value in a matlab function, I can do it two ways -
first, I can use a data store read to feed an input to a matlab function (rmathfunc in image).
second, I can declare the data store variable as global inside a matlab function (tmathfunc) and access it without an input to the function.
In the simscape LOAD element, to access R_ohms, I tried to set the resistance to resval. I have to run the sim two times though after changing the slider before the updated value makes it to the simscape component. That's not so nice.
Sim attached.

Matthew Mishrikey
Matthew Mishrikey on 9 Feb 2022
Edited: Matthew Mishrikey on 9 Feb 2022
I think I figured out a way. I hope there's a better solution because this seems like a royal pain.
1) in the simscape component, set the parameter to a base workspace variable.
2) slider switch controls a constant block.
3) constant block sets a data store write, and also a to_workspace block.
4) to control the simscape component with the slider switch, you need to create the workspace variable before the block loads. Do this with model level Callbacks. In this case I do
resval = str2num(get_param(strcat(bdroot,'/Constant'),"Value"));
in PostLoadFcn, InitFcn, and also StartFcn.
5) Matlab Function blocks can access the data store memory (either by a data store read, or by Edit Data | add global variable - must declare data store memory as global in function).
I played around with ways to have the matlab function blocks access the workspace variable directly, to elminate the data store memory middleman, but didn't have any luck.
Anyway, thanks for your comments Benjamin, helped me get across the finish line here.


Find more on Simulink Environment Customization in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!