Automatically Assigning base workspace variables

I have almost 250 variables that I need to assign in the base workspace inside a PARFOR loop. Is there an automated way of doing this other than writing assignin('base','var',var) for each variables?

4 Comments

Why do they need to go into the base workspace? That sounds crazy to me. Why don't you just return them from your function to the calling routine, or pass them via a mat file or setappdata() or something? I never send variables from a function's workspace into the base workspace - ever!
.mat file would be relatively slow for large data. setappdata() in theory relies upon graphics being available: it is not clear that the root graphics object exists on a node without graphics, such as would be the case for parfor. And then there is the fact that it is parfor. There was a case about 2 weeks ago in which variables had to be defined via assignin('base') in order to make them available to Simulink for the one simulation per worker case.
Well, I am trying to do parallel simulation and one of the matlab webinars say that variables have to be assigned for each simulink model's variable into the base workspace for parallel simulation.
As Walter had mentioned variables had to be defined via assignin in order to make them available.
OK - I don't have Simulink. I added it to the product list for you so others will know.

Sign in to comment.

 Accepted Answer

Avoid doing that. For example create only a single struct and assignin() the struct.

6 Comments

I am not sure if I follow: How would I replace the following 4 assignin commands for a, b , c and d variables as per your suggestion. This is a simple case.
In my case The variable gets generated depending upon the configuration I choose. Either way , I have bunch of variables that I need to assign inside PARFOR. Thanks a lot
a = -1;
b = 1;
c = 1;
d = 0;
KIdx = 0:0.1:1;
iterations = length(KIdx);
simout(iterations) = Simulink.SimulationOutput;
parfor idx = 1:iterations
load_system(mdl);
assignin('base','K',KIdx(idx));
assignin('base','a',a);
assignin('base','b',b);
assignin('base','c',c);
assignin('base','d',d);
try
simout(idx) = sim(mdl,'SimulationMode','normal');
catch exception
error_var{idx} = lasterror
stack_var{idx} = dbstack
exception_var{idx} = getReport(exception,'extended')
end
end
Assigning variables from inside a parfor loop is not useful. They are assign in each thread and would overwrite each other.
So , what are my options for assigning a,b,c,d values above? I thought you have to do assignin or evalin inside a parfor loop
To avoid overwriting should I create a temp directory and load the model and assign the variable in each directory?
Jan, The technique was approved by a Mathworks person a couple of weeks ago.
Hi Walter, Thanks for your answer. Structure would work for the simple exam I showed.
In my actual problem, the variables are getting generated behind the scenes. So, are there any other way for doing this? Can I leverage WHO in some way to do this.
I think I have figured out a way.
a = -1;
b = 1;
c = 1;
d = 0;
W = who;
for i1 = 1:length(W)
Wval{i1} = eval(W{i1});
end
KIdx = 0:0.1:1;
iterations = length(KIdx);
simout(iterations) = Simulink.SimulationOutput;
parfor idx = 1:iterations
load_system(mdl);
for i1 = 1:length(W)
assignin('base',W{i1},Wval{i1});
end
assignin('base','K',KIdx(idx));
try
simout(idx) = sim(mdl,'SimulationMode','normal');
catch exception
error_var{idx} = lasterror
stack_var{idx} = dbstack
exception_var{idx} = getReport(exception,'extended')
end
end

Sign in to comment.

More Answers (1)

I have a similar problem to the one described above. I tried the solution proposed by Abhijit but I always get the following error: - Can somebody help me to get this right?
"It is illegal to use a Composite within the body of a parfor loop
Caused by: Error using distcomp.remoteparfor (line 72) Composite objects cannot be used within a parfor loop."
My code can be seen below:
list = who;
N = length(list);
for j = 1:N
w{j} = eval(list{j});
end
spmd
% Setup tempdir and cd into it
addpath(pwd);
currDir = pwd;
addpath(currDir);
tmpDir = tempname;
mkdir(tmpDir);
cd(tmpDir);
end
%Set parameter values with reduced comb. matrix and simulate all combs.
parfor m = 1:1:num_comb_red
load_system(uParam.sysInfo.modelName); % Load the model parameters on the workers
for j = 1:N
assignin('base',list{j}, w{j});
end
for n = 1:1:num_input_param
num_value_red = cell2mat(comb_red(n,m));
path = cell2mat(uParam.uncertainties.all{n,1});
para_name = uParam.uncertainties.all{n,2};
para_value = uParam.uncertainties.all{n,3}(num_value_red);
set_param(path,para_name,num2str(para_value));
end
%Simulate model for all uncertainty combinations and all temperatures
disp('Simulating linear model...')
simOut(i,m) = sim(uParam.sysInfo.modelName,'SimulationMode', uParam.simInfo.simMode, 'StopTime', uParam.simInfo.simTime);
disp('finished')
end

Categories

Find more on Loops and Conditional Statements 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!