error using "fread"

7 vues (au cours des 30 derniers jours)
mahdi khabazi
mahdi khabazi le 17 Sep 2021
Modifié(e) : Jan le 17 Sep 2021
Hi. in the below i wrote my code and i have this error. please help me
error
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in statistics (line 12)
h=fread(e, [442,503], 'uint16');
code
clc
clear
close all
cd('E:/image_trans/Emis32_%d.bin');
fntest = dir('*.raw');
testnum = size(fntest,1);
rows=442; cols=503; bands=1; datatype='float32';
for i=1:732
f=sprintf('E:/image_trans/Emis32_%d.bin',i);
e=fopen(f);
h=fread(e, [442,503], 'uint16');
d=h(:);
data(i,:)=h';
fclose(f);
end

Réponses (1)

Jan
Jan le 17 Sep 2021
Modifié(e) : Jan le 17 Sep 2021
cd('E:/image_trans/Emis32_%d.bin');
This does not look like a valid folder name. Is there really a % character? Later on this is used as a file name.
Whenever you open a file, check for the success:
[e, msg] = fopen(f);
assert(e > 0, msg); % Show the message, if fopen failed
You create the file handle called "e" with fopen, but try to close "f" with fclose.
I guess, this cleaned version is working:
Folder = 'E:/image_trans/';
fntest = dir(fullfile(Folder, '*.raw'));
rows = 442;
cols = 503;
data = zeros(numel(fntest), rows * cols); % pre-allocate for speed
for i = 1:numel(fntest)
file = fullfile(Folder, fntest(i).name);
[fid, msg] = fopen(file, 'r');
assert(fid > 0, msg);
data(i, :) = fread(fid, [1, rows*cols], 'uint16');
fclose(fid);
end

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by