error using "fread"

조회 수: 7 (최근 30일)
mahdi khabazi
mahdi khabazi 2021년 9월 17일
편집: Jan 2021년 9월 17일
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

답변 (1개)

Jan
Jan 2021년 9월 17일
편집: Jan 2021년 9월 17일
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

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by