How to check if particular string exists within a file path (string)

Hi, I am stuck up with a simple query wherein I want to check if particular string exits in a given string. i.e. If I am using input image from following file path C:\New Folder\Level1\Image1.tiff Now I want to check if a string "Level1" exists in a given file path name. I want to know only its existence & not its length, position or occurrence. Simply in the form of true or false (o or 1). Can someone please help me with this. Thanks in advance for your help!

 채택된 답변

Image Analyst
Image Analyst 2013년 2월 3일
편집: Image Analyst 2013년 2월 3일
Look at these examples, and pick whichever one of the methods suits you best:
s = 'C:\New Folder\Level1\Image1.tiff'
template = 'Level1' % Cases match
foundIt = any(ismember(s, template))
index = strfind(s, template)
foundIt = ~isempty(index)
template = 'level1' % Case mismatch.
index = strfind(s, template)
foundIt = ~isempty(index)
template = 'level1' % Case mismatch.
index = strfind(upper(s), upper(template))
foundIt = ~isempty(index)

댓글 수: 4

Good one! Seems that I am very close to the solution. Just a small issue which I am experiencing. Following is part of my code (Suppose strfind = 'C:\New Folder\Level1\Image1.tiff')
template = 'Level1'; % Cases match
index = strfind(srcFiles, template);
A = ~isempty(index);
if A = 0;
s1x=142; s1y=122; s2x=190; s2y=220;
else template = 'Level2'; % Cases match
index = strfind(srcFiles, template);
A = ~isempty(index); if A = 0
s1x=142; s1y=122; s2x=190; s2y=220; end end
It is giving me an error while checking for A = 0; the error says "Parse error at "=": usage might be invalid MATLAB syntax." Simply I want to use above logic for 8 times to check 8 levels. Am I missing something while checking A = 0?
You want
if A == 0
Notice the double equal sign for checking equality.
And the semicolon at the end of an "if" statement is ignored.
Thanks a million! I missed that one in a haste!

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

추가 답변 (0개)

카테고리

도움말 센터File 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