Find location of exact string

조회 수: 4 (최근 30일)
Tejashree Pawar
Tejashree Pawar 2021년 3월 11일
댓글: Tejashree Pawar 2021년 3월 12일
Using this line fo code to find "Sine Delta 2" in my xml file which also contains "Cosine Delta 2" and the code returns locaion for both instead of just for "Sine".
How do i return location of the exact string?
row_idx_SineDelta2 = find(~cellfun('isempty',strfind(data,'sine delta 2')))
  댓글 수: 1
Tejashree Pawar
Tejashree Pawar 2021년 3월 12일
Thanks for the reply! that worked

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

채택된 답변

Jorg Woehl
Jorg Woehl 2021년 3월 11일
편집: Jorg Woehl 2021년 3월 11일
regexp(data, '\<sine delta 2')
The \< indicates that the search string must occur at the beginning of a new word - see MATLAB regexp for the almost limitless possibilities for pattern matching.

추가 답변 (1개)

Jorg Woehl
Jorg Woehl 2021년 3월 11일
편집: Jorg Woehl 2021년 3월 11일
Starting with R2020b, you can use pattern with strfind, which allows you to only find matches if they are preceded by a nonletter character (i.e. if the search string starts a new word):
pat = letterBoundary + 'sine delta 2';
strfind(data, pat)
Note that this is case-sensitive; in your code you are searching for 'sine delta 2', but you mention 'Sine Delta 2' in your introduction...
  댓글 수: 2
Tejashree Pawar
Tejashree Pawar 2021년 3월 11일
Thanks! I am using Matlab2019b for this. Any command or function i can use with this version?
Jorg Woehl
Jorg Woehl 2021년 3월 11일
Yes, I'll add it as a separate answer...

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by