how to query string exactly in matlab

조회 수: 1 (최근 30일)
peter huang
peter huang 2022년 9월 1일
댓글: Rik 2022년 9월 1일
I have a composition table. If I enter matlab, I will get a cell of n*1
e.g. file attached
If I finish reading matlab
I want to find the location of M2 how can I find it
I have tried using
Index = find(contains(str,'M2'));
But this will output all the components that contain the M2 name
I want to output only the position of an M2

채택된 답변

Stephen23
Stephen23 2022년 9월 1일
편집: Stephen23 2022년 9월 1일
C = readcell('TEST.xlsx')
C = 6×1 cell array
{'M2' } {'SM2'} {'M23'} {'S1' } {'O1' } {'K1' }
X = matches(C,'M2')
X = 6×1 logical array
1 0 0 0 0 0
or
X = strcmp(C,'M2')
X = 6×1 logical array
1 0 0 0 0 0
TIP for the future: scroll to the bottom of the CONTAINS documentation page, you will find a list of related functions, including MATCHES and many others. Browsing the MATLAB help is a very useful skill.
  댓글 수: 1
Rik
Rik 2022년 9월 1일
Another related function: ismember, which is mostly similar in this context to the matches function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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