How can I implement wildcard at ismember function of matlab?

北城以北 提交于 2019-12-12 09:28:17

问题


How can I do the implementation doing this in matlab;

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

回答1:


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



回答2:


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...



来源:https://stackoverflow.com/questions/18174133/how-can-i-implement-wildcard-at-ismember-function-of-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!