Lynx
Lynx

Reputation: 175

How can I retrieve variables from parfor on MATLAB?

I am trying to retrieve a variable I executed on a parfor (parallel for) on MATLAB but I can't. Heres my code:

clear all
clc    
matlabpool open 4

parfor i = 1:4
    a = 2^i;
end

matlabpool close

I would like to be able to recover the different values that were executed on the different workers. For example, I would like to access a(1) with a value of 2^1, a(2) with a value of 2^2, and so on. Thank you very much!

Upvotes: 2

Views: 219

Answers (1)

Sameh K. Mohamed
Sameh K. Mohamed

Reputation: 2333

You can't have multiple values when you are overwriting a single variable a , Try instead:

clear; clc;
matlabpool open 4

a = zeros(4,1); 
parfor i = 1:4
    a(i) = 2^i;
end

matlabpool close 

and there are good examples of how to use parfor in matlab documentation of it here.

Upvotes: 5

Related Questions