MATLAB Answers

0

Why it said that "Matrix index is out of range for deletion"

Asked by Xuejian Sun on 17 Nov 2019 at 4:29
Latest activity Answered by Walter Roberson
on 17 Nov 2019 at 5:48
I already comine the string,it should not be a matrix anymore,but the commond window said "Matrix index is out of range for deletion".
first function is below:
function [strOut] = JoinName(strFirst, strLast)
Strin=strcat(strFirst,"_",strLast);
strOut= RemoveDash(Strin);
end
second function is below:
%Xuejian Sun,Lab7,11/12/2019
%write two function,the first is to determine whether string has dash and
%the second one is to remove dash
%clear windows,variables and figures
function [strOut]= RemoveDash(Strin)
Find=strfind(Strin,'-');
if isempty(Find)==1
strOut=Strin;
else
Strin(Find)=[];
strOut=Strin;
end
end

  1 Comment

Can you provide input strings for the function JoinName used in the error case?

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 17 Nov 2019 at 5:48

Strin=strcat(strFirst,"_",strLast);
Because you used "_" the result you get is going to be a string object, not a character vector.
Find=strfind(Strin,'-');
strfind() returns positions inside the scalar string object
Strin(Find)=[];
Positions inside a scalar string object are not the same thing as indices into the string object. A scalar string object has index 1 only -- the index for a string object is which complete string you are accessing. Like ["hello", "bye"] has indices 1 for "hello" and 2 for "bye".
Strin{1}(Find)=[]; %change the inside of the string

  0 Comments

Sign in to comment.