Spooferman
Spooferman

Reputation: 353

How to extract specific files from a list of files in MATLAB

I have a cell array named list which contain a list of files. I would like to extract only specific files from that list and store them in the same array. Here is what I am trying to do:

function [ varargout ] = myFiles( varargin )

list = {'test.m' '.' '..' 'test1.m' 'test2.txt'};

list = strmatch('*.m', list)

end

My final list should be like as below, but yeah the indices should suffice.

list = test.m  test1.m

I am using MATLAB on Windows. Using the pattern in double quotes throws an error. Tried using \\'s also as escape sequence for (.) and/or (*) too.

Upvotes: 2

Views: 130

Answers (2)

npinti
npinti

Reputation: 52185

According to here you would need something like so: .*\.m$. The problem is that unlike the usual search in windows, the * means 0 or more repetitions of was preceds it, so doing it on its own will not work.

In this case, I am saying match any character (the . means any single character) 0 or more times which is then followed by a .m at the end. The $ specifies that the match must end at the end of the string.

Upvotes: 0

user57368
user57368

Reputation: 5765

strmatch is for finding strings that start with a given prefix. Regular expressions aren't supported. For that you want regexp, which takes its arguments in the opposite order. Alternatively, if you reverse all your strings, you can use strmatch('m.',list) and then reverse the results again to recover the filenames.

Upvotes: 1

Related Questions