Problem to load tiff files using for loop

조회 수: 1 (최근 30일)
Jonathan Demmer
Jonathan Demmer 2022년 3월 15일
댓글: Jonathan Demmer 2022년 3월 15일
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
  댓글 수: 1
Jonathan Demmer
Jonathan Demmer 2022년 3월 15일
Thank you very much it works perfectly fine!

댓글을 달려면 로그인하십시오.

추가 답변 (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