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

2 ビュー (過去 30 日間)
Ara
Ara 2015 年 1 月 7 日
コメント済み: dpb 2015 年 1 月 8 日
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 件のコメント
Image Analyst
Image Analyst 2015 年 1 月 7 日
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?
Ara
Ara 2015 年 1 月 8 日
Thank you for your answer.

サインインしてコメントする。

採用された回答

dpb
dpb 2015 年 1 月 7 日
編集済み: dpb 2015 年 1 月 7 日
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 件のコメント
Ara
Ara 2015 年 1 月 8 日
編集済み: Ara 2015 年 1 月 8 日
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 2015 年 1 月 8 日
I'd caution you to look at the above ADDENDUM results carefully before relying too heavily on the routine as it stands...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by