Error while using fopen saying 'Inavlid file identifier' [duplicate] - matlab

I have a MATLAB script that I could have sworn worked fine the last time I used it (a year ago). Now, I get this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
If I understand correctly, it is failing to find or open(?) a file specified elsewhere in the script. Is this right? If so, what could cause it?

fid (file identifier) is the output of fopen. It's an integer, but not related to the file permanently. You need to use fopen to get the fid. It seems to me that you are using incorrect fid (file identifier) in some file-related I/O command, such as fread, fscanf or fclose. Unsuccessful fopen gives fid of -1. For any valid normal file successful fopen will give fid that is 3 or greater integer.
However, without any code it's impossible to say where or what the bug or error is. You could use MATLAB debugger to single-step the code from relevant fopen (set breakpoint there and run your program) until the relevant fclose and see if fid (or whatever variable name you use for file identifier) or any data structure for your file identifiers (if have more than one file identifier in your code) changes in any point between relevant fopen and fclose.

I solved this problem for my self by adding permission option to fopen.
As you see in http://www.mathworks.se/help/matlab/ref/fopen.html , fopen syntax is:
fileID = fopen(filename,permission)
Possible permissions, for example are:
'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | ...
'r' – Open file for reading.
'w' – Open or create new file for writing. Discard existing contents, if any.
'a' –
Open or create new file for writing. Append data to the end of the file.
'r+' – Open file for reading and writing.
'w+' – Open or create new file for reading and writing. Discard existing contents, if any.
'a+' – Open or create new file for reading and writing. Append data to the end of the file.
...
If I use fopen without permission option, or if I use 'r' (default) option, fopen will return -1, which is error. I success with this:
fid=fopen('tmp.txt', 'w');
fid=fopen('tmp.txt', 'a');

I had this problem. It turned out that the file I was trying to write was too large (I didn't have enough free space to accommodate it). However, the program didn't fail until the call to fclose. Very confusing!
Try freeing up some space, or writing a very small file, to test this diagnosis.

I encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.

fopen can fail because MATLAB doesn't have the permissions to read/write the file you've specified.
Try opening a file in a location where you/MATLAB have all the rights (depending on your OS).

I have used fopen with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.

I had the file opened in excel and as a result fopen returned a -1.
Took me forever to find such a trivial problem.

It also happens when trying to create a file in a non-existent directory. Try mkdir('folderName') within MATLAB or just create the directory beforehand.

The path with a forward slash at the beginning can cause the same error.
filename = '/data/myfile.txt';
throws this error, while
filename = 'data/myfile.txt';
does not produce an error.

For my situation, I have checked everything, but missed an easy step.
Please select "browse your folder" and browse for your current document location before you run your 'fopen' code.

It also occurs when a script is trying to read beyond the end of the file.

Related

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
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).

fprintf after opening file using fopen does not work in matlab

May be my tired eyes are ignoring something trivial. But I am not able to get the following code work in matlab. I need to open an existing file and write text to it.
filename='E:\\data_bak\\test.txt';
fileId=fopen(filename,'r+');
if(fileId~=-1)
myline=fgetl(fileId);
nbytes=fprintf(fileId,'%s\n','testdata')
fclose(fileId);
end
I am using matlab 7.9.0 on windows 7. I have tried rt+ for permissions in fopen. Also, fileId is not equal to zero as I am able to read the line in the variable myline. In addition, fprintf('%s\n','testdata') successfully prints testdata on the matlab prompt.
The value assigned to nbytes is 9. So it does appear it has written to file but when I open the file in text editor, I am not able to find text 'testdata'.

Matlab: error using fprintf

This is a script written by someone else, and I don't understand why it is giving me this error (I don't really know Matlab, but this appears to be a fairly straightforward script, so I'm a bit stumped).
The file starts with
clear all
filein=['Runs/'];
Namein1=['AIC'];
Nameout=['Nash'];
It then does a bunch of calculations to get Nash-Sutcliffe coefficients (not important for this issue) and then tries to write the results to one file:
%Write Nash
%Output file writing
%Write file header
D={'Conbination','Nash with Error','Nash-error','RMSE','RMSE-error',...
'AIC', 'MinNash', 'MaxNash'};
NameOut=[filein,Nameout, '.txt'];
fileID = fopen(NameOut,'w');
for i=1:length(D)-1
fprintf(fileID,'%s\t',D{i});
Then more stuff follows, but this is where I get the error message:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Nash_0EV_onlyT (line 169)
fprintf(fileID,'%s\t',D{i});
I don't get what is wrong here? The script specifies the file, and uses fopen...? Isn't it supposed to create the file Nash.txt with the fopen statement (this file currently does not exist in my folder Runs/)? What am I missing? Thanks!
PS I am running Matlab2013a (group license via the university) on a MacBook Pro with OSX 10.8
Try using fclose all before calling this script again. Often when testing, the file handle is never released (an error occurs before the file is closed), causing fopen on the same file to fail.
The better way to do this would be to use a more fail-safe construct:
NameOut = [filein Nameout '.txt'];
fileID = fopen(NameOut,'w');
if fileID > 0
try
for i = 1:length(D)-1
fprintf(fileID,'%s\t',D{i});
end
fclose(fileId);
catch ME
fclose(fileId);
throw(ME);
end
else
error('Error opening file.');
end
I am running Win 10 and matlab 2015a, but it happens in mine too.
finally, I realise that the matlab.exe can't write file in the folder /../bin/
so, change to
Nameout=['C:\Users\yourname\DesktopNash'];
try it, then tell me what' going on.
Okay, so as I said, I don't know Matlab... I hadn't properly specified the path for writing! So it was reading the input but I guess didn't know where to write to. Thanks for the answers, it did help me narrow down the problem!

a+ file permission in Matlab

I opened a File in Matlab using the a+ permission to read and write, and append data to end of file. I am able to write and append data to the file properly, but I am not able to read.
Though the file permission definition says reading and writing, I am not able to read. Can you please help me here? Anybody encountered this problem?
fid = fopen('E:\code folder\Translation values.txt','a+')
[Array] = fscanf(fid,'%f %f')
The array returned is empty. same is the problem with textscan as well. The Cell array is empty. But when I change the permission to r+, it works fine. Please help me.
The flag a+ stands for "append and read", thus, the file position indicator pointer probably points to the end of the file. If so, you might need to call fseek or frewind before reading.

What causes an invalid file identifier in MATLAB?

I have a MATLAB script that I could have sworn worked fine the last time I used it (a year ago). Now, I get this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
If I understand correctly, it is failing to find or open(?) a file specified elsewhere in the script. Is this right? If so, what could cause it?
fid (file identifier) is the output of fopen. It's an integer, but not related to the file permanently. You need to use fopen to get the fid. It seems to me that you are using incorrect fid (file identifier) in some file-related I/O command, such as fread, fscanf or fclose. Unsuccessful fopen gives fid of -1. For any valid normal file successful fopen will give fid that is 3 or greater integer.
However, without any code it's impossible to say where or what the bug or error is. You could use MATLAB debugger to single-step the code from relevant fopen (set breakpoint there and run your program) until the relevant fclose and see if fid (or whatever variable name you use for file identifier) or any data structure for your file identifiers (if have more than one file identifier in your code) changes in any point between relevant fopen and fclose.
I solved this problem for my self by adding permission option to fopen.
As you see in http://www.mathworks.se/help/matlab/ref/fopen.html , fopen syntax is:
fileID = fopen(filename,permission)
Possible permissions, for example are:
'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | ...
'r' – Open file for reading.
'w' – Open or create new file for writing. Discard existing contents, if any.
'a' –
Open or create new file for writing. Append data to the end of the file.
'r+' – Open file for reading and writing.
'w+' – Open or create new file for reading and writing. Discard existing contents, if any.
'a+' – Open or create new file for reading and writing. Append data to the end of the file.
...
If I use fopen without permission option, or if I use 'r' (default) option, fopen will return -1, which is error. I success with this:
fid=fopen('tmp.txt', 'w');
fid=fopen('tmp.txt', 'a');
I had this problem. It turned out that the file I was trying to write was too large (I didn't have enough free space to accommodate it). However, the program didn't fail until the call to fclose. Very confusing!
Try freeing up some space, or writing a very small file, to test this diagnosis.
I encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.
fopen can fail because MATLAB doesn't have the permissions to read/write the file you've specified.
Try opening a file in a location where you/MATLAB have all the rights (depending on your OS).
I have used fopen with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.
I had the file opened in excel and as a result fopen returned a -1.
Took me forever to find such a trivial problem.
It also happens when trying to create a file in a non-existent directory. Try mkdir('folderName') within MATLAB or just create the directory beforehand.
The path with a forward slash at the beginning can cause the same error.
filename = '/data/myfile.txt';
throws this error, while
filename = 'data/myfile.txt';
does not produce an error.
For my situation, I have checked everything, but missed an easy step.
Please select "browse your folder" and browse for your current document location before you run your 'fopen' code.
It also occurs when a script is trying to read beyond the end of the file.