MATLAB: I want to read 24 images and stores value of omega,imou,nb into mat file, but some error in the program…

matrix read

function [timeelapsed1] = matfileforallimages()
%create .mat file and calculates weights of each binary image in the database
clc;
clear all;
% database veins
nuser=4;
nrep=6;
path='C:\Program Files\MATLAB\R2013a\bin\img\PCA\my_PhD_Programs\DorsalVeinsDEMO\templates';
for iuser=1:nuser
for irep=1:nrep
nfichd=['binaryROImano-',num2str(iuser,'%.3d'),'-',num2str(irep,'%.3d')];
nfichd=[path,'\',nfichd,'.bmp'];
image1=imread(nfichd,'bmp');
%cd ..
timeelapsed1=cputime;
[irow,icol]=size(image1);
S=reshape(image1, irow*icol, 1);
%image mean
imoy=[];
imoy=mean(S,2);
S=double(S);
%eigen values and eigen vectors for the matrix L=S'S
L=S'*S;
[vv dd]=eig(L);
v=[];
d=[];
for i=1:size(vv,2)
if (dd(i,i)>1e-4)
v=[v vv(:,i)];
d=[d dd(i,i)];
end
end
%sort eigen values of L=S'S
[dtemp index]=sort(d,'descend');
for i=1:length(d)
vtemp(:,i)=v(:,index(i));
end
d=dtemp;
v=vtemp;
%normalize eigen vectors of L=S'S
for i=1:size(v,2)
kk=v(:,i);
temp=sqrt(sum(kk.^2));
v(:,i)=v(:,i)./temp;
end
%eigen values and eigen vectors for the covariance matrix C=SS'
u=[];
for i=1:size(v,2)
temp=sqrt(d(i));
u=[u (S*v(:,i))./temp];
end
%normalize eigen vectors of C
for i=1:size(u,2)
kk=u(:,i);
temp=sqrt(sum(kk.^2));
u(:,i)=u(:,i)./temp;
end
%number of eliminated components from the eigen base
nb=0;
%weights of database's images
omega=[];
for h=1:size(S,2)
ww=[];
for i=1:size(u,2)-nb
t=u(:,i);
Weight=dot(t,S(:,h)');
ww=[ww;Weight];
end
omega=[omega,ww];
end
M=4;
save('omega1', 'omega', 'imoy', 'u', 'nb', 'M') %omega contains the extracted features of database images
end
end

Best Answer

Balaji - note that on each iteration of the second loop you are saving the data to file
for iuser=1:nuser
for irep=1:nrep
...
M=4;
save('omega1', 'omega', 'imoy', 'u', 'nb', 'M') %omega contains the extracted features of database images
end
end
which means that you are overwriting the data in the omega1 file on each of these iterations. So the only data in this file corresponds to the 24th iteration.
If you want to store all of this data to the same file, then I suggest that you create some cell arrays (in case your matrix dimensions change on any iteration) to store the data from each iteration. Once you have finished looping over all of these files, then save the cell array data to file (doing this outside of the outer for loop.