spm_changepath.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function varargout = spm_changepath(Sf, oldp, newp, verbose)
  2. % Recursively replace all occurences of a text pattern in a MATLAB variable.
  3. % FORMAT S = spm_changepath(Sf, oldp, newp)
  4. %
  5. % Sf - MATLAB variable to fix, or char array of MAT filenames,
  6. % or directory name (all found MAT files will be analysed)
  7. % oldp - old string to replace
  8. % newp - new string replacing oldp
  9. %
  10. % S - updated MATLAB variable (only if Sf is one)
  11. %
  12. % If the pattern is found in a string, any occurence of an invalid file
  13. % separator is replaced to match that of the current system.
  14. %
  15. % If MAT filenames are specified, they will be overwritten with the new
  16. % version. A backup of the initial version is made with a ".old" suffix.
  17. %__________________________________________________________________________
  18. % Copyright (C) 2009-2016 Wellcome Trust Centre for Neuroimaging
  19. % Guillaume Flandin
  20. % $Id: spm_changepath.m 6948 2016-11-24 10:34:01Z guillaume $
  21. %-Input arguments
  22. %--------------------------------------------------------------------------
  23. if ~nargin
  24. Sf = spm_select(Inf,'mat','Select MAT files to fix');
  25. end
  26. if nargin <= 1
  27. oldp = spm_input('Old pattern','+1','s');
  28. end
  29. if nargin <= 2
  30. newp = spm_input('New pattern','+1','s');
  31. end
  32. if nargin <= 3
  33. verbose = true;
  34. end
  35. %-Replace pattern in given MAT-files
  36. %--------------------------------------------------------------------------
  37. if ischar(Sf)
  38. if nargout
  39. error('Output argument only valid for MATLAB variable input');
  40. end
  41. if exist(Sf,'dir')
  42. Sf = spm_select('FPList',Sf,'^.*\.mat$'); % FPListRec for recursive
  43. end
  44. if isempty(Sf), Sf = {}; else Sf = cellstr(Sf); end
  45. for i=1:numel(Sf)
  46. f = Sf{i};
  47. try
  48. S = load(f);
  49. catch
  50. error('Cannot load "%s".',f);
  51. end
  52. tmp = changepath(S,oldp,newp,verbose);
  53. if ~isequalwithequalnans(tmp,S)
  54. if verbose, fprintf('=> Fixing %s\n',f); end
  55. [sts, msg] = movefile(f,[f '.old']);
  56. if ~sts, error(msg); end
  57. save(f ,'-struct','tmp', spm_get_defaults('mat.format'));
  58. end
  59. end
  60. else
  61. varargout = { changepath(Sf,oldp,newp,verbose) };
  62. end
  63. %==========================================================================
  64. function S = changepath(S,oldp,newp,verbose)
  65. check = false;
  66. switch class(S)
  67. case {'double','single','logical','int8','uint8','int16','uint16',...
  68. 'int32','uint32','int64','uint64','function_handle'}
  69. case 'cell'
  70. for i=1:numel(S)
  71. S{i} = changepath(S{i},oldp,newp,verbose);
  72. end
  73. case 'struct'
  74. for i=1:numel(S)
  75. fn = fieldnames(S);
  76. for j=1:length(fn)
  77. S(i).(fn{j}) = changepath(S(i).(fn{j}),oldp,newp,verbose);
  78. end
  79. end
  80. case 'char'
  81. f = {'\' '/'};
  82. if ispc, f = fliplr(f); end
  83. tmp = cellstr(S);
  84. for i=1:numel(tmp)
  85. t = strrep(tmp{i},oldp,newp);
  86. if ~isequal(tmp{i},t)
  87. t = strrep(t,f{1},f{2});
  88. if check
  89. if spm_existfile(t)
  90. sts = ' (OK) ';
  91. else
  92. sts = ' (NOT FOUND) ';
  93. end
  94. else
  95. sts = '';
  96. end
  97. if verbose, fprintf('%s%s\n',t,sts); end
  98. end
  99. tmp{i} = t;
  100. end
  101. S = char(tmp);
  102. case 'nifti'
  103. for i=1:numel(S)
  104. S(i).dat = changepath(S(i).dat,oldp,newp,verbose);
  105. end
  106. case 'file_array'
  107. S.fname = changepath(S.fname,oldp,newp,verbose);
  108. case 'gifti'
  109. if isfield(S,'cdata') && isa(S.cdata,'file_array')
  110. fa = struct(S.cdata);
  111. fa.fname = changepath(fa.fname,oldp,newp,verbose);
  112. S.cdata = file_array(fa);
  113. end
  114. otherwise
  115. warning('Unknown class "%s".',class(S));
  116. end