I am using Matlab to work on my project, and I want to use "parfor" as a part of my code that returns an array of structures in each its iteration, But when I run my code I got some errors. I just tried to bring an example of my problem in the simplest way. Will be appreciated any helps! Here is the example:
clear;
clc;
% An arbitrary number
constant_Number=3;
tic
parfor i=1:2
k=[constant_Number,i];
r(i)=test(k);
end
toc
and test function is as follows:
function [a]=test(k)
a.first=k(1) * k(2);
a.second=k(1)+k(2);
b.first=k(1)/k(2);
b.second=k(1);
a=[a;b];
end
One note is that when I just return one structure, it works fine but when I have more than one (adding b for instance), I got the following error: "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Thanks, Ali
Find the solution, just need to define a cell array and then finally merge them! Follow can be an answer:
clear;
clc;
% An arbitrary number
constant_Number=3;
% Define an arbitrary array of structure
arb_arr=[];
% Define an arbitrary cell array
r={};
tic
parfor i=1:2
k=[constant_Number,i];
r{i}=test(k);
end
for i=1:2
arb_arr=[arb_arr;r{i}];
end
toc