Unique workspace for each instance of Interpreted MATLAB Function block?

9 views (last 30 days)
I have a Simulink model with several Interpreted MATLAB Function blocks that all call the same function. Within that function, I load a structure variable from the base workspace that is used in calculating the outputs. Each instance of the block within my model needs to load a different structure variable. I assume there is some overhead in loading these variables from the base workspace every time the function is called, so I'd prefer to just load them once when the simulation is initialized and save them within the function workspace as persistent variables. However, that doesn't seem possible because the function workspace is common among all the blocks that call this function. Is there any way to have each instace of the block call the same function but with a different workspace? Or are there other efficient ways to load base workspace variables into the Interpreted MATLAB Function block? Thanks!
  4 Comments
Herschel Pangborn
Herschel Pangborn on 18 Jan 2022
Block parameters shown below (obj_string is a parameter defined in the mask of a subsystem where this Interpreted MATLAB Function block is located).
Function code as follows:
function out = fcn_name(in,obj_string)
obj = evalin('base',obj_string);
%etc.

Sign in to comment.

Accepted Answer

Paul
Paul on 18 Jan 2022
I think the way that you can do this with minimal changes to your overall approach would be as follows. If you're willing to change your fundamental approach, other (i.e., better) solutions may be available.
Anyway, in what follows I'm going to assume that the mask parameter and function argument obj_string can take on values that are strings that are the names of the Matlab variables in the base workspace. These variable names must be known a priori. In other words, I'm going to assume that the base workspace contains variables named objvar1, objvar2, objvar3, and that obj_string can take on values "objvar1", "objvar2", and "objvar3". Then the function looks like this:
function out = fcn_name(in,obj_string)
persistent objvar1 objvar2 objvar3
if isempty(objvar1)
objvar1 = evalin('base','objvar1');
end
if isempty(objvar2)
objvar2 = evalin('base','objvar2');
end
if isempty(objvar3)
objvar3 = evalin('base','objvar3');
end
obj = eval(obj_string);
% etc
end
Note that the persistent variables objvar1, etc. will remain in the function workspace after the simulation completes, even if you call it from the command line. So you might want to clear the function (and its persistent variables) after the sim completes or before the sim starts, either by hand or via a simulation callback function, so that every time the sim runs it picks up the current values of the base workspace objects.
Caveat: I didn't actually try this, but I don't see a reason it won't work.
Again, there are probably better ways to do this. For example, if all the objects can be stored in an array, then why not do that and access the array with an index that's passed into fcn_name (as opposed to passing in obj_string). That way you'd only have a single persistent variable to deal with in the function. Or you can store all the objects as fields in a single structure with field names being "objvar1" etc. and then access the structure using dynamic field names
mystruct.(obj_string)
which again has the advantage of only having to make mystruct persistent. There are probably other options as well.

More Answers (1)

Walter Roberson
Walter Roberson on 18 Jan 2022
"In this example, the top model uses a signal object in the MATLAB® workspace to define the error data store. This is necessary because data stores are visible across model boundaries only if they are defined by signal objects in the MATLAB workspace or a data dictionary. The model specifies code for the PreLoadFcn model callback parameter that creates the signal object. This code executes before the model loads."
So you can create data stores that self-initialize at load time, and you can route them as signals to become an additional parameter nto the function block.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by