Can I use stringmatch, if I am looking for a match at the end of a string??

1 回表示 (過去 30 日間)
Ines
Ines 2012 年 4 月 3 日
Hello everybody, I have a dataset of strings with a structure 'product_reaction_parameter' and I want to check whether a specified string (e.g. 'parameter2' is contained in the string, how can I do that? Strmatch only looks for matches at the beginning of the string but I wanna look for matches in the middle or at least in the end...the output I need is the row index as it is obtained when using conventional strmatch.
Would really appreciate any kind of advice, I am a Chemist and thus not very well-trained in programming :)

採用された回答

Geoff
Geoff 2012 年 4 月 3 日
You should use regular expressions for this.
doc regexp
They are far more powerful than simple string matching, but you can use them for that purpose:
idx = regexp(s, 'parameter2$');
if ~isempty(idx)
disp('Found parameter2');
end
The $ matches at the end of the string.
You can also use regexp to split your string up into its parts:
tokens = regexp(s,'(.*)_(.*)_(.*)', 'tokens')
And then use straight string comparison on the third token.
  1 件のコメント
Geoff
Geoff 2012 年 4 月 3 日
This will also work on a cell-array of strings, matching each row. It returns an array of indices for the position where the string was matched. To turn that into a row index into your cell, you do this:
rows = find(cellfun(@(x) ~isempty(x), regexp(s, 'parameter2$')));

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

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