# How to find more than one string using find - strcmp?

10 vues (au cours des 30 derniers jours)
Marco le 5 Fév 2024
Déplacé(e) : Stephen23 le 6 Fév 2024
I need to find the location in a structure of these two strings: 'stm+' and 'stm-'.
I am using the simple following line code:
index = find(strcmp({EEG.event.code}, 'stm+')==1)
However, this finds only one string (ie, 'stm+').
How can I find both of them ('stm+' and 'stm-') so that matlab returns the position of all the strings?
I tried
index = find(strcmp({EEG.event.code}, 'stm+', 'stm-')==1)
but it doesn't work.
It's important to mention that I'm not looking for true or false answer, but the location (row number).
Thanks a lot for your help!!!
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Benjamin Kraus le 6 Fév 2024
Modifié(e) : Benjamin Kraus le 6 Fév 2024
There are several options to accomplish this goal.
I'm assuming that the output from {EEG.event.code} is a cell-array of character vectors. So that my code below works, I'm going to hard-code a bunch of codes.
codes = {'notstm','abc','def','stm+','stm-','ghi','nope','jkl','stm-','mno','stm+','stmnot-','stmnot+'};
If I'm understanding correctly, your goal is to find the index of either 'stm+' or 'stm-' (which, in this case, should be 4,5, 9, and 11.
Option 1
This uses two separate calls to == and a boolean comparison (or).
index = find(codes == "stm+" | codes == "stm-")
index = 1×4
4 5 9 11
Option 2
This option uses ismember.
index = find(ismember(codes,{'stm+','stm-'}))
index = 1×4
4 5 9 11
Option 3
This option uses pattern expressions.
When you combine two scalar strings using the boolean "or" (|) operator, you get a pattern object that matches either string, then you can call matches to look for valid matches to your pattern.
Note that when you call matches you can specify whether to IgnoreCase or not.
pat = "stm+" | "stm-"
pat = pattern
Matching: "stm+" | "stm-"
index = find(matches(codes, pat))
index = 1×4
4 5 9 11
Option 4
This is just a variant of the previous example.
pat = "stm" + ("+" | "-")
pat = pattern
Matching: "stm" + ("+" | "-")
index = find(matches(codes, pat))
index = 1×4
4 5 9 11
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Marco le 6 Fév 2024
Déplacé(e) : Stephen23 le 6 Fév 2024
I managed to make it work in this way:
I have transformed the structure in a chararray with char function.
Then I used Option 2 posted by Benjamin Kraus.
Thanks a lot everyone!
Matt J le 6 Fév 2024
Déplacé(e) : Stephen23 le 6 Fév 2024
If so, you should accept-click @Benjamin Kraus' answer.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur STMicroelectronics Discovery Boards dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by