/trunk/examples/toolboxes/fieldtrip/ft_volumereslice.m

http://brainstream.googlecode.com/ · MATLAB · 112 lines · 45 code · 16 blank · 51 comment · 10 complexity · ae3a9aa0d393682c80996efaa17cc42b MD5 · raw file

  1. function mri = ft_volumereslice(cfg, mri)
  2. % FT_VOLUMERESLICE reslices a volume along the principal axes of the
  3. % coordinate system according to a specified resolution.
  4. %
  5. % Use as
  6. % mri = ft_volumereslice(cfg, mri)
  7. % where the mri contains an anatomical or functional volume and cfg is a
  8. % configuration structure containing
  9. % cfg.xrange = [min max], in physical units
  10. % cfg.yrange = [min max], in physical units
  11. % cfg.zrange = [min max], in physical units
  12. % cfg.resolution = number, in physical units
  13. %
  14. % See also FT_VOLUMEDOWNSAMPLE, FT_SOURCEINTREPOLATE
  15. %
  16. % Undocumented local options:
  17. % cfg.inputfile = one can specifiy preanalysed saved data as input
  18. % cfg.outputfile = one can specify output as file to save to disk
  19. % Copyright (C) 2010, Robert Oostenveld
  20. %
  21. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  22. % for the documentation and details.
  23. %
  24. % FieldTrip is free software: you can redistribute it and/or modify
  25. % it under the terms of the GNU General Public License as published by
  26. % the Free Software Foundation, either version 3 of the License, or
  27. % (at your option) any later version.
  28. %
  29. % FieldTrip is distributed in the hope that it will be useful,
  30. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. % GNU General Public License for more details.
  33. %
  34. % You should have received a copy of the GNU General Public License
  35. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  36. %
  37. % $Id$
  38. % check if the input cfg is valid for this function
  39. cfg = checkconfig(cfg, 'trackconfig', 'on');
  40. % set the defaults
  41. if ~isfield(cfg, 'resolution'); cfg.resolution = 1; end % in physical units
  42. if ~isfield(cfg, 'downsample'); cfg.downsample = 1; end
  43. if ~isfield(cfg, 'inputfile'), cfg.inputfile = []; end
  44. if ~isfield(cfg, 'outputfile'), cfg.outputfile = []; end
  45. % load optional given inputfile as data
  46. hasdata = (nargin>1);
  47. if ~isempty(cfg.inputfile)
  48. % the input data should be read from file
  49. if hasdata
  50. error('cfg.inputfile should not be used in conjunction with giving input data to this function');
  51. else
  52. mri = loadvar(cfg.inputfile, 'data');
  53. end
  54. end
  55. % check if the input data is valid for this function and ensure that the structures correctly describes a volume
  56. mri = checkdata(mri, 'datatype', 'volume', 'inside', 'logical', 'feedback', 'yes', 'hasunits', 'yes');
  57. cfg = checkconfig(cfg, 'required', {'xrange', 'yrange', 'zrange'});
  58. if ~isequal(cfg.downsample, 1)
  59. % downsample the anatomical volume
  60. tmpcfg = [];
  61. tmpcfg.downsample = cfg.downsample;
  62. mri = ft_volumedownsample(tmpcfg, mri);
  63. end
  64. % compute the desired grid positions
  65. xgrid = cfg.xrange(1):cfg.resolution:cfg.xrange(2);
  66. ygrid = cfg.yrange(1):cfg.resolution:cfg.yrange(2);
  67. zgrid = cfg.zrange(1):cfg.resolution:cfg.zrange(2);
  68. pseudomri = [];
  69. pseudomri.dim = [length(xgrid) length(ygrid) length(zgrid)];
  70. pseudomri.transform = translate([cfg.xrange(1) cfg.yrange(1) cfg.zrange(1)]) * scale([cfg.resolution cfg.resolution cfg.resolution]);
  71. pseudomri.anatomy = zeros(pseudomri.dim, 'int8');
  72. clear xgrid ygrid zgrid
  73. fprintf('reslicing from [%d %d %d] to [%d %d %d]\n', mri.dim(1), mri.dim(2), mri.dim(3), pseudomri.dim(1), pseudomri.dim(2), pseudomri.dim(3));
  74. tmpcfg = [];
  75. mri = ft_sourceinterpolate(tmpcfg, mri, pseudomri);
  76. % accessing this field here is needed for the configuration tracking
  77. % by accessing it once, it will not be removed from the output cfg
  78. cfg.outputfile;
  79. % add version information to the configuration
  80. try
  81. % get the full name of the function
  82. cfg.version.name = mfilename('fullpath');
  83. catch
  84. % required for compatibility with Matlab versions prior to release 13 (6.5)
  85. [st, i] = dbstack;
  86. cfg.version.name = st(i);
  87. end
  88. cfg.version.id = '$Id: ft_sourceinterpolate.m 715 2010-03-09 10:57:27Z roboos $';
  89. % remember the configuration details of the input data
  90. try cfg.previous = mri.cfg; end
  91. % remember the exact configuration details in the output
  92. mri.cfg = cfg;
  93. % the output data should be saved to a MATLAB file
  94. if ~isempty(cfg.outputfile)
  95. savevar(cfg.outputfile, 'data', mri); % use the variable name "data" in the output file
  96. end