How can I read from a text file in the following format?
이전 댓글 표시
I have a text file formatted as follows,I want to read this text file and separate it in three blocks, each one should be a [4*4] matrix, I don't know how I can do that with "textscan"?!!
# TT50/Data/BaseballPitch/v_BaseballPitch_g01_c01
5 0.25 0.228125 0.0654206
5 0.133333 0.0375 0.0747664
5 0.208333 0.55625 0.0747664
5 0.495833 0.221875 0.0747664
#TT50/Data/BaseballPitch/v_BaseballPitch_g01_c02
5 0.591667 0.134375 0.0860215
5 0.320833 0.125 0.0967742
5 0.458333 0.24375 0.0967742
5 0.520833 0.140625 0.0967742
# TT50/Data/BaseballPitch/v_BaseballPitch_g01_c03
5 0.625 0.821875 0.0873786
5 0.6125 0.765625 0.203883
5 0.575 0.78125 0.262136
5 0.6 0.778125 0.271845
I would appreciate if you help me with this problem...
Best,
채택된 답변
추가 답변 (1개)
If block sizes can vary, I would build a solution around:
content = fileread( 'myFile.txt' ) ;
nCols = 4 ;
blocks = regexp( content, '#\s?(\S+)([^#]*)', 'tokens' ) ;
nBlocks = length( blocks ) ;
labels = cell( nBlocks, 1 ) ;
data = cell( nBlocks, 1 ) ;
for k = 1 : nBlocks
labels{k} = blocks{k}{1} ;
data{k} = reshape( sscanf(blocks{k}{2}, '%f'), nCols, [] ).' ;
end
Run this on your data file and observe then cell arrays labels and data. The whole could be made more concise with CELLFUN, but it wouldn't be as clear and maybe not as efficient.
Let me know if you have any questions.
댓글 수: 8
Niloofar Yousefi
2014년 5월 5일
Niloofar Yousefi
2014년 5월 5일
Ok, do all blocks have the same number of columns, or can this number vary? Also, do you need to keep track of the reference (label/header) for each block?
On my laptop, for example, it takes 5s for processing a file with 140,000 rows and 180 columns. It's not that much overall. In any case, you can store a random selection of rows in data{k}.
Niloofar Yousefi
2014년 5월 5일
If you have the statistics toolbox, you can use RANDSAMPLE. Also, you'll need a little more code if some blocks can have fewer than 100 rows.
content = fileread( 'myFile.txt' ) ;
nCols = 172 ;
nSamples = 100 ;
blocks = regexp( content, '#\s?(\S+)([^#]*)', 'tokens' ) ;
nBlocks = length( blocks ) ;
labels = cell( nBlocks, 1 ) ;
data = cell( nBlocks, 1 ) ;
for k = 1 : nBlocks
fprintf( 'Block %d/%d..\n', k, nBlocks ) ; % Can be removed.
labels{k} = blocks{k}{1} ;
temp = reshape( sscanf(blocks{k}{2}, '%f'), nCols, [] ).' ;
rowIds = randsample( size(temp, 1), nSamples ) ;
data{k} = temp(rowIds,:) ;
end
Niloofar Yousefi
2014년 5월 5일
카테고리
도움말 센터 및 File 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!