Question

Variations of this question have already been asked several times, for example here. However, I can't seem to get this to work for my data.

I have a text file with 3 columns. First and third columns are floating point numbers. Middle column is strings. I'm only interested in getting the first column really.

Here's what I tried:

filename=fopen('heartbeatn1nn.txt');
A = textscan(filename,'%f','HeaderLines',0);
fclose(filename);

When I do this A comes out as just a single number--the first element in the column. How do I get the whole column? I've also tried this with the '.tsv' file extension, same result.

Also tried:

filename=fopen('heartbeatn1nn.txt');
formatSpec='%f';sizeA=[1 Inf];
A = fscanf(filename,formatSpec,sizeA);
fclose(filename);

with same result.

Could the file size be a problem? Not sure how many rows but probably quite a few since file size is 1.7M.

Was it helpful?

Solution

Assuming the columns in your text file are separated by single whitespace characters your format specification should look like this:

A = textscan(filename,'%f %s %f');

A now contains the complete file content. To obtain the first column:

first_col = A{:,1};

Alternatively, you can tell textscan to skip the unneeded fields with the * option:

first_col = cell2mat( textscan(filename, '%f %*s %*f') );

This returns only the first column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top