Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Can you use DIR to list files in subfolders ?

Asked by John on 12 Mar 2012

Hello,

Would somebody be able to advise me on how to use DIR to find the dates that each file was last modified with a .mat extension in all sub folders in current directory?

This code does not look in sub directories. Also how to you specify the .mat extension.

files = dir(datenum)

Many thanks

0 Comments

John

Tags

Products

No products are associated with this question.

3 Answers

Answer by Walter Roberson on 12 Mar 2012
Accepted answer

You cannot do that in a single dir() call.

You need one dir() call on the current folder, and you look at the isdir() field of the results to see which names correspond to folders:

dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = [];  %remove non-directories

Then do a step to remove the folder names "." and ".." so you do not infinite loop.

Then you loop over all those names and look inside each of the designated folders:

subdirinfo = cell(length(dirinfo));
for K = 1 : length(dirinfo)
  thisdir = dirinfo(K).name;
  subdirinfo{K} = dir(fullfile(thisdir, '*.mat'));
end

Now subdirinfo{K} is the structure of information about the .mat files in the directory dirinfo(K).name

2 Comments

John on 12 Mar 2012

Hello Walter,

Thank you for your reply.

Does the structure contain the date the file was last modified? The subfolders all contain a file called data.mat. It is these files that I plan to use the SORT function on to sort them by file name and save them as file1, file2...file1000.xls.
Many thanks
John

Walter Roberson on 13 Mar 2012

There is a field named datenum in the structure, which is a serial date number (numeric value) corresponding to the last modified date.

Walter Roberson
Answer by Walter Roberson on 13 Mar 2012
filetofind = 'data.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = [];  %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = [];  %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
  subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
  if ~isempty(subdirinfo)
    lastmod(K) = subdirinfo(1).datenum;
  end
end
[sortedmod, sortorder] = sort(lastmod);
sordorder(~isfinite(sortedmod)) = [];  %directories without data.mat
for K = 1 : length(sortorder)
  thisdirnum = sortorder(K);
  thisoutname = sprintf('file%d.xls', K);
  %copy from the subdirectory in to a sequentially named .xls file
  copy( fullfile( dirinfo(thisdirnum).name, filetofind ), thisoutname );
end

15 Comments

John on 22 May 2012

Hi Walter,

The variables are vectors of different sizes. The first variable is n X 1 and the the second variable is m x 2. So this means I need two xlswrite() calls. Is this correct? - Do I need two lines for "thisdata =" and "fprint" also?

Thanks for your help

John

filetofind = 'data.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
if ~isempty(subdirinfo)
lastmod(K) = subdirinfo(1).datenum;
end
end
[sortedmod, sortorder] = sort(lastmod);
sordorder(~isfinite(sortedmod)) = []; %directories without data.mat
vartofind = 'ess_plant_energy_in_total_simu';
vartofind1 = 'sch_cycle';
for K = 1 : length(sortorder)
thisdirnum = sortorder(K);
thisdirname = dirinfo(thisdirnum).name;
thisoutname = sprintf('file%d.xls', K);
try
thisdata = load( fullfile(thisdirname,filetofind), vartofind);
thisdata1 = load( fullfile(thisdirname,filetofind), vartofind1);
xlswrite( thisoutname, thisdata.(vartofind) );
xlswrite( thisoutname, thisdata1.(vartofind1) );
catch
fprintf(2, 'File "%s/%s" does not have variable "%s\n", thisdirname, filetofind, vartofind);
fprintf(2, 'File "%s/%s" does not have variable "%s\n", thisdirname, filetofind, vartofind1);
end
end

Walter Roberson on 22 May 2012

You can combine the load() into a single statement:

thisdata = load( fullfile(thisdirname,filetofind), vartofind, vartofind1 );
xlswrite( thisoutname, thisdata.(vartofind) );
xlswrite( thisoutname, thisdata.(vartofind1) );

However, you need to either write to different file names or else use range specifications on the xlswrite() -- otherwise the data from the second xlswrite() will overwrite the first.

John on 22 May 2012

Thanks very much for your help. I got it working.

Regards

John

Walter Roberson
Answer by Frederic Moisy on 12 Mar 2012

1 Comment

John on 12 Mar 2012

Hello Frederic,

Would you mind helping me code this for my application. I've been trying for hours with no luck.

Thank you

John

Frederic Moisy

Contact us