Assigning nested tables inside a parfor-loop
Show older comments
I use parfor for paralelisation, and create an table as LOG which contains a nested table.
Runing the following code will result in the following Error:
Subscripted assignment dimension mismatch for table variable 'Var2'.
VarTypes={'double','table'};
LOG=table('Size',[1 2],'VariableTypes',VarTypes);
parfor i=1:1
nested_table=table(2,3);
table_row=table(i,nested_table);
LOG(i,:)=table_row;
end
My suboptimal solution is to fill the table before the parfor loop:
But is there a cleaner way?
%create first entry
i=1;
nested_table=table(1,1);
table_row=table(1,nested_table);
LOG=table_row;
%concentrate table with duplicates of this entry
for i=2:10
LOG=[LOG;table_row];
end
%run in parallel
parfor i=2:10
nested_table=table(5,5);
table_row=table(i,nested_table);
LOG(i,:)=table_row;
end
Accepted Answer
More Answers (1)
Walter Roberson
on 28 Jun 2021
The problem is that
LOG=table('Size',[1 2],'VariableTypes',VarTypes);
is creating LOG.Var2 as a 1 x 0 table, but you are trying to assign in a 1 x 2 table.
If you assign a 1 x 2 table to LOG.Var2 before-hand, then the assignments will function properly.
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!