MATLAB: How to replace a segment of a string with another of different size

MATLABstring length errorstringstag replacement

I have a long string with a tag appearing a couple times in it (the tag is a specific set of characters) and I need to replace that tag with another string that has a different length than the tag. I am able to use strfind to find the indeces of the tags and replace them with a "replacement" variable when the replacement is the same size as the tag, but it gives me an error when the tag is smaller or bigger. I was wondering if there is a way of adding or taking away an element from the original string to then make the assignment I was doing without the string size problems. (The tag I have is a string with 1 less character compared to the replacement.)

Best Answer

Well, I was able to do it for my case. This code may be helpful if anyone needs to do something similar. What it does is it adds a space after each tag that is found on the string.
if length(isspace(tag)) ~= length(isspace(replacement))
for i = [1:size(FinalString,1)]
index = strfind(lower(FinalString{i}),lower(tag));
for j = [1:length(index)]
index = strfind(lower(FinalString{i}),lower(tag));
temp1 = FinalString{i}(1:index(j)+length(isspace(tag))-1);
temp2 = FinalString{i}((index(j)+length(isspace(tag))):end);
temp1 = temp1+" ";
FinalString{i} = char(strcat(temp1,temp2));
end
end
end