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

get error when remove an item

Asked by huda nawaf on 2 Mar 2012

hi, i want to remove an item from array, but get error

this is my code

for i=1:12
  if align(1,i)=='-' & align(2,i)=='-'
     align(1,i)=[];  align(2,i)=[];
      ??? A null assignment can have only one non-colon index.
Error in ==> testt at 6

thanks in advance


huda nawaf



No products are associated with this question.

3 Answers

Answer by Jan Simon on 2 Mar 2012
Accepted answer

When you remove a character, the string is getting shorter. But the FOR-loop tries to access all characters of the initial size.

Either run the loop in reverse direction:

align(1,:) = ['asd-as---tyr'; ...
for i=12:-1:1
  if align(1,i)=='-' & align(2,i)=='-'
    align(:,i) = [];

Or use a vectorized approach, which is faster and nicer:

align(align(1, :)=='-' & align(2, :)=='-', :) = [];  % No FOR loop, no IF

[EDITED]: Typo in the line above (thanks Walter!):

align(:, align(1, :)=='-' & align(2, :)=='-') = [];


huda nawaf on 2 Mar 2012

hi, thanks Jan the first solution seem good and get what need.
i hope to correct the second because it is faster.
i ran it and get this error
??? Matrix index is out of range for deletion.

Error in ==> testt at 12
align(align(1, :)=='-' & align(2, :)=='-', :) = [];

Walter Roberson on 2 Mar 2012

align(:, align(1, :)=='-' & align(2, :)=='-') = []; % No FOR loop, no IF

huda nawaf on 2 Mar 2012

many many thanks walter

Jan Simon
Answer by Aurelien Queffurust on 2 Mar 2012

To remove the minus sign you could use:

 align= char(strrep(align(1,:),'-','') ,strrep(align(2,:),'-','') )


Aurelien Queffurust
Answer by the cyclist on 2 Mar 2012

I can see how you would find that error message a bit cryptic, but here is what is going on.

Your variable align is a 2x12 character array. With your line of code


is going to attempt to remove the upper-left corner of that array. But MATLAB can't do that. There cannot be a character array that has one row of 11 elements, and a second row that is 12 elements. One element cannot be "empty" in that way. (That is what error message is saying.) You can only do the null assignment for a whole row or column at once.

So, in your case, you could combine your two null assignment statements into the single statement

align(:,i) = [];

and MATLAB will delete the i'th column, in its entirety.

Be aware that your array align is going to keep getting trimmed, as it goes through the for loop, so it will no longer be width 12.


huda nawaf on 28 Mar 2012

I want to remove any two item are equal without loop
x=[1 2 3 5];
y=[4 3 1 6 7];
I want the result be
x=[2 5]
y=[4 6 7]
how do that?

Jan Simon on 28 Mar 2012

Please post a new question in a new thread.

huda nawaf on 28 Mar 2012

okay, but erlier walter said to me if the new question relate previous thread do not send new thread

the cyclist

Contact us