从 MATLAB 中的文件夹读取许多 .img 文件时出错
Error reading many .img files from a folder in MATLAB
我尝试使用以下代码从目录中读取 100 个 .img 文件:
srcFiles = dir('/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/*.img'); % the folder in which ur images exists
for i = 1:100 % length(srcFiles)
filename = srcFiles(i).name;
fid = fopen(filename);
image = fread(fid, 2048*2048, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 2048, 2048);
figure;
imshow(image);
end
'/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/'
是我的 .img 文件所在目录的路径。看来我在定义文件标识符时出错了,但我不知道我错过了什么:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in sequenceimage (line 32)
image = fread(fid, 2048*2048, 'uint8=>uint8');
谁能帮我解决这个错误?
您收到该错误的原因是因为 dir
returns 相对 列出的每个文件的名称,而不是 绝对 每个文件的路径。因此,通过执行 srcFiles(i).name
,您只会获得文件名本身 - 而不是 文件的完整路径 .
因此,您需要在调用 fopen
时将目录附加到文件本身的顶部。
为了使事情更灵活,将目录放在一个单独的字符串中,这样您只需在一个地方而不是两个地方修改代码。
非常简单:
%// Change here
loc = '/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/';
%// Change here
srcFiles = dir([loc '*.img']); % the folder in which ur images exists
for i = 1:100 % length(srcFiles)
filename = srcFiles(i).name;
%// Change here!
fid = fopen([loc filename]);
image = fread(fid, 2048*2048, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 2048, 2048);
figure;
imshow(image);
end
我尝试使用以下代码从目录中读取 100 个 .img 文件:
srcFiles = dir('/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/*.img'); % the folder in which ur images exists
for i = 1:100 % length(srcFiles)
filename = srcFiles(i).name;
fid = fopen(filename);
image = fread(fid, 2048*2048, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 2048, 2048);
figure;
imshow(image);
end
'/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/'
是我的 .img 文件所在目录的路径。看来我在定义文件标识符时出错了,但我不知道我错过了什么:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in sequenceimage (line 32)
image = fread(fid, 2048*2048, 'uint8=>uint8');
谁能帮我解决这个错误?
您收到该错误的原因是因为 dir
returns 相对 列出的每个文件的名称,而不是 绝对 每个文件的路径。因此,通过执行 srcFiles(i).name
,您只会获得文件名本身 - 而不是 文件的完整路径 .
因此,您需要在调用 fopen
时将目录附加到文件本身的顶部。
为了使事情更灵活,将目录放在一个单独的字符串中,这样您只需在一个地方而不是两个地方修改代码。
非常简单:
%// Change here
loc = '/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/';
%// Change here
srcFiles = dir([loc '*.img']); % the folder in which ur images exists
for i = 1:100 % length(srcFiles)
filename = srcFiles(i).name;
%// Change here!
fid = fopen([loc filename]);
image = fread(fid, 2048*2048, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 2048, 2048);
figure;
imshow(image);
end