1

I want to check if a specific pattern was in a string to do some action

[filename pathname]=uigetfile
fullpath=[pathname filename]

In my program I will only browse for Pictures , all pictures are named with that pattern

*_cam1.jpg , *_cam1.jpg , *_cam2.jpg *_cam2.jpg , *_cam3.jpg

What I want to do to check if the image ends with cam1 then do some logic

if (filename.contain(cam1)
 then imread('1.jpg')
elseif (filename.contain(cam2)
 then imread ('2.jpg)

I know that there no method called 'contain' in matlab but this is an example to demonstrate what I want.

4

2 回答 2

3

You can select a folder and then import only those pictures which match a pattern by using directly dir() and wildcard *:

dir('C:\Users\username\Desktop\folder\*_cam*.jpg')
于 2013-06-21T19:01:50.780 回答
2

For more complex searches you can use regular expressions, but in this simple case a string comparison is enough.

% Let the user choose only files that end in .jpg
[filename pathname]=uigetfile('*.jpg');
% Use fullfile to join file parts! It is OS independent. 
fullpath=fullfile(pathname, filename);

if length(filename) > 8 && strcmp(filename(end-8:end),'_cam1.jpg')
    stuff = imread(fullpath);
    ...
elseif length(filename) > 8 && strcmp(filename(end-8:end),'_cam2.jpg')
    stuff = imread(fullpath);
    ...
end

It is not the most glamorous code, but it should get the job done.

于 2013-06-21T21:32:24.307 回答