0
votes

Error:

Subscript indices must either be real positive integers or logicals.

Hi I am doing image conversion and I get an error when checking the size of the matrix. I am confused as to why I am getting it for this particular instance with the code:

size(maleGrey)

Here is the code I am running:

male = getAllFiles('male');

% Variable Initialization
size = 250*250;
numM = length(male);
maleGrey = zeros(size,numM);

% Convert to gray scale
for i = 1:numM
   rgb = imread(char(male(i)));
   img = single(rgb2gray(rgb));
   vec = img(:); % make it a vector of (62500,1) in size
   maleGrey(:,i) = vec; 
end
2

2 Answers

3
votes

You made the mistake of using size as a variable when calling size=250*250. Once you do that in a workspace, the function is masked (overloaded) by the variable, and Matlab will always treat further calls to size as manipulations of the variable.

Call clear size, and the function will work as intended. Also, do not use size as name for a variable (or other function names like length or double, or zeros etc), but e.g. siz, or numberOfRows (because that's what your variable stands for).

0
votes

You're overwriting (locally) Matlabs native function reference size by a variable name

% Variable Initialization
size = 250*250; % <--

Hence when you call size(maleGrey) it treats maleGrey as an index in the variable size.