How do I get and index to a row (comparing a string to a column of the table) in a table as I could with strmatch?

16 ビュー (過去 30 日間)
Ronald Ouwerkerk
Ronald Ouwerkerk 2017 年 10 月 17 日
回答済み: Jan 2017 年 10 月 20 日
We are supposed to replace strmatch because is becomes obsolete. But I currently use it to find the row index in a table where a unique ID identifies the row I want to modify:
>>rowidx = strmatch( '7890808.20180808',MRSresultsTable.Spectro_Study_ID)
rowidx =
1
2
Now with the suggested replacement of strmatch this does not work:
>> rowidx = strncmp( '7620408.20170130',MRSresultsTable.Spectro_Study_ID, 16)
rowdy =
[]
  1 件のコメント
Duncan Lilley
Duncan Lilley 2017 年 10 月 20 日
Hello,
It is worth noting that all of the recommend replacements for "strmatch" function slightly differently than "strmatch" itself. The "strmatch" function determines the indices of the second parameter which begin with the first parameter. If you intend to check if an ID matches exactly, then I would recommend using "strcmp".
It is also important to note that "strcmp" and "strncmp" return different outputs than "strmatch". Both "strcmp" and "strncmp" return a logical array, where a 1 indicates a match for that index and a 0 indicates no match. You can use "find" to get the indices of the strings that match.
Here are a few questions I have about your question:
  • What is "rowdy"? It appears that you are saving the results of "strncmp" to a different variable ("rowidx") than you are displaying.
  • You are using a different string in each of the examples your posted ('7890808.20180808' compared to '7620408.20170130'). Does the second string exist within your table?

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

回答 (1 件)

Jan
Jan 2017 年 10 月 20 日
The replacement of
rowidx = strmatch('7890808.20180808', MRSresultsTable.Spectro_Study_ID)
by
rowidx = find(strncmp('7890808.20180808', MRSresultsTable.Spectro_Study_ID, 16)
is valid and give the same results.
The output "rowdy = []" must be something else, because strncmp replies an empty vector only, if one of the inputs is empty also.

カテゴリ

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