How to distinguish two similar but different strings?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a function that sorts files based on file name. Two of the categories are 'Z7PR' and 'Z7PR1', which are two different tests. However, my sorting program sorts 'Z7PR' into 'Z7PR1', because it seems to match the 'Z7PR'. Is there anyway to distinguish the two without using 'Z7PR_' as I am currently doing?
Here is my code:
function otpS = filesort(inpS)
C = {'Z1P','Z2P','Z1G','Z2G','Z3K','Z4K','Z5K','Z3M','Z4M','Z5M','Z6MS','Z7PR_','Z7PR1'}; %files to find
N = {inpS.name}; %initializes structure
otpS = struct(); %initializes structure
for k = 1:numel(C)
idx = cellfun('isempty',regexp(N,C{k},'once')); %finds matching strings
otpS.(C{k}) = N(~idx); %organizes files based on C
end
end
댓글 수: 2
답변 (1개)
Stephen23
2017년 6월 28일
편집: Stephen23
2017년 6월 28일
Some ideas:
- use strcmp instead of regexp.
- add ^ and $ to your regular expression.
- split the strings and use sortrows.
- calculate the edit-distance, e.g.: https://www.mathworks.com/matlabcentral/answers/318477-find-the-most-similar-word-from-a-set-of-words#answer_248951
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!