# 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

조회 수: 47 (최근 30일)
Robin Häsler . 2018년 5월 3일
댓글: Stephen23 . 2018년 5월 4일
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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by