Prev: How to eliminate/replace values if accumulated sum in an arrayexceeds a certain limit
Next: Making vector global and local
From: us on 8 Apr 2010 13:58 "Thomas Ibbotson" <thomas.ibbotson(a)gmail.com> wrote in message <hpl4so$r6d$1(a)fred.mathworks.com>... > I am currently finding files in a directory using the following code: > re = 'some regular expression'; > f = dir('.'); > matchingFiles = f(cellfun(@(x) ~isempty(regexp(x, re, 'once')), {f.name})).name; > > Somehow I feel there must be a better way to do this. Does anyone have any suggestions? > > Tom recently, this - albeit only slightly different - solution has been proposed... http://www.mathworks.com/matlabcentral/newsreader/view_thread/278520#733158 us
From: Doug Schwarz on 8 Apr 2010 14:33 Thomas Ibbotson wrote: > I am currently finding files in a directory using the following code: > re = 'some regular expression'; > f = dir('.'); > matchingFiles = f(cellfun(@(x) ~isempty(regexp(x, re, 'once')), > {f.name})).name; > > Somehow I feel there must be a better way to do this. Does anyone have > any suggestions? > > Tom Slightly simpler: matchingFiles = f(~cellfun(@isempty,regexp({f.name},re,'once'))).name; I might break this up into two statements to make it easier to understand, but that's pretty much how I would do this task. -- Doug Schwarz dmschwarz&ieee,org Make obvious changes to get real email address.
From: Walter Roberson on 8 Apr 2010 17:18
Doug Schwarz wrote: > Thomas Ibbotson wrote: >> I am currently finding files in a directory using the following code: >> re = 'some regular expression'; >> f = dir('.'); >> matchingFiles = f(cellfun(@(x) ~isempty(regexp(x, re, 'once')), >> {f.name})).name; >> >> Somehow I feel there must be a better way to do this. Does anyone have >> any suggestions? >> >> Tom > > > Slightly simpler: > > matchingFiles = f(~cellfun(@isempty,regexp({f.name},re,'once'))).name; > > I might break this up into two statements to make it easier to > understand, but that's pretty much how I would do this task. You can get away without referencing f again if you prefix re with '^.*' and suffix it with '.*$' -- then the match would be against the complete string when a match occurred. You would, though, probably end up with empty cells for the non-matches, and would need to squeeze those out. |