使用MATLAB遍历指定的子文件夹及其下文件
来源:互联网 发布:mac 10.11.6 官方下载 编辑:程序博客网 时间:2024/06/06 05:39
文件目录结构
项目需要批量将图像导入Matlab,进行分类。
/maindir |-- subdir1 |-- test1.bmp |-- test2.bmp |-- ... |-- subdir2 |-- test3.bmp |-- test4.bmp |-- ... |-- subdir3 |-- test5.bmp |-- test6.bmp |-- ... |-- ...
主文件夹maindir下含有十个子文件夹,子文件夹分别包含多个图像bmp文件。
函数dir
可以使用函数dir,D = DIR(‘directory_name’)返回一个结构数组,包含了文件夹directory_name下的子文件夹和子文件的一些信息,第1个成员是文件名,第4个成员表示是否为文件夹。
%DIR List directory.% DIR directory_name lists the files in a directory. Pathnames and% wildcards may be used. For example, DIR *.m lists all program files% in the current directory.%% D = DIR('directory_name') returns the results in an M-by-1% structure with the fields: % name -- Filename% date -- Modification date% bytes -- Number of bytes allocated to the file% isdir -- 1 if name is a directory and 0 if not% datenum -- Modification date as a MATLAB serial date number.% This value is locale-dependent.%% See also WHAT, CD, TYPE, DELETE, LS, RMDIR, MKDIR, DATENUM.%% Copyright 1984-2010 The MathWorks, Inc.% Built-in function.
[注意] 要注意的是,第1个数组元素和第2个数组元素分别是’.’和’..’,表示当前目录和上层目录。
代码实现
%% clear allclc;clear;close all;%% Add all pathglobal matlabVisRoot;demo_path = [ matlabVisRoot '' ];addpath( demo_path );main_path = [ demo_path, 'MAINDIR/' ];%% Read all the images in the sub-files under LOGOS file.maindir = dir( main_path );for i = 1 : length( maindir ) % If maindir(i) is not a dir, skip if( isequal( maindir( i ).name, '.' )||... isequal( maindir( i ).name, '..')||... ~maindir( i ).isdir) continue; end % If maindir(i) is a dir, find the image files under the maindir(i).name subdirpath = fullfile( logo_path, maindir( i ).name, '*.bmp' ); dat = dir( subdirpath ); % read the files under the subdirpath for j = 1 : length( dat ) datapath = fullfile( logo_path, maindir( i ).name, dat( j ).name); img = imread( datapath ); endend
函数uigetdir
由于前面maindir需要指定路径,可能不是太方便。
使用uigetdir可以方便的通过对话框选择文件夹,返回值为文件夹路径名。代码如下:
maindir = uigetdir( '选择一个文件夹' );
参考资料
[原]MATLAB遍历子文件夹及其下文件:
http://m.blog.csdn.net/blog/u012675539/43671663
0 0
- 使用MATLAB遍历指定的子文件夹及其下文件
- MATLAB遍历子文件夹及其下文件
- 遍历文件夹下文件
- 遍历文件夹下文件
- 遍历文件夹下文件
- 将指定文件夹下文件及子文件夹下的文件复制到同一级目录
- java io 使用递归删除指定的文件夹及其子文件夹和文件 先测试再正式使用哦
- PHP遍历文件夹下文件
- android遍历文件夹下文件
- C遍历文件夹下文件
- C++遍历文件夹下文件
- Linux遍历文件夹下文件
- Android 遍历删除SD卡中某一文件夹下的指定子文件夹及其子文件
- java循环遍历文件夹及其子文件夹和文件
- 批量复制某文件夹下文件(包含子文件夹)到指定文件夹
- SHELL文件夹及其子文件夹的遍历
- 将指定文件夹下文件的文件名导入excel中
- 批处理 显示指定文件夹下文件的大小和时间
- ubuntu 登陆时密码正确,却不进入桌面
- 升级scipy
- Win32修改ListView控件扩展属性
- 黑马程序员——面向对象
- Spring---->Bean的作用域
- 使用MATLAB遍历指定的子文件夹及其下文件
- Reinventing the company(From the economist)
- 水仙花数
- Spring bean 的生命周期的详解
- Android 5.0动态设计细节
- 简单的分析绣球网这款产品
- 【Leetcode】之Regular Expression Matching
- android gridview 间距相等布局代码
- Eclipse快捷键 10个最有用的快捷键