UNDEFINED VARIABLE ERROR AFTER EXECUTING FOR LOOP IN MATLAB

조회 수: 11 (최근 30일)
Aybüke Ceren Duran
Aybüke Ceren Duran 2019년 4월 24일
댓글: Walter Roberson 2019년 4월 24일
for k=1:numel(listOfImages)
data{k}=imread(fullfile(whereImagesReside, listOfImages(k).name));
data{k}=im2double(rgb2gray(data{k}));
[r,c] = size(data{k}); % get number of rows and columns in image
columnMatrix(:,k)=data(:);
V{k}=data(:);
end
meanIntensities = mean(columnMatrix,2);
But I get the error Undefined function or variable 'columnMatrix'. How can I solve this?

답변 (2개)

Walter Roberson
Walter Roberson 2019년 4월 24일
You did not initialize columnMatrix, and numel(listOfImages) was 0.

Aybüke Ceren Duran
Aybüke Ceren Duran 2019년 4월 24일
format compact
%Firstly, I converted all the images in the vector form.
whereImagesReside = 'lfwdataset';
%dir function will return the images in a structure array.
listOfImages= dir(fullfile(whereImagesReside, '*.pgm'));
%0x1 structure array is created
data = cell(size(listOfImages));
%pmg files are stored in column matrix columnMatrix in the end of the loop
%below
for k=1:numel(listOfImages)
data{k}=imread(fullfile(whereImagesReside, listOfImages(k).name));
data{k}=im2double(rgb2gray(data{k}));
[r,c] = size(data{k}); % get number of rows and columns in image
columnMatrix(:,k)=data(:);
V{k}=data(:);
end
But how can I initialize columnMatrix to 0?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 4월 24일
The empty structure array from dir is the more immediate problem. You should try putting the complete path in the variable about where the images reside.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by