MATLAB Answers


Textscan has an error with some file names and not others?

Asked by ZS
on 11 Aug 2018 at 12:14
Latest activity Commented on by Star Strider
on 13 Aug 2018 at 3:59

I'm running a long and complex program for data analysis that runs perfectly fine with certain numbered data files but not with others.

When it is unhappy, I get the following error: ??? Error using ==> textscan Invalid file identifier. Use fopen to generate a valid file identifier.

The file names are zs_120.dat and zs_149.dat. I see no reason why 120 is a valid file identifier but 149 is not?

Is there some kind of setting I can change, either in Matlab or on my computer, that will enable it to run properly?

I'm currently running windows 10, as is my coworker who can run the program. My computer is a HP Pavilion laptop. Matlab version is 7.11.0(R2010a)

EDIT: More information:

fopen documentation returns ans = -1

The second line of the error message is as follows: Error in ==> NonRephasingC16_120 at 51 MDdata(:,:) = textscan(file,'%s','delimiter', ['whitespace','\n'], 'BufSize', 8000);

here, NonRephasingC16_120 is the name of the program being used.

The files are also both in the same folder.


Sign in to comment.




1 Answer

Answer by Star Strider
on 11 Aug 2018 at 12:29
Edited by Star Strider
on 11 Aug 2018 at 12:49
 Accepted Answer

Check the second output ‘errmsg’ in your fopen (link) call. It will tell you what the problem is. Most likely, it cannot find the file. The reasons could be misspelling, the file is in a different directory, or any number of other possibilities.

[fileID,errmsg] = fopen( ... )


From the documentation:

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

The which function may also not be able to find them. It will return the full path and filename if it can:

result = which('zs_120.dat')
result = which('zs_149.dat')


Show 1 older comment

My pleasure!

The which function outputs indicate that your files do not exist anywhere in your MATLAB search path. You will have to find them somehow and copy them to a directory in your MATLAB search path if you want to use them.

If my Answer helped you solve your problem, please Accept it!

Thank you very much for this addition- that's exactly what I did, and at least that part of the program seems to be working!

As always, my pleasure!

Sign in to comment.