自己时常花费较多时间用于寻找及回忆命令上,不应该,因此做些记录,更快的恢复记忆。
% 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