Loading Text File with Multiple sections of headers
显示 更早的评论
Hi All, I am trying to using the "importdata" command to import to Matlab 2012a from a file that has multiple sections of headers. The file looks like:
header header header
1 1 1
1 1 1
header header header
2 2 2
2 2 2
However, import data will only return the data after the first line of headers (in this example a 2x3 matrix of 1's). How can I get it to also read the data after the second set of headers (so a 4x3 matrix for this example of 1's then 2's). Thank you for the help.
Brendan
2 个评论
Walter Roberson
2012-6-8
Do you know the exact amount of data in each section ahead of time?
Do you want the data broken up into parts, or do you want all the data together?
Does the header always start with the same string?
Brendan
2012-6-8
采纳的回答
更多回答(1 个)
per isakson
2012-6-8
importdata cannot handle your file.
An approach is to
- read the file as characters to a string, buf
- split the string, buf, into one string of characters per block of header&data
- parse each block with textscan
Something like
function M = Answer( )
fid = fopen( 'cssm.txt', 'r' );
buf = fread( fid, '*char' );
sts = fclose( fid );
while buf > 0
ix2 = first position of next header
str = buf( 1 : ix2-1 );
buf( 1 : ix2-1 ) = [];
cac = textscan( str, format );
peel off the braces
end
end
or write the blocks to separate files and read with importdata.
类别
在 帮助中心 和 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!