0

I want to loop through a list of images in Matlab by unsing a loop. I want to get one Image at a time and turn it into a Matrix by using the imread function. Then I want to save each Matrix in a variable that is alphabetical and declared automatically in a separate loop.

myFolder = '/user/Matlab/projectfile/pictures';
filePattern = fullfile(myFolder, '*.bmp');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(theFiles(k).folder, baseFileName);
    imageArray = imread(fullFileName)

My problem is that I don't get the return. If I save my imageArray it only creates a Matrix of one image. But I need the program to save each Matrix within a unique Variable such as A,B,C,D... and so on. It should not matter how many pictures there are.

If anyone has a tipp for me I would be glad, thx :)

Johannes
  • 25
  • 7

1 Answers1

1

I think you can use structure and dynamic call.

Here an example :

A=struct();
for k = 1 : 5
    fieldName=char(strcat('Field_',string(k)));
    A.(fieldName)=k; % Be sure to use parenthesis
end

The A variable is then :

A = 

  struct with fields:

    Field_1: 1
    Field_2: 2
    Field_3: 3
    Field_4: 4
    Field_5: 5

So with that, you can store variables as wish.

Hope it helps, tell me if I misunderstood.

AnchorInk
  • 46
  • 4
  • Good strategy! If all the images are of the same size though, consider using an ND matrix, e.g. for a 10 400-by-400 RGB images you could store it as `A = zeros(400,400,3,10)`. If your images are of unequal sizes, indeed use a struct (epseically when you store more information about the images) or a cell-array. – Adriaan Jan 06 '22 at 13:28