【问题标题】:Matlab, Advanced loop in folders and applying a functionMatlab,文件夹中的高级循环并应用函数
【发布时间】:2012-01-27 21:03:58
【问题描述】:

我编写了一个分析视频文件的代码,然后在图表上绘制数据 将此图保存到 excel和jpg。

但我的问题是我在大约 20 个文件夹中有超过 200 个视频要分析,

所以我需要自动化此代码以在文件夹内循环并分析其中的每个 *.avi 文件和

.. 所以任何想法或建议

非常感谢您的帮助

我需要知道如何循环文件夹并在其中获取文件,并将函数应用于该文件夹中的这些文件

请注意,我还想将图形结果保存到 img 的函数,然后我应该在保存时包含完整路径吗?我该怎么做?

【问题讨论】:

标签: matlab loops file-io for-loop directory


【解决方案1】:

dirfullfile 命令是您所需要的。根据您的目录结构,如下所示:

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

【讨论】:

  • 矢量化测试可以使第一个循环更简洁:video_subdirs = video_dir_children([video_dir_children.isdir] & ~ismember({video_dir_children.name}, {'.','..'})).
  • 太好了:),还有一个问题,在我的视频处理中,我为每个视频结果“saveas(h,[b'.jpg']);”保存文件,但它不适用于循环,有什么办法解决吗?
  • 究竟是什么不工作?您可能需要使用fullfile 构造文件的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多