Why did my fread suddenly stop working?
2 views (last 30 days)
Show older comments
Stian Hilstad
on 10 Feb 2017
Commented: Walter Roberson
on 10 Feb 2017
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
Accepted Answer
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
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.
More Answers (0)
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!