Reading in a text file containing "end-of-record" ("end-of-row") markers
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a text file (test.txt) that is of the following form:
1.1 1.2 1.3
X
2.1 2.2 2.3
X
3.1 3.2 3.3
where X denotes the end of a record (that is, the end of a row). I would like to be able to read this type of text file into Matlab, perhaps using dlmread or something similar. My desired result is:
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3
where this is a two-dimensional matrix called A, such that A(1,1)=1.1, A(2,2)=2.2, A(3,3)=3.3,and so on.
I tried using dlmread "as is":
A=dlmread('test.txt')
but dlmread gives an error message, since, as noted in the dlmread documentation, "dlmread reads numeric data only. The file being read may contain nonnumeric data, but this nonnumeric data cannot be within the range being imported":
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 2, field 1) ==> X\n
Is there a way to do this--specify an "end-of-row" marker--perhaps using textscan?
I tried the following using textscan:
fid=fopen('test.txt');
A=textscan(fid,'%f','EndOfLine','X');
but I get only the first row: I get only a 1-by-1 cell with the first row [1.1 1.2 1.3]. I would like to read in all the rows. (Also, if possible, I would prefer that the result be saved in an numeric array/matrix, not in a cell array.)
If you have time, do you have any suggestions?
Thank you.
Andrew DeYoung
Carnegie Mellon University
ADDENDUM ON AUGUST 8
You are right, I was not specific about what I am looking for. Sorry about this.
Every row (i.e., record) will have the same number of elements. However, the row elements may be on the same line in the text file, and the X (i.e., the row delimiter) may or may not be on separate lines. This means that I would like to be able to read in
1.1 1.2 1.3
X
2.1 2.2 2.3
X
3.1 3.2 3.3
or
1.1 1.2 1.3
X
2.1 2.2 2.3
X
3.1 3.2 3.3
X
and obtain the array
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3
But, I would also like to be able to read in
1.1 1.2 1.3 X 2.1 2.2 2.3 X 3.1 3.2 3.3
or
1.1 1.2 1.3 X 2.1 2.2 2.3 X 3.1 3.2 3.3 X
and obtain the array
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3
Do you have any additional suggestions? Thank you for your time.
0 个评论
采纳的回答
Walter Roberson
2011-8-7
You could specify the X as the CommentStyle for textscan.
It is not clear whether you will [eventually] always have the same number of values per record, and it is not clear whether those values might continue on separate lines, with the X marking the end of the list.
If the values are all on line line then using '%f%f%f' would be more efficient, or repmat('%f',1,15) (for example) to avoid writing all of the %f out.
If the number of values per record are fixed but they might be split over several lines, then ... ummm, use %f and reshape and transpose the data afterwards.
3 个评论
Walter Roberson
2011-8-8
Taking in to account the addendum, I would suggest
A = textscan(fid,'%f%f%f','WhiteSpace',' X', 'CollectOutput',1);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!