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
Stephen23 2018 年 9 月 24 日
編集済み: Stephen23 2018 年 9 月 24 日
This code is confusing:
filenames = dir(fullfile(image_folder, 'Flat.tif'));
total_images = numel(filenames);
full_name = filenames.name;
Apparently dir is used to match one file. But then on the next line you count the number of files! Why? And if you expect multiple files, then that third line is definitely going to cause some problems when it discards all of the filenames except the first one...
"I want to use conditional statement to compare the string name using wild card."
String comparisons do not have a wildcard. You might be able to use dir (which does include a wildcard) or regular expressions (powerful but more complex), but it is not clear exactly what you are trying to compare and what output you expect.
hendra kurnia febriawan
hendra kurnia febriawan 2018 年 9 月 24 日
Sorry my question is not clear, I have revised in the below answer
Stephen23
Stephen23 2018 年 9 月 24 日
hendra kurnia febriawan's "Answer" moved here:
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;

サインインしてコメントする。

 採用された回答

Steven Lord
Steven Lord 2018 年 9 月 24 日

0 投票

Use startsWith to determine if the file name starts with "Tree" or "Flat".

1 件のコメント

hendra kurnia febriawan
hendra kurnia febriawan 2018 年 9 月 24 日
Yeah that works Steven, thank you
if startsWith(full_name,'Tree')
disp('Tree')
else
disp('Flat')
end
Can it be modified to choose not only two parameters tree and flat but also third paramater?
if startsWith(full_name,'Tree')
disp('Tree')
elseif startsWith(full_name,'Tree')
disp('Flat')
else
disp('rock')
end
Like that?or must use other codes?

サインインしてコメントする。

その他の回答 (2 件)

ANKUR KUMAR
ANKUR KUMAR 2018 年 9 月 24 日

2 投票

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
Dennis
Dennis 2018 年 9 月 24 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by