Conditional with string wildcard
조회 수: 28 (최근 30일)
이전 댓글 표시
hendra kurnia febriawan
2018년 9월 24일
댓글: hendra kurnia febriawan
2018년 9월 24일
Hi all,
I want to use conditional statement to compare the string name using wild card.
image_folder = 'C:\Users';
filenames = dir(fullfile(image_folder, 'Flat.tif'));
total_images = numel(filenames);
full_name = filenames.name;
disp(full_name);
Text1 = 'Tree'*;
Text2 = 'Flat'*;
if strcmp(full_name,Text2)
disp('OK')
else
strcmp(full_name,Text1)
disp('Not OK')
end
But it always comes with the second text "Not OK"
Anyone can help me with that? Thank you in advance
댓글 수: 3
Stephen23
2018년 9월 24일
Thanks for the comments. I have many image files in that folder with the name Tree1, Tree2,... and Flat1, Flat2,... and want to use loop that read each image and then using if 'the name Tree' then executing some codes else 'the name is Flat' then executing another code, that's why I am using *, so not detecting the exact file's name. The code should be
filenames = dir(fullfile(image_folder, '*.tif'));
total_images = numel(filenames);
full_name = filenames.name;
채택된 답변
추가 답변 (2개)
ANKUR KUMAR
2018년 9월 24일
I have taken names as filename.
full_name='Tree'
Text2 = 'Tree'; %here is you mistake. Remove * from the last
Text1 = 'Flat'; %here is you mistake. Remove * from the last
if strcmp(full_name,Text2)
disp('OK')
else
strcmp(full_name,Text1)
disp('Not OK')
end
댓글 수: 0
Dennis
2018년 9월 24일
You should get a warning that
Text1 ='Tree'*;
is no valid syntax (the * is a problem). If you want to use * as a wildcard try regexp. If the wildcard is always only the ending you could use strncmp:
image_folder = 'C:\Users';
filenames = dir(fullfile(image_folder, 'Flat.tif'));
total_images = numel(filenames);
full_name = filenames.name;
disp(full_name);
Text1 = 'Tree';
Text2 = 'Flat';
if strncmp(full_name,Text2,length(Text2))
disp('OK')
else
strncmp(full_name,Text1,length(Text1))
disp('Not OK')
end
댓글 수: 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!