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?

32 visualizzazioni (ultimi 30 giorni)
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 Commento
Duncan Lilley
Duncan Lilley il 20 Ott 2017
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?

Accedi per commentare.

Risposte (1)

Jan
Jan il 20 Ott 2017
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.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by