Import .txt data into CELL ARRAY
이전 댓글 표시
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.
답변 (1개)
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
Paolo
2018년 9월 18일
I am happy to explain but before doing so can you just confirm that the output table is what you are after?
Philip
2018년 9월 18일
Paolo
2018년 9월 18일
The second line of code is a regular expression. It can be used to match pattern in strings. In the regular expression, there are two capturing groups. Those are used to extract the name of the variable and the data itself.
Please note that I am assuming that this pattern is consistent across your entire text file.
Philip
2018년 9월 20일
Philip
2018년 9월 25일
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!