MATLAB Answers

Adrian
0

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

Asked by Adrian
on 18 Feb 2014
Latest activity Commented on by Steven Lord
on 3 Feb 2018
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.

  0 Comments

Sign in to comment.

2 Answers

Answer by 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.

  0 Comments

Sign in to comment.


Answer by 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...

  3 Comments

I have had similar problems using fclose. I am running MatlabR2017b on Mac 10.12.6. I have not been able to find out what causes the problem, but the problem goes away (at least temporarily) when I reboot Matlab.
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.

Sign in to comment.