Jonah Peltz
Jonah Peltz

Reputation: 13

Inputting images into array or matrix elements in MATLAB

I'm trying to have a user select images from a folder using the following command in MATLAB:

uigetfile('*.tiff','Select the image files')

And I wish to write the images into an array or matrix with n elements (n being the number of images selected in an image selection loop).

I've tried multiple different ways, so any help would be very appreciated. Thank you kindly.

Here's my most recent attempt:

function imagereader

x={};
i=1;

response = 1;

while response ~= 0
    [FileName] = uigetfile('*.tiff','Select the image files')
    x{i} = FileName;

    choice = questdlg('Do you wish to select more images?','Image             selection','Yes','No','No');
      switch choice
          case 'Yes'
              response = 1;
              i+1;
          case 'No'
              response = 0;
      end
end

while i >= 1
    image(x{i})
    i-1;
end    

Upvotes: 0

Views: 254

Answers (2)

Marcin
Marcin

Reputation: 238051

I amended your example. Hope it helps:

function imagereader

x={};
i=1;

response = 1;

while response ~= 0
    [FileName,PathName] = uigetfile('*.tiff','Select the image files');
     [FileName,PathName]
    x{i} = imread([PathName, FileName]);

    choice = questdlg('Do you wish to select more images?','Image             selection','Yes','No','No');
      switch choice
          case 'Yes'
              response = 1;
              i+1;
          case 'No'
              response = 0;
      end
end

while i >= 1
    figure;
    imshow(x{i});
    i = i-1;
end    

Upvotes: 1

Shai
Shai

Reputation: 114786

The most stright-forward way would be to store the images in a cell array with n cells:

filenames = uigetfiles('*.tiff','Select the image files', 'multiselect', 'on' );
n = numel(filenames); % number of files selected
imgs = cell( n , 1 ); % pre-allocate space
for ii=1:n
    imgs{ii} = imread( filenames{ii} );
end

If all your images are of the same size, you may stack them into a 4D (color images) / 3D (gray-scale images) arrays.

Upvotes: 0

Related Questions