SeeDoubleYou
SeeDoubleYou

Reputation: 1273

Matlab importdata ignores delimiter and nheaderlines with txt files

I have several txt files that look like this:

Experiment ended at 5/8/12 10:00 AM
Number of Tunings 9
0 14.0 82 43.305 
1 34.142857142857146 95 23.432 
2 5.857142857142857 82 31.573 
3 37.42857142857143 83 22.387 
4 5.0 93 14.664 
...etc

I want to load this data such that the first two lines are ignored and that I get columns based on space as a delimiter, so the above would become:

0  14.0                82  43.305 
1  34.142857142857146  95  23.432 
2  5.857142857142857   82  31.573 
3  37.42857142857143   83  22.387 
4  5.0                 93  14.664 

The function importdata(FILENAME, DELIM, NHEADERLINES), claimes to do exactly this. However when I call this function with importfile('pathtofile', ' ', 2) I get one single string containing all character in the file, so like this:

Experiment ended at 5/8/12 10:00 AM Number of Tunings 9 0 14.0 82 43.305 1 34.142857142857146 95 23.432 ...etc

How do I get importdata to parse my data correctly?

Upvotes: 2

Views: 5579

Answers (2)

Deve
Deve

Reputation: 4648

You could also use dlmread like this:

dlmread('filename.txt', ' ', 2, 0)

Upvotes: 1

Peter
Peter

Reputation: 14937

>> help importdata has the following note:

If importdata recognizes the file extension, it calls the MATLAB helper function designed to import the associated file format. Otherwise, importdata interprets the file as a delimited ASCII file.

Maybe the .txt triggers importdata to load the file just as text. But in my version of MATLAB, importdata works fine on your sample data in a file named .txt. What version are you using?

If all else fails, you can use copyfile from inside MATLAB to do your renaming

fname = 'sample.txt';
barename = regexprep(fname, '\.txt$', '');
copyfile(fname, barename);
importdata(barename, ' ', 2);

Upvotes: 0

Related Questions