How to check a pattern in a string in matlab?

前端 未结 2 1318
陌清茗
陌清茗 2021-01-15 18:27

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

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

In my p

相关标签:
2条回答
  • 2021-01-15 19:11

    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')
    
    0 讨论(0)
  • 2021-01-15 19:18

    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.

    0 讨论(0)
提交回复
热议问题