210928-Matlab读取文件夹及.nii格式

自己时常花费较多时间用于寻找及回忆命令上,不应该,因此做些记录,更快的恢复记忆。

% read the whole files
patient_paths='patient_paths';
patient_names=dir(patient_paths);
for ii=3:length(patient_names)  % start from 3
    patient_name=patient_names(ii).name;
    patient_path=fullfile(patient_paths,patient_name);
% read the files end with '.nii'
patient_paths='patient_paths';
patient_names=dir([patient_paths, '/*.nii']);  % careful! / for Lunix and \ for windows
for ii=1:length(patient_names)  % start from 1
    patient_name=patient_names(ii).name;
    patient_path=fullfile(patient_paths,patient_name);

另一种方法,写法更简洁,自己用的少,可以学习下:

dirs = dir([path, '/*.nii']);  % careful! / for Lunix and \ for windows
dircell = struct2cell(dirs)';
names = dircell(:,1);

至此,以后不应在matlab读取文件上浪费时间,如有更好方法,随时更新。

读取文件夹:

function SubFolders = A6_GetSubFolders(ParentFolder)
    SubFolderNames = dir(ParentFolder);
    SubFolder = struct([]);
    for i=1:length(SubFolderNames)
            if( isequal( SubFolderNames( i ).name, '.' )||...
            isequal( SubFolderNames( i ).name, '..')||...
            ~SubFolderNames( i ).isdir) % 如果不是目录则跳过
                continue;
            end
            SubFolder(i).SubFolderName = SubFolderNames( i ).name;
    end

    SubFoldersTemp = {SubFolder.SubFolderName};
    idx = cellfun(@(x)~isempty(x),SubFoldersTemp,'UniformOutput',true); % 利用cellfun函数得到元胞数组中所有非空元素的下标
    SubFolders = SubFoldersTemp(idx);
end
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值