MATLAB: I am getting error “The length of X must match the number of rows of Y.”

image analysisMATLAB

RGB =imread('image.jpg');
hsv= rgb2hsv(RGB);
imshow(hsv);
r1=0.1;
r2=0.85;
i= 0:20:240;
s= 0.0:0.2:1.0;
wgray = 1 - bsxfun(@power, s(:), (r1*(255./(i(:).')).^r2) );
P = gradient(wgray,0.2);
Q= gradient(wgray,0.4);
R= gradient(wgray,0.6);
S= gradient(wgray,0.8);
T= gradient(wgray,1.0);
bar(i,P,);
please kindly help .

Best Answer

kaylani - at which line are you observing this error? Note that when I run your code, the only problem I observe is with
bar(i,P,);
and the error is
Unbalanced or unexpected parenthesis or bracket.
Once I correct this (remove the final comma), then I see how it is this same line, invoking bar, that generates your error message.
Error using bar (line 58)
The length of X must match the number of rows of Y.
This is because your 'x' input, i (which should be renamed to avoid using MATLAB's representation for an imaginary number), is a 1x13 array whereas your 'y' input, P, is a 6x13 matrix. According to the documentation for bar
x values, specified as a vector or a matrix. If x and y are both vectors, then they must be equal length. If x and y are both matrices, then they must be equal size. If x is a vector and y is a matrix, then the length of x must equal the number of rows in y.
So your length for x must be 6 and not 13 if you wish it to work with the provided data. Or, perhaps you can transpose P as
bar(i,P');