필터 지우기
필터 지우기

Read data from a tricky text file

조회 수: 1 (최근 30일)
fsgeek
fsgeek 2015년 9월 27일
댓글: fsgeek 2015년 9월 28일
Dear all,
I would like to read numeric data from a file which has the following standard format:
1 (15, 16, 266, 251)
2 (16, 17, 267, 266)
3 (17, 18, 268, 267)
4 (18, 19, 269, 268)
5 (19, 20, 270, 269)
6 (20, 21, 271, 270)
7 (21, 22, 272, 271)
8 (22, 23, 273, 272)
9 (23, 24, 274, 273)
The data is tab separated and number of rows arbitrary. I would like to end up with a variable containing the values of the left-most column, and the data in the parentheses should be treated as a single vector per row.
e.g.:
>> data{1}
{[1]}, {[15, 16, 266, 251]}
>> data{2}
{[2]}, {[16, 17, 267, 266]}
As long as I can access the the data as two numeric columns, I'd be happy!
I have tried importdata(), dlmread() and fget methods. They all seem to return each row of data as a cell of characters.
Thanks in advance for the help!
Best regards,
Louis

채택된 답변

per isakson
per isakson 2015년 9월 27일
편집: per isakson 2015년 9월 27일
Assuming "data in the parentheses" all have equal length. Why cell array? Isn't this good enough?
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%f%f%f%f%f', 'Delimiter', {'\t',','} ...
, 'Whitespace',' ()', 'CollectOutput',true );
fclose( fid );
num = cac{:};
>> num
num =
1 15 16 266 251
2 16 17 267 266
3 17 18 268 267
4 18 19 269 268
5 19 20 270 269
6 20 21 271 270
7 21 22 272 271
8 22 23 273 272
9 23 24 274 273
>> num(2,2:end)
ans =
16 17 267 266
where cssm.txt contains the data of your question.
  댓글 수: 1
fsgeek
fsgeek 2015년 9월 28일
No need for cell arrays, that was just a demo. This method works great, thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by