Automatically Assigning base workspace variables
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 24 Set 2013
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!
Walter Roberson
il 24 Set 2013
.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.
Abhijit
il 24 Set 2013
Image Analyst
il 24 Set 2013
OK - I don't have Simulink. I added it to the product list for you so others will know.
Risposta accettata
Più risposte (1)
David Arriola
il 9 Ott 2014
Modificato: David Arriola
il 9 Ott 2014
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
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!