Convert from cell of a table to an array
조회 수: 6 (최근 30일)
이전 댓글 표시
I am importing data from an excel spreadsheet as follows:
options = readtable('options.xlsx');
Some of the excel cells contain descitions of arrays like "[1,2,3]". The cell from readtable would then be:
{'[1,2,3]'}
I would like to convert this to an array of doubles like
[1,2,3]
How can I go about doing this?
댓글 수: 0
채택된 답변
Sindar
2020년 10월 30일
편집: Sindar
2020년 10월 30일
It gets tricky with different variable types and widths, but hopefully this helps. Worst case, you build up the new table element by element:
% build example like your table
t=table();
t{1,1}={'[1,2,3]'};
t{1,2}={'[4,5,6,7]'}
t =
1×2 table
Var1 Var2
___________ ___________
{'[1,2,3]'} {'[4,5,6,7]'}
% start a new table
t2=table();
% convert the text in the cell to an array:
t2{1,1}=str2num(t{1,1}{1});
t2{1,2}=str2num(t{1,2}{1})
t2 =
1×2 table
Var1 Var2
___________ ________________
1 2 3 4 5 6 7
댓글 수: 2
Sindar
2020년 10월 30일
FYI: if a table has arrays as elements, they need to be the same width down columns. This will error:
t=table()
t{1,1}={'[1,2,3]'}
t{2,1}={'[4,5,6,7]'}
t2=table()
t2{1,1}=str2num(t{1,1}{1})
t2{2,1}=str2num(t{2,1}{1})
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!