Hi,
I have a text file (test.txt) that is of the following form:
1.1 1.2 1.3X2.1 2.2 2.3X3.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.32.1 2.2 2.33.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 145Mismatch 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.3X2.1 2.2 2.3X3.1 3.2 3.3
or
1.1 1.2 1.3X2.1 2.2 2.3X3.1 3.2 3.3X
and obtain the array
1.1 1.2 1.32.1 2.2 2.33.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.32.1 2.2 2.33.1 3.2 3.3
Do you have any additional suggestions? Thank you for your time.
Best Answer