需求
有一个比较深层的文件夹,每个文件夹及其子文件夹下都可能存在我所需要的csv文件,写一个函数,输入文件夹路径后可以返回所有符合要求的csv文件。
代码实现
% folder_path为输入的文件夹,str为指定的文件所特有的关键字,例如"csv"
function [file_list]=get_file_list_from_folder(folder_path,str)
%% find all the object file path in a folder
file_list={};
files = dir(folder_path);
count=1;
for i=1:length(files)file=files(i)if file.isdir && ~contains(file.name,'.') && ~contains(file.name,'..')subdir=fullfile(folder_path,file.name);get_file_list_from_folder(subdir)elsepath=fullfile(folder_path,file.name)if contains(path,str) % disp(path)file_list{count}=path;count=count+1;endend
endend
说明,matlab中要实现python列表类似的可以储存字符串的矩阵,可以通过cell去实现。cell的定义方式为var_cell={}
。