如何让程序在matlab中打开给定文件夹的文件?

时间:2011-07-13 12:43:33

标签: matlab file-io

我正在尝试用matlab编写程序。 选择文件夹后,程序必须打开该文件夹中的每个文件, 它必须替换从用户选择文件夹的操作。 有谁能够帮我? 感谢

1 个答案:

答案 0 :(得分:2)

很难准确理解你想做什么。现在,我假设您希望用户选择目录,程序将对文件执行某些操作。

处理许多文件的最佳方法是使用内置Matlab函数dir()返回的目录结构。假设您在当前导演下有一个名为testdir的目录,其中有一些文本文件,您需要使用某些函数foo()进行操作(其中foo()是虚构函数),

% Get the folder name from the user
dirname = uigetdir(pwd);

% Get a directory structure of all text files in that directory
dirStruct = dir(fullfile(dirname,'*.txt'));

% Loop over all files using the directory structure calling the function foo 
% the name of the file
for k=1:length(dirStruct)
    foo(fullfile(dirname,dirStruct(k).name));
end

有关详细信息,请尝试查看uigetdiruigetfiledir的Matlab文档。