Problem to load tiff files using for loop

4 次查看(过去 30 天)
Hi all,
I would like to reshape several (N = 12) tiff files and do that through a for loop to go quicker than doing it one by one. Find below the code I wrote, however an error appeared, which said that only Mat and ASCII can be open this way. DOes anybody knows how to do it for .tiff files, please?
Error using load
Unable to read file
'E:\Data_emodnet_fisheries\Vessel_density\2017_01_st_01.tif'. Input
must be a MAT-file or an ASCII file containing numeric data with
same number of columns in each row.
Error in code (line 15)
t = load(fullfile(cd, file));
Regards
close all
clear all
clc
% assign the path to your working directory:
cd ('E:\Data_emodnet_fisheries\Vessel_density\');
file = '2017_##_st_01.tif';
file1 = '##.mat';
for it = 1:12
file(6:7) = sprintf('%02.0f',it);
file1(1:2) = sprintf('%02.0f',it);
t = load(fullfile(cd, file));
imageData = read(t);
new_lim = imageData(2700:4000, 3500:4700);
save(file1,'new_lim');
end
  3 个评论
Jan
Jan 2022-3-15
The brute clearing header "close all, clear all, clc" is rarely useful. Especially clear all has no benefit, but wastes a lot of time.
"which said that only Mat and ASCII can be open this way" - please post a copy of the error message, because the details matter. This is much better than paraphrasing the message.
Jonathan Demmer
Jonathan Demmer 2022-3-15
The entire error message is copied below this sentence...

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-3-15
load() works with MAT files and some text formats only. If you want to load an image file, use imread().
Using CD to change the current directory is less stable than using the absolute file path. Especially fullfile(cd, file) is a fragile overkill.
folder = 'E:\Data_emodnet_fisheries\Vessel_density\';
fmt = '2017_%02d_st_01.tif';
for it = 1:12
imageFile = fullfile(folder, sprintf(fmt, it));
imageData = imread(imageFile);
new_lim = imageData(2700:4000, 3500:4700, :); % RGB data have 3 dimensions
matFile = fullfile(folder, sprintf('%02d.mat', it));
save(matFile, 'new_lim');
% Or: imwrite(new_lim, file) ???
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by