|
| 1 | +%% RDIR Enhanced - Examples of use |
| 2 | +% |
| 3 | +% This script demonstrates how to use the different abilities of the |
| 4 | +% enhanced |rdir| function. |
| 5 | +% |
| 6 | +% Examples are based on |matlabroot| directory content. Results may vary |
| 7 | +% depending on your version of Matlab. |
| 8 | +% |
| 9 | + |
| 10 | +%% Standard use |
| 11 | +rdir([matlabroot, '\*.txt']) |
| 12 | + |
| 13 | +%% Using double wildcard ** |
| 14 | +% List |".m"| files whose name contains |"tmpl"| in all subdirectories of |
| 15 | +% |matlabroot| |
| 16 | +rdir([matlabroot, '\**\*tmpl*.m']) |
| 17 | + |
| 18 | +%% RDIR output |
| 19 | +d = rdir([matlabroot, '\**\*tmpl*.m']) |
| 20 | + |
| 21 | +%% |
| 22 | +disp(d(1)) |
| 23 | + |
| 24 | + |
| 25 | +%% Using 3rd argument to shorten output names |
| 26 | +% Remove |"C:\Program Files\"| in returned names |
| 27 | +rdir([matlabroot, '\*.txt'], '', 'C:\Program Files\') |
| 28 | + |
| 29 | +%% |
| 30 | +% Remove |matlabroot| in returned names |
| 31 | +rdir([matlabroot, '\*.txt'], '', true) |
| 32 | + |
| 33 | +%% |
| 34 | +% Optional 2nd |rdir| output indicates common path removed from each output |
| 35 | +% name |
| 36 | +[d, p] = rdir([matlabroot, '\*.txt'], '', true); |
| 37 | + |
| 38 | +fprintf('Common path : \n%s\n\n', p) |
| 39 | + |
| 40 | +disp( d(1) ) |
| 41 | + |
| 42 | +%% Using a filter with "regexp" |
| 43 | +% List |".mat"| files, then select those whose name match regular expression |
| 44 | +% |'data\d'| (ie |"data"| followed by a numeric digit) |
| 45 | +rdir([matlabroot '\toolbox\**\*.mat'], 'regexp(name, ''data\d'')', true) |
| 46 | + |
| 47 | +%% Using a function handle as filter |
| 48 | + |
| 49 | +fun = @(d) ~isempty(regexp(d.name, 'data\d')) && (d.bytes < 10*1024) |
| 50 | + |
| 51 | +rdir([matlabroot '\toolbox\**\*.mat'], fun, true) |
| 52 | + |
| 53 | +%% Specific display - No item matching filter |
| 54 | +% When some items match input path, but none match filter, a specific |
| 55 | +% message is displayed. |
| 56 | +rdir(matlabroot, 'strcmp(name, ''unknowtoolbox'')', 1) |
| 57 | + |
| 58 | + |
| 59 | +%% Specific display - Wrong filter |
| 60 | +% A warning is displayed after the non-filtered result list if entered |
| 61 | +% filter is wrong. |
| 62 | +rdir(matlabroot, 'wrong filter', 1) |
| 63 | + |
| 64 | + |
| 65 | +% EOF |
0 commit comments