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

7 ビュー (過去 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
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
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.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by