spm_extract_files.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function spm_extract_files(P,cwd)
  2. % FORMAT spm_extract_files(P,cwd)
  3. % forints files (and their subroutines) and expect them to the current
  4. % directory
  5. %__________________________________________________________________________
  6. % Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging
  7. % Karl Friston
  8. % $Id: spm_extract_files.m 5175 2013-01-04 12:50:44Z guillaume $
  9. if nargin == 1; cwd = pwd; end
  10. % deal with cell arrays
  11. %--------------------------------------------------------------------------
  12. if iscell(P)
  13. for i = 1:length(P)
  14. spm_extract_files(P{i},cwd)
  15. end
  16. return
  17. end
  18. % get file
  19. %--------------------------------------------------------------------------
  20. if isempty(dir(P))
  21. try
  22. % check for subroutines
  23. %------------------------------------------------------------------
  24. copyfile(which(P),cwd);
  25. end
  26. else
  27. return
  28. end
  29. % check for subroutines
  30. %--------------------------------------------------------------------------
  31. try
  32. fid = fopen(P);
  33. Q = textscan(fid,'%s');
  34. fclose(fid);
  35. Q = Q{1};
  36. for i = 1:length(Q)
  37. s = strfind(Q{i},'spm_');
  38. for k = 1:length(s)
  39. % calls: spm_???(
  40. %--------------------------------------------------------------
  41. j = strfind(Q{i},'(');
  42. j = j(find(j > s(k),1));
  43. if ~isempty(j)
  44. q = [Q{i}(s(k):(j - 1)) '.m'];
  45. if ~strcmp(P,q)
  46. spm_extract_files(q,cwd);
  47. end
  48. end
  49. % functions: 'spm_???'
  50. %--------------------------------------------------------------
  51. j = strfind(Q{i},'''');
  52. j = j(find(j > s(k),1));
  53. if ~isempty(j)
  54. q = [Q{i}(s(k):(j - 1)) '.m'];
  55. if ~strcmp(P,q)
  56. spm_extract_files(q,cwd);
  57. end
  58. end
  59. end
  60. end
  61. end