我有一些文件都有相同的名称,比如'file.nii',这些文件有60个,每个文件都在一个特定的目录中。
结构为/Users/name/DirC2133/nDir/file.nii
,其结构始终采用/Users/name/DirC{4 integers here}/nDir/file.nii
形式。所以我们这样说:
/Users/name/DirC3065/nDir/file.nii
/Users/name/DirC3067/nDir/file.nii
/Users/name/DirC3078/nDir/file.nii
/Users/name/DirC3156/nDir/file.nii
/Users/name/DirC3546/nDir/file.nii
.....
等等。我想将每个完整路径存储到file.nii以供以后使用。我如何用MATLAB做到这一点? nDir中还有其他文件,但我只想要file.nii
的路径在伪代码中我猜它会是这样的:
for i in 1:60
file[i] = /Users/name/DirC%/nDir/file.nii
答案 0 :(得分:0)
如果您知道文件的ID并且只想创建字符串,则可以执行以下操作。
image_num_list = [3065,3067,3078,3156,3546]
container = cell(length(image_num_list),1);
for i=1:length(image_num_list)
container{i}=sprintf('/Users/name/DirC%04d/nDir/file.nii',image_num_list(i))
end
如果你想以递归的方式列出目录,你可以在matlab中通过一个dir调用来完成这个,你可以使用这段代码:
http://www.mathworks.com/matlabcentral/fileexchange/32226-recursive-directory-listing-enhanced-rdir
如果您偶然使用SPM,它的功能是选择nii和相关文件:
spm_select
File selector
FORMAT [t,sts] = spm_select(n,typ,mesg,sel,wd,filt,frames)
n - Number of files
A single value or a range. e.g.
1 - Select one file
Inf - Select any number of files
[1 Inf] - Select 1 to Inf files
[0 1] - select 0 or 1 files
[10 12] - select from 10 to 12 files
typ - file type
'any' - all files
'image' - Image files (".img" and ".nii")
Note that it gives the option to select
individual volumes of the images.
'mesh' - Surface mesh files (".gii" or ".mat")
'xml' - XML files
'mat' - Matlab .mat files or .txt files (assumed to contain
ASCII representation of a 2D-numeric array)
'batch' - SPM batch files (.m, .mat and XML)
'dir' - select a directory
Other strings act as a filter to regexp. This means
that e.g. DCM*.mat files should have a typ of '^DCM.*\.mat$'
mesg - a prompt (default 'Select files...')
sel - list of already selected files
wd - Directory to start off in
filt - value for user-editable filter (default '.*')
frames - Image frame numbers to include (default '1')
t - selected files
sts - status (1 means OK, 0 means window quit)
FORMAT [t,ind] = spm_select('Filter',files,typ,filt,frames)
filter the list of files (cell or char array) in the same way as the
GUI would do. There is an additional typ 'extimage' which will match
images with frame specifications, too. Also, there is a typ 'extdir',
which will match canonicalised directory names. The 'frames' argument
is currently ignored, i.e. image files will not be filtered out if
their frame numbers do not match.
t returns the filtered list (cell or char array, depending on input),
ind an index array, such that t = files{ind}, or t = files(ind,:).
FORMAT cpath = spm_select('CPath',path,cwd)
function to canonicalise paths: Prepends cwd to relative paths, processes
'..' & '.' directories embedded in path.
path - string matrix containing path name
cwd - current working directory [default '.']
cpath - conditioned paths, in same format as input path argument
FORMAT [files,dirs] = spm_select('List',direc,filt)
Returns files matching the filter (filt) and directories within direc
direc - directory to search
filt - filter to select files with (see regexp) e.g. '^w.*\.img$'
files - files matching 'filt' in directory 'direc'
dirs - subdirectories of 'direc'
FORMAT [files,dirs] = spm_select('ExtList',direc,filt,frames)
As above, but for selecting frames of 4D NIfTI files
frames - vector of frames to select (defaults to 1, if not
specified). If the frame number is Inf, all frames for the
matching images are listed.
FORMAT [files,dirs] = spm_select('FPList',direc,filt)
FORMAT [files,dirs] = spm_select('ExtFPList',direc,filt,frames)
As above, but returns files with full paths (i.e. prefixes direc to each)
FORMAT [files,dirs] = spm_select('FPListRec',direc,filt)
FORMAT [files,dirs] = spm_select('ExtFPListRec',direc,filt,frames)
As above, but returns files with full paths (i.e. prefixes direc to
each) and searches through sub directories recursively.
FORMAT spm_select('prevdirs',dir)
Add directory dir to list of previous directories.
FORMAT dirs = spm_select('prevdirs')
Retrieve list of previous directories.