MATLAB Examples

RDIR Enhanced - Examples of use

This script demonstrates how to use the different abilities of the enhanced rdir function.

Examples are based on matlabroot directory content. Results may vary depending on your version of Matlab.

Contents

Standard use

rdir([matlabroot, '\*.txt'])
  76 kb  02-Aug-2007 16:03:52  C:\Program Files\MATLAB\R2007b\license.txt                       
 631  b  02-Aug-2007 15:18:42  C:\Program Files\MATLAB\R2007b\patents.txt                       
 413  b  04-Aug-2007 12:16:36  C:\Program Files\MATLAB\R2007b\trademarks.txt                    

Using double wildcard **

List ".m" files whose name contains "tmpl" in all subdirectories of matlabroot

rdir([matlabroot, '\**\*tmpl*.m'])
  11 kb  20-Dec-2006 03:31:58  C:\Program Files\MATLAB\R2007b\toolbox\simulink\blocks\msfuntmpl.m 
   8 kb  20-Mar-2007 20:57:30  C:\Program Files\MATLAB\R2007b\toolbox\simulink\blocks\sfuntmpl.m 
 455  b  29-Jun-2006 12:23:38  C:\Program Files\MATLAB\R2007b\toolbox\simulink\blocks\ja\msfuntmpl.m 
   3 kb  29-Jun-2006 12:23:40  C:\Program Files\MATLAB\R2007b\toolbox\simulink\blocks\ja\sfuntmpl.m 

RDIR output

d = rdir([matlabroot, '\**\*tmpl*.m'])
d = 

4x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

disp(d(1))
       name: 'C:\Program Files\MATLAB\R2007b\toolbox\simulink\blocks\msfuntmpl.m'
       date: '20-déc.-2006 03:31:58'
      bytes: 11035
      isdir: 0
    datenum: 7.3303e+005

Using 3rd argument to shorten output names

Remove "C:\Program Files\" in returned names

rdir([matlabroot, '\*.txt'], '', 'C:\Program Files\')
All in : C:\Program Files\
  76 kb  02-Aug-2007 16:03:52  MATLAB\R2007b\license.txt                                        
 631  b  02-Aug-2007 15:18:42  MATLAB\R2007b\patents.txt                                        
 413  b  04-Aug-2007 12:16:36  MATLAB\R2007b\trademarks.txt                                     

Remove matlabroot in returned names

rdir([matlabroot, '\*.txt'], '', true)
All in : C:\Program Files\MATLAB\R2007b\
  76 kb  02-Aug-2007 16:03:52  license.txt                                                      
 631  b  02-Aug-2007 15:18:42  patents.txt                                                      
 413  b  04-Aug-2007 12:16:36  trademarks.txt                                                   

Optional 2nd rdir output indicates common path removed from each output name

[d, p] = rdir([matlabroot, '\*.txt'], '', true);

fprintf('Common path : \n%s\n\n', p)

disp( d(1) )
Common path : 
C:\Program Files\MATLAB\R2007b\

       name: 'license.txt'
       date: '02-août-2007 16:03:52'
      bytes: 77445
      isdir: 0
    datenum: 7.3326e+005

Using a filter with "regexp"

List ".mat" files, then select those whose name match regular expression 'data\d' (ie "data" followed by a numeric digit)

rdir([matlabroot '\toolbox\**\*.mat'], 'regexp(name, ''data\d'')', true)
All in : C:\Program Files\MATLAB\R2007b\toolbox\
   6 kb  01-Mar-2000 13:24:02  ident\ident\iddata1.mat                                          
   7 kb  01-Mar-2000 13:24:04  ident\ident\iddata2.mat                                          
   8 kb  01-Mar-2000 13:24:06  ident\ident\iddata3.mat                                          
   7 kb  01-Mar-2000 13:24:06  ident\ident\iddata4.mat                                          
   9 kb  01-Mar-2000 13:24:08  ident\ident\iddata5.mat                                          
   9 kb  01-Mar-2000 13:24:10  ident\ident\iddata6.mat                                          
  13 kb  01-Mar-2000 13:24:10  ident\ident\iddata7.mat                                          
  19 kb  01-Mar-2000 13:24:12  ident\ident\iddata8.mat                                          

Using a function handle as filter

fun = @(d) ~isempty(regexp(d.name, 'data\d')) && (d.bytes < 10*1024)

rdir([matlabroot '\toolbox\**\*.mat'], fun, true)
fun = 

    @(d)~isempty(regexp(d.name,'data\d'))&&(d.bytes<10*1024)

All in : C:\Program Files\MATLAB\R2007b\toolbox\
   6 kb  01-Mar-2000 13:24:02  ident\ident\iddata1.mat                                          
   7 kb  01-Mar-2000 13:24:04  ident\ident\iddata2.mat                                          
   8 kb  01-Mar-2000 13:24:06  ident\ident\iddata3.mat                                          
   7 kb  01-Mar-2000 13:24:06  ident\ident\iddata4.mat                                          
   9 kb  01-Mar-2000 13:24:08  ident\ident\iddata5.mat                                          
   9 kb  01-Mar-2000 13:24:10  ident\ident\iddata6.mat                                          

Specific display - No item matching filter

When some items match input path, but none match filter, a specific message is displayed.

rdir(matlabroot, 'strcmp(name, ''unknowtoolbox'')', 1)
No item matching filter.

Specific display - Wrong filter

A warning is displayed after the non-filtered result list if entered filter is wrong.

rdir(matlabroot, 'wrong filter', 1)


% EOF
All in : C:\Program Files\MATLAB\
                               R2007b\bin                                                      
                               R2007b\extern                                                   
                               R2007b\help                                                     
                               R2007b\ja                                                       
                               R2007b\java                                                     
                               R2007b\jhelp                                                    
                               R2007b\notebook                                                 
                               R2007b\rtw                                                      
                               R2007b\simulink                                                 
                               R2007b\stateflow                                                
                               R2007b\sys                                                      
                               R2007b\toolbox                                                  
                               R2007b\uninstall                                                
                               R2007b\work                                                     
 775  b  19-Jul-2012 15:20:40  R2007b\MATLAB R2007b.lnk                                         
  76 kb  02-Aug-2007 16:03:52  R2007b\license.txt                                               
 631  b  02-Aug-2007 15:18:42  R2007b\patents.txt                                               
 413  b  04-Aug-2007 12:16:36  R2007b\trademarks.txt                                            
Warning: Invalid TEST "wrong filter" : Error: Unexpected MATLAB expression.