0

i wrote a code that analyze a video file and then plot data on a graph then save this graph into excel and jpg.

but my problem is that i have more than 200 video to analyze in around 20 folder,

so i need to automate this code to loop inside folders and analyze each *.avi file inside and

.. So any ideas or suggestions

Really appreciate your help

I need to know how to loop folders and get files inside and the apply a function to these files in that folder

Please note that my function that i also want to save graph result to img, should i then include full path while saving ? and how can i do it ?

Amro
  • 123,847
  • 25
  • 243
  • 454
Zalaboza
  • 8,899
  • 16
  • 77
  • 142
  • related question: [How to get all files under a specific directory in MATLAB?](http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab). Also there are many functions on FEX for recursively traversing directories that you might find useful.. This one for example: [Recursive directory listing](http://www.mathworks.com/matlabcentral/fileexchange/19550-recursive-directory-listing) – Amro Jan 29 '12 at 00:56

1 Answers1

2

The dir and fullfile commands are what you need. Depending on your directory structure, something like this:

video_dir = 'videos';

% I'm not sure if there's a way to directly get a list of directories, but
% this will work
video_dir_children = dir(video_dir);
video_subdirs = [];
for ix = 1 : length(video_dir_children),
  % note we're careful to kick out '.' and '..' 
  %   (and any other directory starting with a '.')
  if(video_dir_children(ix).isdir && video_dir_children(ix).name(1) ~= '.')
    video_subdirs = [video_subdirs; video_dir_children(ix)];
  end
end

for ix = 1 : length(video_subdirs),
  this_dir = fullfile(video_dir, video_subdirs(ix).name);
  avi_files_in_this_dir = dir(fullfile(this_dir, '*.avi'));

  for jx = 1 : avi_files_in_this_dir,
    doVideoProcessing(fullfile(this_dir, avi_files_in_this_dir(jx).name));
  end

end
dantswain
  • 5,427
  • 1
  • 29
  • 37
  • 1
    Vectorized test can make that first loop more concise: `video_subdirs = video_dir_children([video_dir_children.isdir] & ~ismember({video_dir_children.name}, {'.','..'}))`. – Andrew Janke Jan 27 '12 at 21:45
  • thats great :), just one more question,in my video processing i save files for each video result "saveas(h,[b '.jpg']);" , but its not working with loop , any way to solve ? – Zalaboza Jan 27 '12 at 22:21
  • What exactly isn't working? You may need to construct the path of the file using `fullfile`. – dantswain Jan 27 '12 at 22:23