How can I parametrize the "To Workspace" block in Simulink?

11 views (last 30 days)
Hi there. I am trying to parametrize the "To Workspace" block in Simulink, but it shows me error. Instead of the default variable name simout , I want to use a name that I can pass from workspace. Ex: eval('HHH') instead of simout , where HHH='some_var' . Then the signal from simulation should be stored in a variable named some_var in the base workspace.
The actual scenario is a bit more complex. I would want to provide variable name through eval('B.Label') , where the field Label of the structure B is initialized in the base workspace as B.Label='some_var' . Otherwise, B could be an internal variable in the mask of the subsystem that contains the "To Workspace" block.
The variable name is never changed during the simulation. Thanks in advance for any helpful suggestions.
These are the inputs I tried instead of the default simout, but they gave error Invalid MATLAB variable name specified in ...
eval('B.Label')
B.Label

Accepted Answer

Christopher Berry
Christopher Berry on 4 Aug 2014
Hi Krishnanand,
Since the variable name does not change during the simulation, you can set the output parameter 'VariableName' of the 'To Workspace' block completely from the base workspace like this:
First, you will need to get the system path for the 'To Workspace' block in question. You can do this interactively by selecting that block and then typing:
>> h = gcb;
or you can do this at the cmd prompt by typing:
>> h = find_system(gcs,'Name','To Workspace')
Where 'To Workspace' is the exact name of the block you want to manipulate. If you have more than one 'To Workspace' block, make sure to pick the right one!
Either way, the system path for the block will not change unless you change it, so you will only need to do this once after you create your model.
Then, before you start your simulation, just execute the following lines:
>> B.Label = 'test_struct_name';
>> set_param(h,'VariableName',B.Label);
Then you shoudld be good to go!
  2 Comments
Krishnanand K.R.
Krishnanand K.R. on 4 Aug 2014
Edited: Krishnanand K.R. on 4 Aug 2014
Thank you Christopher. I had done this a few hours ago. Since my To Workspace is inside a masked subsystem, I had received B as an parameter to the subsystem and had used similar code in the mask editor set_param([gcb '/To Workspace'],'VariableName',B.Label);
This is of course better than nothing. My original intention was to store all " simouts " inside a structure. So, different instances of the same masked subsystem must be intelligent to store their measured signals inside/under a particular structure name. Lets say P(5).Label='D.M_5' (in the initialization of the model (using model properties)). [I did simplify in the original post. P is actually an array of structure. Also, as an example, the input parameter for the 5th instance of the masked subsystem is P(5) . That variable is passed on to an internal variable B. This B structure is used to replace simout using B.Label ]
So, at the end of the simulation, I would have a single structure D with fields M_1 , M_2 , ... so on, each field corresponding to signals from an instance of the subsystem. If you could please look into this, it would be a great help. Currently when I make 10 instances of subsystems, my MATLAB workspace gets filled by 10 variable names [but what I prefer is a one structure with 10 fields]. Thank you.
If I cannot specify a structure name like D.M_5 or D.A.M_5 instead of simout , then I have to find another way. [The design idea I am trying to follow is - For a set of similar subsystems, let there be one array of structures as input ( P ) & let there be one array of structures as output ( D ). I did not find any issue with the input part, but only with output]. Thanks again :)
Edit: If possible, please check my other 2 questions before. I had reached this current question as a compromise actually. I had also tried defining InitFcn() callback of the To Workspace block, but I couldn't read the mask's internal variable B using that.
Krishnanand K.R.
Krishnanand K.R. on 4 Aug 2014
Edited: Krishnanand K.R. on 4 Aug 2014
UPDATE: I could achieve the single common structure by programming the StopFcn() of the To Workspace block (using a similar code as below). Thank you for your kind help :)
Q=char(39);
Code=['D.A=' B.Label '; clear(' Q B.Label Q ');' ];
set_param(Block_Object,'StopFcn',Code);
where Block_Object corresponds to the To Workspace block inside the masked subsystem.
Edit: My other two questions are still worth solving :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!