MATLAB: Get error when remove an item

remove

hi, i want to remove an item from array, but get error
this is my code
align(1,:)='asd-as---tyr';
align(2,:)='fg--ds--h-ty';
for i=1:12
if align(1,i)=='-' & align(2,i)=='-'
align(1,i)=[]; align(2,i)=[];
end
end
??? A null assignment can have only one non-colon index.
Error in ==> testt at 6
align(1,i)=[];
thanks in advance

Best 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'; ...
'fg--ds--h-ty'];
for i=12:-1:1
if align(1,i)=='-' & align(2,i)=='-'
align(:,i) = [];
end
end
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, :)=='-') = [];