Calling multiple outputs from a function in a parfor loop

Hi,
I am trying to call the multiple outputs of different functions in a 'parfor' loop, using multiple workers by 'matlabpool' command. To the best of my knowledge, I've got to use a 'nested Cell Array', as I have copied below.
i) The called function (1)
function [ M1,T1,G1 ] = Tr1(Nul)
syms m1 t1
M1=sym('M1',[5 1]);
T1=sym('T1',[5 5]);
G1=ones(2,2)*300;
for i=1:5
M1(i)=m1^i;
T1(i)=t1*i;
end
end
ii) The called function (2)
function [ M2,T2,G2 ] = Tr2(Nul)
syms m2 t2
M2=sym('M2',[5 1]);
T2=sym('T2',[5 5]);
G2=ones(2,2)*600;
for i=1:5
M2(i)=m2^i;
T2(i)=t2*i;
end
end
iii) And the main program with the error message
matlabpool 2
Funlist={@Tr1, @Tr2};
parfor i=1:2
[Data{i^2:3*i}]=Funlist{i}(0)
end
And the error
Error: The variable Data in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
I even tried to simplify the expressions and then pass them into the nested Cell Array "Data" and again the same error, copied below
>> parfor i=1:2
t=i^2;
j=3*i;
[Data{t,j}]=Funlist{i}(0)
end
Error: The variable Data in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
I am expecting to get Data as a nested cell array Data={[1*3],[1*3]}, where Data{1}=[5*1],[5*5],[2,2]
This is a simple example I am tying to work with to figure out how to handle a much more time consuming and complicated program, where I need to call the Multiple Outputs of 4 different functions into a main program and to save the time, I have to do it in parallel. (matlabpool, parfor). I know that this structure works with "for loops", but not of much help to me. Could you please recommend a solution to handle such issues? (MATLAB Version: 8.0.0.783 (R2012b))
Thanks a lot!

 Accepted Answer

Something like the following perhaps? If Data isn't in the precise shape you want you can reshape() it after the loop.
Funlist={@Tr1, @Tr2}; %sliced variable
Data=cell(1,2); %sliced variable
parfor i=1:2
out={}; %temporary variable
numout=3*i+1-i^2; %temporary variable
[out{1:numout}]=Funlist{i}(0);
Data{i}=out;
end

2 Comments

It's also good practice to plan how you want the parfor loop to classify your variables using the classification rules described at,
and then label them accordingly as I have done (sliced, temporary, reduction, etc...) using code comments.

Sign in to comment.

More Answers (0)

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!