Import .txt data into CELL ARRAY

2 ビュー (過去 30 日間)
Philip
Philip 2018 年 9 月 17 日
コメント済み: Philip 2018 年 9 月 25 日
I have text files with arrays of data:
{ Centroid = [[1 0 1],[0 1 0]] Ages= [10 20 30],Times = [ 3:00, 3:05] }
->Exact example, text file includes enclosed brackets '{ }'
I need to import this data into a (cell array?) in MATLAB, so that I can call the various labels and access their data.
  2 件のコメント
Paolo
Paolo 2018 年 9 月 17 日
What would the desired cell array look like?
Philip
Philip 2018 年 9 月 17 日
I don't have a particular idea, but some type of table in which I could call chunks of data.
'Centroid'
{[0 1 1]}
{[2 1 3]}
Etc: I'm not super familiar with Tables in MATLAB

サインインしてコメントする。

回答 (1 件)

Paolo
Paolo 2018 年 9 月 17 日
Try something like this:
raw = fileread('mytextfile.txt');
[~,tok]=regexp(raw,'([A-Z][a-z]+).*?(\[.*?(?=[ ,][A-Z}]))','match','tokens');
tokens = [tok{:}];
varnames = tokens(1:2:end);
data = tokens(2:2:end);
cell2table(data,'VariableNames',varnames)
1×3 table
Centroid Ages Times
___________________ ____________ _______________
'[[1 0 1],[0 1 0]]' '[10 20 30]' '[ 3:00, 3:05]'
  6 件のコメント
Philip
Philip 2018 年 9 月 20 日
That's intense, thanks, I will have to play with it and lookup documentation to understand each component of this.
Philip
Philip 2018 年 9 月 25 日
Question on output: Can you now call specific parts from the table? How would you do that: example
answer = table('Ages',array[0])
...
(answer = 10)
Would it be better/easier to use textscan? Especially for text files that are just one big array of numbers?
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by