MATLAB: How to compare two strings with different size/length in MatLab

compare strings

I have two strings with different length, say "Tiredness" and "Tired", I need to count the common letters between them?

Best Answer

E.g., assuming you just want to count total number of characters that are exactly the same:
s1 = a string
s2 = another string
n = min(numel(s1),numel(s2));
result = sum(s1(1:n)==s2(1:n));
If you want to stop counting when the first difference is found:
result = sum(cumprod(s1(1:n)==s2(1:n)));
And if case doesn't matter, e.g.,
result = sum(cumprod(upper(s1(1:n))==upper(s2(1:n))));