How to open a number of directories equal to a number given by a user

조회 수: 1 (최근 30일)
Im trying to open tif images from a number of directories given by the user in a inputdlg. Afterwards im trying to plot these tif images as a figure. What I cant figure out is how to get a new directory to open for each iteration of the for loop. Any help is appreciated, below is my code:
answer = inputdlg('Enter Number:');
value = str2double(answer{1});
for k = 1:numel(value)
P = uigetdir('C:\');
S = dir(fullfile(P(k),"*.tif"));
F = fullfile(S(k).folder,S(k).name);
[A, R, cmap] = readgeoraster(F(k));
figure
mapshow(A, cmap, R)
end

채택된 답변

Cris LaPierre
Cris LaPierre 2025년 3월 11일
편집: Cris LaPierre 2025년 3월 11일
P is not a vector so you do not need to index it. Its value is updated each loop. Just use it as you would outside a loop.
This is also true for S and F.
Also, how many numbers do you expect people to enter? I assume just one. Your code will loop numel(value) times, which should be once. I think you want it to loop value times.
Try this. Note this assumes one tif file per directory. If there are more, you will need a second for loop to load the images one at a time.
answer = inputdlg('Enter Number:');
value = str2double(answer{1});
for k = 1:value
P = uigetdir('C:\');
S = dir(fullfile(P,"*.tif"));
F = fullfile(S.folder,S.name);
[A, R, cmap] = readgeoraster(F);
figure
mapshow(A, cmap, R)
end
  댓글 수: 1
Bradley
Bradley 2025년 3월 11일
The user might enter a 1 or values greater than that, for this specific tool I would guess 3 to 5 would be the number they would want to enter. Your code worked! thank you for your answer.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by