我正在尝试用matlab编写程序。 选择文件夹后,程序必须打开该文件夹中的每个文件, 它必须替换从用户选择文件夹的操作。 有谁能够帮我? 感谢
答案 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
有关详细信息,请尝试查看uigetdir
,uigetfile
和dir
的Matlab文档。