How to find more than one string using find - strcmp?
조회 수: 15 (최근 30일)
이전 댓글 표시
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
채택된 답변
Benjamin Kraus
2024년 2월 6일
편집: Benjamin Kraus
2024년 2월 6일
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
index = find(codes == "stm+" | codes == "stm-")
Option 2
index = find(ismember(codes,{'stm+','stm-'}))
Option 3
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-"
index = find(matches(codes, pat))
Option 4
This is just a variant of the previous example.
pat = "stm" + ("+" | "-")
index = find(matches(codes, pat))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Embedded Coder Support Package for STMicroelectronics STM32 Processors에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!