Please help - Matlab novice here: Error using fprintf. Invalid file identifier. Use fopen to generate a valid file identifier.
1 view (last 30 days)
Show older comments
HI,
I am a complete novice at Matlab so I will apologise now for my lack of comprehension in Matlab coding.
Background I have been given a script which has been used previously to test co-operativity in some data we gather when conducting our scientific research. I have previously used this script fine and had no issues in the US. I am now trying to use this in the UK on Matlab2015a and the exact same code is spitting out the following error:
"Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier.
Error in CoupledGating (line 20) fprintf(fid,'%s \n',output_fname);
Error in run (line 96) evalin('caller', [script ';']);"
I was using this fine in the US, but I don't know what version of Matlab I was using there - Any chance transferring to a newer version may lead to this error? (I am currently trying to find out what version I used there).
The first error refers to the following codes:
output_fname=input('Please enter the filename where output will be saved (including extension):','s');
fid=fopen(output_fname,'r+');
fprintf(fid,'%s \n',output_fname); **(line 20)**
fprintf(fid,'%s ','File');
fprintf(fid,'%6s ','nPs');
fprintf(fid,'%6s ','Ps');
fprintf(fid,'%8s ','kappa');
fprintf(fid,'%8s ','rho');
fprintf(fid,'%8s ','zeta');
fprintf(fid,'%8s ','Ca Moment');
fprintf(fid, '%8s ', 'mu');
fprintf(fid,'%9s ','precision');
fprintf(fid,'%9s \n','iteration');
The second error, I am not too sure what it refers to. It's an "error in run" - I'm not sure what this means.
Any help anybody can give will be greatly appreciated.
4 Comments
Walter Roberson
on 14 Dec 2016
I notice that you used
fid=fopen(output_fname,'r+');
and that you use fprintf() to write to the file. You should only open for 'r+' (read and write) for binary files or in the rare case where you read through part of a text file and rewrite from that location through to the end of file (at least) when you are absolutely certain that the new information will take you to at least the old end of file. As you are not reading anything from the file and you are not operating in binary, chances are strong that you want 'w' permission instead of 'r+' -- or possibly you want 'a' or 'a+' (append to file).
This difficulty would not cause the error message you observe, which is caused by the failure to open the file at all. Using 'r+' permission inappropriately results in messed up partly-overwritten files.
Answers (1)
Guillaume
on 14 Dec 2016
Unfortunately, fopen doesn't throw an error when it fails to open a file. It would have made your problem clearer if it had. As the error message tells you the file identifier is not valid, and that is because fopen failed to open your file.
You may get a clue as to the cause of the error if you change your fopen call into:
[fid, errmsg] = fopen(...
and look at the content of errmsg. There can be plenty of reasons why the call fails, the most likely being you don't have write permission in the current directory or that the entered filename is not valid for your OS.
You should always check the value of fid after fopen. If it is -1, the file is not opened.
I would also recommend always specifying the full path of the file rather than relying on the current directory being the correct one (or worse, changing the current directory):
[fid, errmsg] = fopen(fullfile(folderpath, output_fname), 'r+');
There is no second error, the error message just shows you the call chain that lead to the error.
2 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!