Why did my fread suddenly stop working?

2 views (last 30 days)
file = uigetfile('*.bin', 'select a binary file');
[fid, message] = fopen(file,'r');
% Read number of frames
NumberOfFrames = fread(fid, 1, 'int16');
I've had this code working for a couple of weeks now. I reinstalled matlab yesterday and took the night. When I returned today this code produced an error which it has not done before, namely:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
I would appreaciate any help on the matter, or fixes if anyone has had this happening.
  2 Comments
KSSV
KSSV on 10 Feb 2017
What is value of fid? Probably problem is at fopen.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Feb 2017
[filename, filedir] = uigetfile('*.bin', 'select a binary file');
if ~ischar(filename)
return;
end
file = fullfile(filedir, filename);
[fid, message] = fopen( file, 'r');
if fid < 0
error("failed to open file "%s" because "%s"', file, message);
end
  2 Comments
Stian Hilstad
Stian Hilstad on 10 Feb 2017
It worked, thank you! But could you give me any insight into what the error was and why it didn't occur before?
Walter Roberson
Walter Roberson on 10 Feb 2017
My guess is that you were asking to read a file in a different directory. Your code expected the file to be in your current directory.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!