-1

How can I do the implementation doing this in matlab;

ismember(file_names,['*.mp4'])
erogol
  • 13,156
  • 33
  • 101
  • 155
  • Are you getting `file_names` with `dir()`? – Oleg Aug 11 '13 at 17:02
  • possible duplicate of [How to check a pattern in a string in matlab?](http://stackoverflow.com/questions/17241603/how-to-check-a-pattern-in-a-string-in-matlab) – Eitan T Aug 11 '13 at 17:53

2 Answers2

5

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
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    The dot (`.`) in `regexp` will be interpreted as any character, you should escape it: `\.`. – Eitan T Aug 11 '13 at 16:39
  • 1
    No problem. Also note that curiously `cellfun('isempty', ...)` is much faster than `cellfun(@isempty, ...)` (see details [here](http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/)). – Eitan T Aug 11 '13 at 16:42
3

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

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 1
    Note that there's also the `regexptranslate` function that converts wildcard-patterns to regular expressions. – sebastian Aug 12 '13 at 08:26