MATLAB Answers

Read a specified list of images given the filename and the directory the files are in

22 views (last 30 days)
Devdolly Saini
Devdolly Saini on 7 Sep 2019
Edited: Jeremy Hughes on 13 Sep 2019
I am not too sure on how to write a function that takes in as inputs a string containing the name of a directory that the images are contained in, and a 1xn 1D array containing n strings where each element is a filename of an image to read.
the function needs to return one output; a 1D array containing n images where each element is an RGB image.
Any help would be appreciated:)

  0 Comments

Sign in to comment.

Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 7 Sep 2019
Edited: Stephen Cobeldick on 7 Sep 2019
D = "path of the directory where the images are saved";
S = ["string array","of","the image filenames"];
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(D,S(k));
C{k} = imread(F);
end
See also:

  3 Comments

Walter Roberson
Walter Roberson on 8 Sep 2019
function homework7b(D, S)
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(D,S(k));
C{k} = imread(F);
end
end
Rik
Rik on 13 Sep 2019
Comment posted as answer by Devdolly Saini:
how will i write a function for this piece of code though. sorry I am very new to MATLAB.
Walter Roberson
Walter Roberson on 13 Sep 2019
What I posted is a function already. The only thing it is missing is returning the images as a 1d array.

Sign in to comment.


Jeremy Hughes
Jeremy Hughes on 13 Sep 2019
Edited: Jeremy Hughes on 13 Sep 2019
Datastores are convenient for reading collections of files.
imds = imageDatastore(directory)
imshow(read(imds))
Each time you call read, it will give the next image in the directory.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!