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 Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by