フィルターのクリア

find a word in sentence by "strfind"

7 ビュー (過去 30 日間)
Amr Hashem
Amr Hashem 2015 年 5 月 18 日
コメント済み: Amr Hashem 2015 年 5 月 19 日
i want to search in a cell by a word using "strfind" and if it exist return 1 if not return 0
this is the rows i search in:
i am using now "strcmpi" as: % it returns 1 or 0 but it search only for the same word % strfind: search for any word containing this letters%
if strcmpi(alldata{i,6},'battery') % search for word "battery" in 6th column
how i can do it by "strfind" ?
note : i split the sentence into words for easy search so i want by the end to sum the whole row if the sum =< 1 means that this row has the word i search for

採用された回答

Stephen23
Stephen23 2015 年 5 月 18 日
編集済み: Stephen23 2015 年 5 月 18 日
You can simply wrap the strfind inside isempty, like this:
>> ~isempty(strfind('Schroedingers cat is alive!','cat'))
ans =
1
You can also use regexp to quickly identify matching substrings. Here is a simple example that locates the string cat inside a cell array of strings.
>> A = {'the cat sat on the mat','the cat in the hat','four blind mice','cats!'};
>> ~cellfun('isempty',regexp(A,'cat'))
ans =
1 1 0 1
And if you want case-insensitive matching then use regexpi instead. Note that regexp also has many other powerful features that may be of interest to you... the documentation explains these, with examples too.
  1 件のコメント
Amr Hashem
Amr Hashem 2015 年 5 月 19 日
thank you

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

その他の回答 (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