Florian Hanak-Hammerl
Florian Hanak-Hammerl

Reputation: 13

Matlab fopen error with csv file

I'm just trying to import a csv file into Matlab using the textscan function. But everytime i run the program it always throws back this error

Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.

But as you can see in the code below i'm using fopen to prepare the file for the use of textscan.

S = 'Proto2.csv';
fidi = fopen(S);
C = textscan(fidi, '%f%s%f%f%f%f%f%f%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Delimiter','\n', 'HeaderLines',11, 'CollectOutput',1);

Afterwards i'm using C to get access to data i need out of the csv file

Upvotes: 1

Views: 555

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

Normally, before proceeding with reading and/or writing operations on a file handle, you must make sure that the handle returned by the fopen function is valid. From the official documentation of Matlab:

fileID = fopen(filename) opens the file, filename, for binary read access, and returns an integer file identifier equal to or greater than 3. MATLAB® reserves file identifiers 0, 1, and 2 for standard input, standard output (the screen), and standard error, respectively.

If fopen cannot open the file, then fileID is -1.

Check your fidi variable value before proceeding with textscan, I'm pretty sure it is equal to -1. This happens either because the file is not found (if you don't specify a full absolute path, Matlab searches for it within the current working directory) or because the file has a sharing lock on it (hence, make sure that it's not being used by other applications while you attempt to read it).

Upvotes: 0

Related Questions