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

1 view (last 30 days)
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
Bhaskar R
Bhaskar R on 17 Nov 2019
Can you provide input strings for the function JoinName used in the error case?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 17 Nov 2019
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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!