Can I create a seperate workspace for Simulink Callback Variables? (MATLAB 2016b)

3 visualizaciones (últimos 30 días)
In my models, I use a lot of dynamic masking to initialize parameters, which involves lots of long Callback functions. Also, creating/editing code in the Callback Editor seems to be very unpractical when the code is over a dozen lines long. A simple example of what I mean is as follows:
gear = {'1' '2' '3' '4' '5' 'R'};
angles = [0 10 20 25 30 -11];
str = get_param(gcb,'Position_final');
i = strcmp(str, Gear);
set_param(gcb,'Position_final_fine',angles(i));
This reads the value selected from a popup box, and changes the value of a spinbox so that angles between the selected value can also be used. This length code is manageable, but some of my Callback functions are over 400 lines long. As well as this, I will have now saved the parameters gear, angles, str and i in my workspace. Which with longer parts of code, turn into 20-30 parameters will quickly fill up my workspace. I could use clear, to clear the entire workspace (possibly damaging other parts of my model that rely on the workspace), or clear the parameters I have used, but then they would still overwrite parameters I have used (considering I use a lot of common acronyms and letters repeatedly).
One solution would be to create a function, and then have the Callback call the external function, which would increase readability of the code, and allow me to use multiple functions for similar Callbacks. However, then I will need to carry around many extra functions with the model, which I would rather avoid.
Is it possible to either,
1) create a separate workspace for the Callback function, in which it is not connected to the global workspace.
or
2) create a function that the Callback can access, and embed this in the Simulink .slx file, so that I do not need to carry round extra functions with the program.
Best regard,
Mathew

Respuesta aceptada

Mathew Thorne
Mathew Thorne el 28 de Feb. de 2018
As mentioned by Mark Schwab, the model workspace could be used in this case. Assigning in variables and evaluating code in the model workspace. This will declutter the global workspace and command window, but will however require more code.
Maybe a functionality could be added to future Matlab releases to define in models, in which workspace the Mask Callback and Initialization code will be executed.
Another option would be to use external files, and therefore functions. You can embed multiple functions into a single m file, and therefore would only need one extra file. This maybe also beneficial for testing.

Más respuestas (1)

Mark Schwab
Mark Schwab el 25 de En. de 2018
Based on the description of your workflow, I would recommend using the model workspace. Every Simulink model has a workspace of its own. For this workspace, you can think of the model as a function. The variables in this workspace are only visible in the scope of the model. I have included a link to documentation on the model workspace below:
I would recommend exploring this documentation and other documentation on the model workspace to see if it fits your use case.
  1 comentario
Mathew Thorne
Mathew Thorne el 20 de Feb. de 2018
Hey Mark,
Thanks for the reply. I have looked into the model workspace before. But as I understand it, I am only able to use commands like assignin or evalin to manipulate the modelworkspace. Ie, I could say
mymodelworkspace.assignin('x', 2);
or
mymodelworkspace.evalin(mymodelworkspace,'x = 2');
However, if I had mode complex code, like the example I posted in the original question I would have something like:
hws = get_param(gcs,'modelworkspace');
hws.assignin('gear', {'1' '2' '3' '4' '5' 'R'});
hws.assignin('angles', [0 10 20 25 30 -11]);
cmd = 'str = get_param(gcb,''Position_final'');';
evalin(hws,cmd);
cmd = 'i = strcmp(str, Gear);';
evalin(hws,cmd);
cmd = 'set_param(gcb,''Position_final_fine'',angles(i));';
evalin(hws,cmd);
clear cmd
This would keep my workspace fairly clean, just leaving the variable cmd to be cleared. But still, if there was a previous variable named cmd in my workspace this would be annoyingly wiped.
Also for this relatively simple code, it has almost doubled this length. And for the more complex code that I am using in my callback functions for the Simulink block masks, with about 500 lines of code, and 50+ variables, this would not really be feasible.
Is there away to maybe set the current workspace of a callback function to the model workspace. So that all following code would be executed there. For example:
set_param(gcs,'CurrentWorkspace','MyModelWorkspace');
Or is it only possible to execute these commands in the base workspace?

Iniciar sesión para comentar.

Categorías

Más información sobre Model, Block, and Port Callbacks en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by