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?

11 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by