Problem to load tiff files using for loop

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jan
Jan il 15 Mar 2022
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 il 15 Mar 2022
The entire error message is copied below this sentence...

Accedi per commentare.

Risposta accettata

Jan
Jan il 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) ???
end

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by