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
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2018-5-4
"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:
回答(1 个)
sloppydisk
2018-5-3
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!