Parsing a text file by character
3 次查看(过去 30 天)
显示 更早的评论
I would like to parse a 331 string of characters into cells but the number of characters per cell is not the same for all?
For example, if I have text like the following
24359_435934009____________90909
where _=space
And I want to put it into a matrix such that
characters 1-5 go into column 1 (a.k.a 24359)
character 6 goes into column 2 (ak.a. NaN)
characters 7-10 goes into column 3
etc
The data comes as a .txt file.
回答(2 个)
Alberto
2014-4-8
If there are several lines, you can extract them using this:
fid=fopen(fileName)
B = textscan(fid, '%f %f %f', 'Delimiter'-' ', 'MultipleDelimsAsOne', 1);
fclose(fid)
It extracts three columns, if you need more columns change the part '%f %f %f'. But this only works if there are 3 numbres in each row.
If you have diferent numbers in each row, you should parse using regexp for each line.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!