Azzera filtri
Azzera filtri

How can I use strcmp (or something else) to compare to multiple strings at once?

65 visualizzazioni (ultimi 30 giorni)
I want to compare all the strings in a cell (messages) to a list of string and return true if it matches any of them.
It should match the function of:
trialSplitPoints = find(strcmp('TRIALID 1',messages));
but for 'TRIALID 1' through 'TRIALID 8'.

Risposta accettata

Titus Edelhofer
Titus Edelhofer il 18 Set 2015
Hi,
apart from Walter's method there are two additional:
  1. Use regexp (unfortunately here I can't help, I've never really understood regular expressions)
  2. You can do partial comparison:
trialSplitPoints = find(strncmp(messages, 'TRIALID', length('TRIALID')));
Note, it's strncmp, not strcmp.
Titus

Più risposte (1)

Walter Roberson
Walter Roberson il 18 Set 2015
tf = ismember(messages, {'TRIALID 1', 'TRIALID 2', 'TRIALID 3', ... 'TRIALID 8'})
tf will be an array the same shape as "messages", indicating for each cell array element whether it matches any of the listed items.

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by