Problem to load tiff files using for loop

9 views (last 30 days)
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));
close all
clear all
% 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);
Jonathan Demmer
Jonathan Demmer on 15 Mar 2022
The entire error message is copied below this sentence...

Sign in to comment.

Accepted Answer

Jan on 15 Mar 2022
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) ???
  1 Comment
Jonathan Demmer
Jonathan Demmer on 15 Mar 2022
Thank you very much it works perfectly fine!

Sign in to comment.

More Answers (0)


Find more on Dialog Boxes 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!

Translated by