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

1 次查看(过去 30 天)
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 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by