Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Improper assignment with rectangular empty matrix., how to solve ?

Asked by mounim

mounim

on 14 Dec 2012
Accepted Answer by Walter Roberson

Walter Roberson

hey everyone

please i am getting this error :

Improper assignment with rectangular empty matrix on this line :

midpoints1(k) = (leftEdge1 + rightEdge1)/2;

how can i solve that ? any help is appreciated, thanks

for k=700:800
   oneRow1 = binaryImage(k, :);
   leftEdge1 = find(oneRow1, 1, 'first');
   rightEdge1 = find(oneRow1, 1, 'last');
   hold on;
   midpoints1(k) = (leftEdge1 + rightEdge1)/2;
   plot(leftEdge1, k, 'rx','LineWidth',2);
   plot(rightEdge1, k, 'rx','LineWidth',2);
   plot(midpoints1(k), k, 'yx','LineWidth',2);
end
X1=(700:800);
midpoints1=midpoints1(midpoints1~=0);
new_x1 = linspace(500, 900);
coeffs1 = polyfit(X1, midpoints1, 1);
new_y1 = polyval(coeffs1, new_x1);
plot(new_y1,new_x1, '-','LineWidth',2);

6 Comments

mounim

mounim

on 14 Dec 2012

well, the most appopriate is just to ignore that row, and go on.

Walter Roberson

Walter Roberson

on 14 Dec 2012

Read the documentation for isempty()

What value do you want stored in midpoints1(k) in this situation?

mounim

mounim

on 14 Dec 2012

it have to just store a zero ( a black pixel ), i tried this code, but it is not working 100% still gives me errors:

if isempty(midpoints1(k)) then
    midpoints1(k)== 0;
end

this one not working neither

if isempty(midpoints1(k)) then
    midpoints1(k)= zeros(size(midpoints1(k)));
end
mounim

mounim

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson

Walter Roberson

on 14 Dec 2012
Accepted answer

In some rows, there are no 1's. When that happens, the results of find() are empty. You are trying to store the midpoint of that empty range into midpoints1(k), which requires a location which is a 1x1 value not a 0 x 0 value.

1 Comment

Walter Roberson

Walter Roberson

on 14 Dec 2012
if isempty(leftEdge1)
  midpoint1(k) = 0;
else
  midpoints1(k) = (leftEdge1 + rightEdge1)/2;
  %and do the plotting here
end
Walter Roberson

Walter Roberson

Contact us