How can I read from a text file in the following format?
Show older comments
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,
Accepted Answer
More Answers (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 Comments
Niloofar Yousefi
on 5 May 2014
Niloofar Yousefi
on 5 May 2014
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
on 5 May 2014
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
on 5 May 2014
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!