0

I have 20 Images saved in the .mat file and when I load these images to the workspace I want to iterate them in the function. The images have different names and different sizes. Anyone can help?

Abu
  • 3
  • 1
  • i think your question does not provide enough information to be answered, but maybe this link answers your question https://stackoverflow.com/questions/408080/is-there-a-foreach-in-matlab-if-so-how-does-it-behave-if-the-underlying-data-c – Hadi Sep 02 '21 at 19:38
  • 1
    You can use [evalin](https://www.mathworks.com/help/matlab/ref/evalin.html) as [here](https://www.mathworks.com/matlabcentral/answers/51907-how-do-i-get-the-value-of-a-variable-from-the-base-workspace-in-my-gui), but you shouldn't... Better solution: **1.** Pass the name of mat file to your function. **2.** [load](https://www.mathworks.com/help/matlab/ref/load.html) the mat into as structure like `S = load('f.mat');`. **3.** Use [struct2cell](https://www.mathworks.com/help/matlab/ref/struct2cell.html) like `C = struct2cell(S)` **4.** iterate `C`. (you may also iterate the struct fields). – Rotem Sep 02 '21 at 19:57
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 06 '21 at 16:42

1 Answers1

2

Load your file. Call Matlab's "who(...)"-function which lists the variable names in the file. Use Matlab's "evalin(...)" function to store the variables' content into a cell array.

The final cell array "images" contains all your image data, regardless of the size of each individual image. Now you can iterate over this cell array as you see fit.

The following code should do the trick. You just need to replace your filename.

load('your_images.mat')
image_vars = who('-file', 'your_images.mat');
N_images = numel(image_vars);
images = cell(N_images, 1);
for ii = 1:N_images
    images{ii} = evalin('base', image_vars{ii});
end
QuarQ
  • 46
  • 2