How can I implement wildcard at ismember function

2019-04-17 15:50发布

How can I do the implementation doing this in matlab;

ismember(file_names,['*.mp4'])

2条回答
beautiful°
2楼-- · 2019-04-17 16:40

I would do that with regexp, like this:

result = ~cellfun(@isempty,(regexp(file_names,'\.mp4$')));

For example,

file_names = {'aaa.mp4','bbb.mp3'};

gives

result =

     1     0
查看更多
Evening l夕情丶
3楼-- · 2019-04-17 16:43

Using regular expressions (regexp)

This can be easily achieved with regexp:

tf = ~cellfun('isempty', regexp(file_names, '.*\.mp4'));

If you want to force the pattern matching to the beginning or the end of the filename, you should add a caret (^) or a dollar sign ($) respectively, for instance:

%// Match pattern at the beginning of the filename
tf = ~cellfun('isempty', regexp(file_names, '^.*\.mp4'));

%// Match pattern at the end of the filename
tf = ~cellfun('isempty', regexp(file_names, '\.mp4$'));

Alternative method (strfind)

If your search pattern is simple enough, you can use strfind instead:

tf = ~cellfun('isempty', strfind(file_names, '.mp4'));

Note that this method does not allow you to search for more complicated patterns, nor check conditions (trivially) such as the appearance of the pattern at the end of the string...

查看更多
登录 后发表回答