programmer
programmer

Reputation: 577

store multi images in mat file using matlab

The bellow matlab code is to read 6 images from folder and save them in mat file then return to read the mat file and check the images inside it

the problem is that just the last images stores in mat file

the problem is in save function::

what should I edit to make save function store all images that stored in result cell into mat file

%Generate mat file
srcFile = dir('C:\Users\Desktop\images\*.jpg');
result = cell(1,length(srcFile));
for i = 1 : length(srcFile)
    filename = strcat('C:\Users\Desktop\images\',srcFile(i).name);
    I = imread(filename);
    %figure, imshow(I);
    I = imresize(I,[128 128]);
    result{i} = I;  
    figure, imshow(result{i});
end

save images, result;



%Read mat file 
for j =1 :length(srcFile)
    filename = strcat('C:\Users\Desktop\images\',srcFile(j).name);
    I = imread(filename);
    a='I';
    input = load('images.mat',a);
    figure, imshow(input.(a));
end

Upvotes: 1

Views: 813

Answers (1)

EBH
EBH

Reputation: 10440

Your first loop, up to the save, is fine.

When you load the data, use load('images.mat') before the second loop. Then, you have back the result variable in your workspace and you iterate on it:

load('images.mat')
for j = 1:length(srcFile)
    figure, imshow(result{j});
end

What you have to remember is that your .mat file only contains the variables you saved, but you can't access them directly with load. You first load them, and then access the loaded variables (which have a different name from the file, usually).

Finally, if you want to check this code, you need to clear the workspace after the save, otherwise you may not notice that some of the variables you use are not there anymore (like the error you have got with I).

Upvotes: 0

Related Questions