matlab遍历文件夹下指定类型文件及提取文件路径及文件名

% 函数功能:指定路径path下所有图像路径,不扫描子文件夹
% path:查找的路径
% file_mask:需要查找的文件类型,比如*.jpg
function file_list = ScanDir(path, file_mask)
file_path =  path;  % 图像文件夹路径
img_path_list = dir(strcat(file_path, file_mask)); % 获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);    % 获取图像总数量
file_list = cell(img_num, 1);
if img_num > 0 %有满足条件的图像
    for j = 1:img_num %逐一读取图像
        image_name = img_path_list(j).name;% 图像名
        fprintf('当前找到指定的文件 %s\n', strcat(file_path,image_name));% 显示扫描到的图像路径名
        file_list{j} = image_name;
    end
end
end

ex

list=ScanDir('E:\Data_Set\','*.xml');

matlab 分割字符串、提取文件路径及文件名

 caseFileName = 'E:\test\test.txt'
 nameList = strsplit(caseFileName,'\')
 nameTmp = char(nameList(length(nameList)))%分割之后是cell类型,需要转换为char类型
 fileName = nameTmp(1:length(nameTmp)-4) % fileName 结果为   test

2

 caseFileName = 'E:\test\test.txt'
[pathstr,name,suffix]=fileparts(caseFileName); % pathstr 结果为 E:\test ;  name 结果为  test;suffix 结果为  .txt

ref
https://blog.csdn.net/m_buddy/article/details/78807263

猜你喜欢

转载自blog.csdn.net/qq_35608277/article/details/86756137