Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB error: Function 'subsindex' is not defined for values of class 'struct'

Tags:

matlab

I tried these commands:

im=imread('untitled_test1.jpg');
im1=rgb2gray(im);
im1=medfilt2(im1,[15 15]);
BW = edge(im1,'sobel'); 

msk=[0 0 0 0 0;
 0 1 1 1 0;
 0 1 1 1 0;
 0 1 1 1 0;
 0 0 0 0 0;];
B=conv2(double(BW),double(msk));

Ibw = im2bw(B);
CC = bwconncomp(Ibw); %Ibw is my binary image
stats = regionprops(CC,'pixellist');

% pass all over the stats
for i=1:length(stats),
size = length(stats(i).PixelList);
% check only the relevant stats (the black ellipses)
if size >150 && size < 600 
    % fill the black pixel by white    

    x = round(mean(stats(i).PixelList(:,2)));
    y = round(mean(stats(i).PixelList(:,1)));
    Ibw = imfill(Ibw, [x, y]);

else
    Ibw([CC.PixelIdxList{i}]) = false;
end;
end;

(here I have another command lines, but I guess the problem is not because of them.)

labeledImage = bwlabel(binaryImage, 8);     % Label each blob so we can make measurements of it
blobMeasurements = regionprops(labeledImage, Ibw, 'all');   
numberOfBlobs = size(blobMeasurements, 1); 

I got this error message:

??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'struct'.

Error in ==> test2 at 129
numberOfBlobs = size(blobMeasurements, 1);

What's going wrong?

like image 433
Alon Shmiel Avatar asked Apr 08 '12 08:04

Alon Shmiel


2 Answers

You're getting that error because you have created a variable called "size" which shadows the built-in function SIZE. Instead of calling the function to compute numberOfBlobs, MATLAB instead tries to index the variable using the structure blobMeasurements as an index (which doesn't work, as the error message shows).

In general, you shouldn't give a variable or function the name of an already existing function (unless you know what you're doing). Just change the name of the variable in your code to something other than "size", issue the command clear size to clear the old size variable from the workspace, and rerun your code.

like image 118
gnovice Avatar answered Nov 03 '22 14:11

gnovice


Your error message tells you that the error is in numberOfBlobs = size(blobMeasurements, 1);. subsindex is most likely used in size(..., 1) to access those elements.

I assume that blobMeasurements is an array of structs (or a single struct), for which that operation is not completely defined.

Why don't you use the length command as before? That worked a little earlier in your code.

like image 1
Egon Avatar answered Nov 03 '22 15:11

Egon