【问题标题】:Generating a list of paths to files in directories, MATLAB生成目录中文件的路径列表,MATLAB
【发布时间】:2015-10-26 15:55:50
【问题描述】:

我有一些文件都具有相同的名称,比如“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

【问题讨论】:

  • d = dir('/Users/name/DirC*');
  • 当我尝试这个时,我得到的索引超出了矩阵维度
  • 这意味着你在工作区中有一个dir 变量。不要用变量覆盖函数名称。清除 dir variablea 并将其命名为 smt ele。

标签: matlab


【解决方案1】:

如果您知道文件的 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.

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多