Thursday, March 31, 2011

Automating file operations in matlab excluding the directories

Automating the work in directory with tree structure is fun. You can leave it overnight and get the output the next day.
However, hidden files/folders can be troublesome.
Specially these:
.
..
and .DS_Store (in mac)
or other set up stuffs

for jj = 1:length(fileList)
if fileList(jj).isdir
% do not operate the file
else
% call the function to operate on it * see footnote

end
%% footnote:

fileTARGET = '/Users/.../monthly';
saveTARGET ='/Users/.../figuresSAVE'
% Do the regular stuff
cd(fileTARGET)
% Do the regular stuff: train
fileLIST = dir;
N = length(fileLIST); % fileLIST.name
fn = fileLIST(4).name;
% fn='asdf.ext';
disp(fn)
trainME(fn) % train the network: takes data and trains
%save the figures here
cd(saveTARGET);
% savefile = fn;
save('savetrained.mat', '-mat', 'net'); % save the network... JIC
% cd(fileTARGET);
% cd(saveTARGET); instead, we can feed the folder structure into function making the code more general see below:
saveFIG(fileLIST, fileTARGET, saveTARGET, net); 

% save the plotted figures; as the target directories and save target directories are taken as input, it is more general for its usages


%%%%%%%%%%

I have also found a better way of working on it:



for jj = 1:length(fileLIST)
fn = fileLIST(jj).name;
[pathstr, name, ext] = fileparts(fn);
if strcmp(ext, '.hdf')
% do not operate the file
% else
disp(['Working on : ', fn])
end
end

No comments: