Code covered by the BSD License  

Highlights from
mgrep

4.75

4.8 | 4 ratings Rate this file 10 Downloads (last 30 days) File Size: 5.14 KB File ID: #9594

mgrep

by John D'Errico

 

11 Jan 2006 (Updated 21 Feb 2008)

Returns the names of all m-files in a directory or directory tree that contain a search string.

| Watch this File

File Information
Description

Long ago, I found that I quite liked Brett Shoelson's function mfilegrep, but back then it had a bug in it. I found the bug and fixed it, so then I decided to update the interface. I really wanted a command form version for my own use.

The guts of mgrep are still all very much thanks to Brett though (but with many changes.) Compared to the current version of mfilegrep, mgrep is a little faster, perhaps a 25% improvement in speed for long searches.

The interface to mgrep is a property/value pair interface. Its all fully functional as a command or as a function call. Note that property names can be shortened to a single character, and can be provided in any order.

Directories may be specified in either an absolute form, with the complete path, or as a relative path starting from some directory.

Some example uses:

Recursive search for the string 'fmin', starting in the current directory, returning the result as a cell array. Any of these 3 alternatives will work, due to the use of defaults for every parameter but the search string.

   filelist = mgrep('fmin');
   filelist = mgrep('fmin','.');
   filelist = mgrep('fmin',pwd,'recurse','on');

Simple search of the directory './mylocaldir' for the string 'fmin', display the result in the command window. Do not search subdirectories.

mgrep fmin ./mylocaldir recurse off

Case sensitive search for the name "John D'Errico", through all subdirectories beginning with the current directory. Display the results in the command window.

mgrep 'John D''Errico' . c on r on

Acknowledgements

Mfilegrep inspired this file.

This file inspired Find 'Orphaned' M Files and Toward A Program Development And Documentation Toolbox.

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
10 Nov 2010 Han Oostdijk  
10 Nov 2010 Han Oostdijk

Indeed, this works good. However the name suggest it works with grep / regexp. And this is not the case: findstr is used.

After replacing the findstr line with a regexp variant the function did exactly what I wanted.

Thanks John!

25 Aug 2010 Christos Saragiotis  
23 Nov 2009 Kevin Shaw

Awesome! It works as advertised.
It allowed me to search through a deep .m file hierarchy and find a dreadfully annoying error message string in an unknown file (added by a colleague) and to remove it. Yeah!

27 Jan 2006 John D'Errico

Intriguingly, this comment by "blacke jonhston" was made by someone who apparently had not actually downloaded this software. It appears to be someone I've upset.

13 Jan 2006 John D'Errico

Sadly, the use of textread here gave only a 5% speedup

12 Jan 2006 urs (us) schwarz

these days one might consider replacing the slow engine
fid3=fopen(...)
line=fgets(fid3)
with
lines=textread(...)
for i=1:numel(lines)
line=lines{i};
just a thought
us

Updates
13 Jan 2006

Profile showed where a 90% speed gain can be achieved over the first release of mgrep.

16 Jan 2006

Includes a new release of parse_pv_pairs, plus some mlint recommendations were followed.

20 Feb 2008

Bug fixed

21 Feb 2008

Discussions with Brett found a few new ways to improve the code.

Contact us