Hundurinn Nero
Hundurinn Nero

Reputation: 83

Extracting the numbers in the 5th column only from a text file

I got a .txt file which has 82332 rows and 5 columns. I only want to extract the numbers from the 5th column to a vector. Is this possible in Matlab?

The text file looks like this.
1.0000000e+00 6.0205125e+01 -1.1923175e+02 3.5341908e+02 3.7197225e-01

Any hints or tips appreciated.

Upvotes: 1

Views: 48

Answers (1)

chappjc
chappjc

Reputation: 30579

With textscan you can specify to ignore fields (columns) with the * modifier to the format specification.

fid = fopen('test_data.txt','r');
C = textscan(fid,'%*f%*f%*f%*f%f');
fclose(fid)

Your vector is C{1}. NOTE: If there are trailing columns to ignore, you can just skip the remainder of the line with %*[^\n].


You can also use dlmread with the range input argument if you know the number of rows in the file:

col = 5; numRows = 24;
dlmread('test_data.txt','',[1 col numRows col] - 1)

Upvotes: 4

Related Questions