How to read multiple image directory with for loop

3 ビュー (過去 30 日間)
MD HASIBUR RAHMAN LEMON
MD HASIBUR RAHMAN LEMON 2020 年 10 月 15 日
編集済み: Stephen23 2020 年 10 月 15 日
I'm trying to display image with for loop. My code is like that. After the 9 picture the image file name is image_0010. So if i want to display this image now what i need to do? Thanks in advance.
imageArray = [];
for i = 1:200
str = int2str(i);
str = strcat('\','image_000',str,'.png');
str = strcat('C:\Users\Hasibur Rahman\Documents\MATLAB\ClassWork2_62018040032_哈西\LFPW\trainset', str);
image = imread(str);
imageArray = image;
imageArray = imageArray + image;
figure,imshow(imageArray);
title('Original Image!');
pause(1.5);
end

採用された回答

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 15 日
1 option could be:
if i>9
str = strcat('\','image_00',str,'.png');
end
  1 件のコメント
Stephen23
Stephen23 2020 年 10 月 15 日
編集済み: Stephen23 2020 年 10 月 15 日
Or just learn how to write simpler and more efficient code (e.g. sprintf).

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 10 月 15 日
編集済み: Stephen23 2020 年 10 月 15 日
Rather than fragile string concatenation and ungainly constructs involving int2str or num2str, the neat and efficient MATLAB approach is to use sprintf and fullfile:
D = 'C:\Users\Hasibur Rahman\Documents\MATLAB\ClassWork2_62018040032_哈西\LFPW\trainset';
for k = 1:200
F = sprintf('image_%04d.png',k);
I = imread(fullfile(D,F));
imshow(I);
pause(1.5)
end
This is what the MATLAB documentation recommends:
Note that I also moved the directory definition to before the loop, where it belongs.
  1 件のコメント
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 15 日
sprintf looks good.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by