I have a 1x50 cell array with a 780x6 table in each cell. I can access the cells but i would like to convert the different tables into matrices and give them a distinctive name but the loop gives me an error every time

3 views (last 30 days)
Simulations =
1×50 cell array
%now I tried to build a loop which converts every cell into a matrix and names the matrices
for i = 1:50
a(i) = table2array(Simulations{1,i})
end
%this gives me the following error-> Unable to perform assignment because the left and right sides have a different number of elements.
  1 Comment
Stephen23
Stephen23 on 4 May 2018
"i would like to convert the different tables into matrices and give them a distinctive name"
Do NOT do that. Dynamically creating/accessing variable names is how beginners force themselves into writing slow, complex, buggy code that is hard to debug. Just use a cell array with indexing. Indexing is neat, easy to debug, and very efficient. Read this to know more:

Sign in to comment.

Answers (1)

sloppydisk
sloppydisk on 3 May 2018
You cannot store matrices inside matrices, you can however leave them inside the cell and convert them there using table2array:
% construct tables in cell
a = cell(1, 50);
b = rand(780, 6);
a(:) = {table(b(:, 1), b(:, 2), b(:, 3), b(:, 4), b(:, 5), b(:, 6))};
class(a{1})
% convert data to doubles in cell
a(:) = {table2array(a{:})};
class(a{1})
Note that a(1) gives a 1x1 cell, while a{1} gives the contents of that cell.

Categories

Find more on Data Type Conversion 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!