Is it possible to compare 2 strings only some of the same alphabet not the whole?

1 view (last 30 days)
If I have 2 variables such as a = ' term'; and b ='computer'; and ter is the same.
Is it possible to compare and turn the true ? I want to compare like query data with 'LIKE' command.
Thank you. Sorry for not good in English.
  3 Comments
Image Analyst
Image Analyst on 7 Jan 2015
It's not just ter that is a substring that is present, there are lots of substrings present. Do you also want it to return starting locations of all places where t, e, r, and te occur?

Sign in to comment.

Accepted Answer

dpb
dpb on 7 Jan 2015
Edited: dpb on 7 Jan 2015
Base Matlab doesn't have the pattern matching with the general substring search for any substring within the two without specifically specifying the substring length and working thru the two, no.
There's always the File Exchange (see link at top of page) for user-contributed submissions that's worth a look.
Indeed, a look-see found--
I didn't like the name much so renamed the m-file to LCSTR.M. If it were mine I'd also rearrange the outputs so the default, first output would be the string itself, but I suppose the author had other uses in mind when chose the interface.
Anyway, for your example,
>> [o1,o2,o3]=lcstr(a,b)
o1 =
0.6000
o2 =
3
o3 =
ter
>>
NB: This function returns only a single matching string of the longest number of characters between the two input strings. If there are more than one of the same length or others of shorter length, those won't be found.
I didn't see a more generic function within File Exchange submittals in a fairly quick perusal; there may be something else if this isn't precisely what you want. Or, of course, use this as a starting point to "roll your own".
PS: I'd think there would be a way with regexp to do this but I'm so feeble with writing regular expressions I'm not even going to try...
ADDENDUM
For the answer to the specific question asked, something like
lcstr(a,b)~=0;
will return a logical, but...
It appears this routine is somewhat buggy; I tried
lcstr(fliplr(a),b)
just to see what it would do for a failed match and discovered it thinks 'mt' "matches". I didn't dig into seeing why, precisely, but looks like you'll need to do some serious testing rather than just dropping this into your application.
  2 Comments
Ara
Ara on 8 Jan 2015
Edited: Ara on 8 Jan 2015
Oh, this code is very helpful.
>> [o1,o2,o3]=lcstr(a,b)
o1 =
0.6000
o2 =
3
o3 =
ter
>>
I will apply it to my application. I'm really thank to you. It's very important to me. Thank you very much.
dpb
dpb on 8 Jan 2015
I'd caution you to look at the above ADDENDUM results carefully before relying too heavily on the routine as it stands...

Sign in to comment.

More Answers (0)

Categories

Find more on Variables 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!