Convert from cell of a table to an array

조회 수: 2 (최근 30일)
Cameron Dyson
Cameron Dyson 2020년 10월 30일
댓글: Cameron Dyson 2020년 10월 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?

채택된 답변

Sindar
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
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})
Cameron Dyson
Cameron Dyson 2020년 10월 30일
Thank you, its working for me now!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by