Read a .txt File into a MATLAB matrix - matlab

I have a .out file (.txt) in the form:
This is a text file
This file was created by Andrew on 4/5/14
Certificate Result Test #12
Time A B C D
50 4 3 8 9
55 4 8 7 4
60 8 4 1 4
65 7 1 5 1
70 4 2 2 2
How do I read the numbers in the table into a matrix, called M, in MATLAB whilst ignoring all the text at the start?
I have tried using fscan and M = dlmread(filename) but I recieve errors saying Mismatch between file and format string due to the lines of text at the start.
Thanks in advance

Use textscan with the 'HeaderLines' option:
fid = fopen('my_file.out'); % or whatever your file is called
M = textscan(fid,'%d %d %d %d %d','HeaderLines',7); % using int32 data types, change as required
fclose(fid)
Note that M is a cell array

textscan is a powerful tool, with good low level functions. There is also the more convenient 'importdata' which works for many files of this kind:
m = importdata('my.txt', ' ', 6)
m =
data: [5x5 double]
textdata: {6x5 cell}
colheaders: {'Time' 'A' 'B' 'C' 'D'}
As you can see, it not only returns the data in m.data, but you also get the column headers for free.

Related

save text file matlab

Another question on fprintf
I have a matrix s(n,5) that I want to shorten (just take columns 3,4 and 5) into s1(n,3) and save with a different name.
s1=s(:,3:5);
txtfilename = [Filename '-1.txt'];
% Open a file for writing
fid = fopen(txtfilename, 'w');
% print values in column order
% two values appear on each row of the file
fprintf(fid, '%f %f %f\n', s1);
fclose(fid);
I don't think I understood the way to use fprintf and rewrite my new matrix, because it is sorting the values.
Thanks for your help
The problem is that MATLAB stores data in column-major order, meaning that when you do s1(:), the first three values are the first three values in the first column not the first row. (This is how fprintf will read values out of s1.) For example:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(:)
ans =
8
3
4
1
5
9
6
7
2
You can simply transpose the matrix to output the way you want:
fprintf(fid, '%f %f %f\n', s1.');

Use textscan in Matlab to output data

I've got a large text file with some headers and numerical data. I want to ignore the header lines and specifically output the data in columns 2 and 4.
Example data
[headers]
line1
line2
line3
[data]
1 2 3 4
5 6 7 8
9 10 11 12
I've tried using the following code:
FID = fopen('datafile.dat');
data = textscan(FID,'%f',4,'delimiter',' ','headerLines',4);
fclose(FID);
I only get an output of 0x1 cell
Try this:
FID = fopen('datafile.dat');
data = textscan(FID,'%f %f %f %f', 'headerLines', 6);
fclose(FID);
data will be a 1x4 cell array. Each cell will contain a 3x1 array of double values, which are the values in each column of your data.
You can access the 2nd and 4th columns of your data by executing data{2} and data{4}.
With your original code, the main issue is that the data file has 6 header lines but you've specified that there are only 4.
Additionally, though, you'll run into problems with the specification of the number of times to match the formatSpec. Take for instance the following code
data = textscan(FID,'%f',4);
which specifies that you will attempt to match a floating-point value 4 times. Keep in mind that after matching 4 values, textscan will stop. So for the sake of simplicity, let's imagine that your data file only contained the data (i.e. no header lines), then you would get the following results when executing that code, multiple times:
>> FID = fopen('datafile_noheaders.dat');
>> data_line1 = textscan(FID,'%f', 4)
data_line1 =
[4x1 double]
>> data_line1{1}'
ans =
1 2 3 4
>> data_line2 = textscan(FID,'%f', 4)
data_line2 =
[4x1 double]
>> data_line2{1}'
ans =
5 6 7 8
>> data_line3 = textscan(FID,'%f', 4)
data_line3 =
[4x1 double]
>> data_line3{1}'
ans =
9 10 11 12
>> data_line4 = textscan(FID,'%f', 4)
data_line4 =
[0x1 double]
>> fclose(FID);
Notice that textscan picks up where it "left off" each time it is called. In this case, the first three times that textscan is called it returns one row from your data file (in the form of a cell containing a 4x1 column of data). The fourth call returns an empty cell. For the usecase you described, this format is not particularly helpful.
The example given at the top should return data in a format that is much easier to work with for what you are trying to accomplish. In this case it will match four floating point values in each of your rows of data, and will continue with each line of text until it can no longer match this pattern.

How to read matrix from text file in matlab?

I have a text file in the same folder as my matlab code called matlab.in, its contents are
training_set = [1 2 3; 4 5 6]
How do I read this matrix into a variable called training_set?
Your text file contains an executable Matlab statement. You could, probably even should, rename it to something like training_set.m (the .m suffix is important) and simply 'read' it from the command line by executing it. On my machine the 'command'
>> training_set
generates the response
training_set =
1 2 3
4 5 6
and, hey presto, the variable training_set is now safely ensconced in your workspace.
Now congratulate yourself for having written your first (?) Matlab script, reward yourself with a visit to the documentation to review this important topic.
First, open it using fopen(...):
fid = fopen('matlab.in');
Second, read the line from the file and close the file again, since you don't need it anymore:
content = fgetl(fid);
fclose(fid);
Third, evaluate the string read from the file:
eval(content);
If you wanted to suppress the output, you might either want to add a semicolon at the end of the text file or instead use:
eval(strcat(content,';'));
file1.txt: 1 2 3 4 5 6 7 8
10 20 30 40 50 60 70 80
[fileID,errmsg] = fopen('file1.txt')
val= textscan(fileID, '%u8')
z = transpose(cell2mat(val))
vec = transpose(reshape(z, [8 2]))
...
Gives you
vec =
2×8 uint8 matrix
1 2 3 4 5 6 7 8
10 20 30 40 50 60 70 80

Read 'm-to-n' row numbers from a txt file (MATLAB)

I'm trying to read data from a .txt file. The sample data is given below. I need an algorithm to read just M-N row numbers. While I can use while/for loops, I'm afraid that it might become very slow. Thanks!
a=[ 1 6 11 16 ;
2 7 12 17 ;
3 8 13 18 ;
4 9 14 19 ;
5 10 15 20] ; % data is in Test.txt -->
% fid = fopen('Test.txt');
% a=a.'; fprintf(fid, '%.3f\t%.3f\t%.3f\t%.3f\r\n', a(:)) ;
fid = fopen('Test.txt') ;
AnsMat = fscanf(fid, '%f %f %f %f')
AnsMat = [2 7 12 17 ; 3 8 13 18] ; % Read row-numbers 2 to 4 this time
You could try textscan which allows a HeaderLines parameter telling matlab how many lines to skip.
For example to read lines n (=2) to m(=4), you could do:
fid = fopen('Test.txt');
C = textscan(fid,'%f %f %f %f\n',m-n+1,'HeaderLines',n-1);
fclose(fid);
This does return the data as a cell array though so you have to convert it:
AnsMat = cell2mat(C);
If your data were in csv format instead of text format, you could use the command:
text=csvread('yourfile.csv',1,1,[1 1 m n])
Obviously, if your data is only available in text format, it would be just as much work to manually convert it as it would be to use the textscan option, but if your text file is being generated elsewhere where you would have control over the output format, this may streamline the process.

Read and parse text file in octave/matlab

I am trying to read in some values from a file in an octave program (I suspect matlab is similar), but not sure how to do it.
I have input file in the form:
x y
A B C
a_11 ... a_1n
a_21 .. a_2n
...
a_m1 ... a_mn
Where x,y are doubles, A, B, C are integers, and a_11...a_mn is a matrix.
I saw examples of how to read in just the matrix, but how can I read mixed stuff like this?
In my opinion this is not a good way of storing data. But octave offers the functionality to read this as well with dlmread:
data = dlmread (file, sep, r0, c0)
data = dlmread (file, sep, range)
If you have this text file test.csv:
1 2
1.1 2.2 3.3 4.4
1 2 3
4 5 6
7 8 9
You can read in your data like this:
integers = dlmread('test.csv', '', [0 0 0 1]);
floats = dlmread('test.csv', '', [1 0 1 3]);
matrix = dlmread('test.csv', '', 2, 0);