How can avoid the error: Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier. Error in open837 (line 4) fclose(fid);

12 views (last 30 days)
Hello. In order to read data from a binary file, I am trying to run the following code:
function TMP = open837(filename)
fid = fopen('18oct2013-121730.837', 'r'); fclose(fid);
TMP.fileheader.ASCII8 = fread(fid,[1 1], 'uint8=>char');
TMP.fileheader.ASCII3 = fread(fid,[1 1], 'uint8=>char');
TMP.fileheader.ASCII7 = fread(fid,[1 1], 'uint8=>char');
TMP.fileheader.nToReadIndex = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.TotalBytes = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.nToRead = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Date = fread(fid,[1 12], 'uint8=>char');
TMP.fileheader.Time = fread(fid,[1 13], 'uint8=>char',0, 'ieee-be');
TMP.fileheader.VideoFrameLength = fread(fid,[1,1], 'uint32',0, 'ieee-be');
TMP.fileheader.XdcrUpDownDisplayMode = fread(fid,[1 1], 'uint8',0, 'ieee-be');
TMP.fileheader.StartGain = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.ProfileTiltAngle = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Reserved1 = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.Reserved2 = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.NumberofPingsAveraged = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.PulseLength = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.UserDefinedByte = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.SoundVelocity = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.GNSSShipsPositionLatitude = fread(fid,[1 14], 'uint8',0, 'ieee-be');
TMP.fileheader.GNSSShipsPositionLongitude = fread(fid,[1 14], 'uint8',0, 'ieee-be');
TMP.fileheader.GNSSShipsSpeed = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.GNSSShipsCourse = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Reserved3 = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.OperatingFrequency = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Pitch = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Roll = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.Heading = fread(fid,[1 1], 'uint16',0, 'ieee-be');
TMP.fileheader.RepetitionRate = fread(fid,[1 1], 'uint16',0,'ieee-be');
TMP.fileheader.DisplayGain = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.Reserved4 = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.Reserved5 = fread(fid, 1, 'uint8',0, 'ieee-be');
TMP.fileheader.Milliseconds = fread(fid,[1 5], 'uint8=>char',0, 'ieee-be');
TMP.fileheader.Reserved6 = fread(fid,[1 1], 'uint16',0, 'ieee-be');
But I get this error:
Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier.
Error in open837 (line 4) fclose(fid);
It used to work before, but I can't tell what's wrong now. If someone could help, I would be much obliged. Thank you.

Answers (2)

Wayne King
Wayne King on 18 Feb 2014
What is
fid = fopen('18oct2013-121730.837', 'r');
returning, a -1?
You can use
[fid,msg] = fopen('18oct2013-121730.837', 'r');
to return the system dependent error message in msg.
If fid is equal to -1, then perhaps the path where the file resides is not on the MATLAB path? If that is the case use
>>pathtool
or addpath() to add the path. Other things to check include making sure you are spelling the name of the file correctly with all the necessary file extensions.

Jon Dudley
Jon Dudley on 21 Nov 2016
I have similar problem but the answer provided above does not explain it. I have a cell of (valid) filenames "files" and the code:
for n=1:length(files)
fid = fopen(files{n},'r');
data = fread(fid);
fclose(fid);
end
When I run the code, it will get to some integer "n" before breaking with the error:
"Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier."
This is already weird, because if I had an invalid file identifier, then it should quit at "fread". But it gets weirder - if I re-run the code, it will progress past the integer at which it failed previously, but then fail at some other "n". I'm pulling my hair out here...
  4 Comments
Steven Lord
Steven Lord on 3 Feb 2018
Call fopen with two output arguments. Before trying to use the first output argument to write to or read from the file, check if it is -1. If it is, MATLAB was not able to open the file and the second output argument will be a message indicating why.
It could be that the file you're trying to read doesn't exist, you don't have permissions to read or write the file, the disk is full, you've specified an invalid file name, or any of a host of other reasons.
If the message doesn't give you enough information to determine why MATLAB was unable to open the file, post the full message here and we may be able to offer suggestions.
Yahor
Yahor on 23 Oct 2019
I am having the same issue. Openinig MATLAB 2018b (Windows) as admin marginally helped, but I still have my file descriptor fileID becoming :
for i = 1:700
plot(data(i));
[fileID, msg] = fopen(fname, 'w');
print(fname,'-dbmp','-r100');
msg
fclose(fileID);
close(gcf);
end
Spits out:
msg =
'Invalid argument'
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.
I did what Steven Lord proposed in two of his posts, but it seems to me that it is an internal MATLAB error.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!