najusten
najusten

Reputation: 59

Reading list of values from text file into MATLAB

I have a text file containing two column data, separated by a comma. However, the first 10 lines contain information which I do not need.

The input text file looks as follows:

# PROGRAM NAME                                      
# The first 10 lines are info I don't need        
#                                           
#                                                                                 
#  
#

892
5
564
1, 0.4377E-014
2, 0.0673E+000
...

I am trying to write a code which reads the value pairs starting on Line 11 into a 2 column matrix.

My (failed) attempt thus far is as follows:

fin = fopen(fullfile(cd, file_name), 'r');
tLine = fgets(fin);
while ischar(tLine)
    crit_list = [crit_list; tLine(:)];
end

My intention was to delete the first 10 lines of the matrix after the code had executed, and then use str2num on the value pairs, but I'm not sure this would be very efficient.

How can I read this file into MATLAB, starting from the 11th line?

Upvotes: 0

Views: 72

Answers (1)

Adriaan
Adriaan

Reputation: 18187

importdata has the ability to skip header lines:

importdata(file_name,delimiter,10);  % skip 10 header lines

where you have to specify your delimeter, judging the file you'll want delimiter = ',', i.e. a comma.

Upvotes: 2

Related Questions