Hello everyone,
i am trying to use imread to read multiple images in a for loop.
path='Z:\collect_data';
for t=1:length(nan_rows)
image_test=imread([path,'nan_rows' (t)]);
%further coding
end
the 'nan_rows' here is a string array.
I want to read the images which the file names are stored in the array.
Does anyone know how to do it?
Thanks a lot in advance!

 採用された回答

VBBV
VBBV 2023 年 2 月 23 日
編集済み: VBBV 2023 年 2 月 23 日

0 投票

image_test{t}=imread([path,'nan_rows' num2str(t)])

7 件のコメント

VBBV
VBBV 2023 年 2 月 23 日
編集済み: VBBV 2023 年 2 月 23 日
Use a cell array to store images. While concatenation of path with filenames, you need to use num2str
Yunwei
Yunwei 2023 年 2 月 23 日
Hi,
sorry to say it but the script you posted is not really working,
It causes a error massage saying "File "Z:\collect_data\nan_rows1" does not exist."
I need to get the data array of "nan_rows".
VBBV
VBBV 2023 年 2 月 23 日
編集済み: VBBV 2023 年 2 月 23 日
Is nan_rows a file or folder ? If it's file then it needs additional string called file extension. E.g
image_test{t}=imread([path,'nan_rows', num2str(t),'.jpg'])
Also, check whether the file exists in the folder collect_data
VBBV
VBBV 2023 年 2 月 23 日
編集済み: VBBV 2023 年 2 月 23 日
If nan_rows is folder, then you need to concatenate filename also to the path. Can you share the path and/or files ?
Yunwei
Yunwei 2023 年 2 月 23 日
nan_row is an array.
I have attached it here.
DGM
DGM 2023 年 2 月 23 日
According to the question, the filenames are in a string array, so I don't know why we're generating number suffixes at all. If we are to generate number suffixes, then we'd actually need to know how they're formatted.
% you have your base path
fprefix = fullfile(matlabroot,'toolbox','images','imdata');
% and you say you have a string array of filenames
fnames = ["cameraman.tif" "tire.tif" "coins.png" "strawberries.jpg"];
nfiles = numel(fnames);
image_test = cell(nfiles,1); % preallocate
for k = 1:nfiles
image_test{k} = imread(fullfile(fprefix,fnames(k)));
end
montage(image_test)
Build the path with fullfile(). If you have to programmatically construct the filename, using sprintf() is simpler and more readable imo.
Yunwei
Yunwei 2023 年 2 月 23 日
That's exactly what l need.
Thank you so much for your help!

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

その他の回答 (0 件)

質問済み:

2023 年 2 月 23 日

コメント済み:

DGM
2023 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by