loadmulti.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function f = loadmulti(m,var,dim,gentle)
  2. % function f = loadmulti(m,var,dim,gentle)
  3. %
  4. % <m> is a pattern that matches one or more .mat files (see matchfiles.m)
  5. % <var> is a variable name
  6. % <dim> (optional) is
  7. % N means the dimension along which to concatenate
  8. % 0 means do not concatenate; instead, successively merge
  9. % in new versions of <var>. there are two cases:
  10. % if <var> is a regular matrix, NaN elements get
  11. % overwritten with new content. if <var> is a cell
  12. % matrix, elements that are [] get overwritten with
  13. % new content. note that the dimensions
  14. % of <var> should be the same in each file.
  15. % however, if they aren't, that's okay --- we
  16. % automatically expand to the bottom and the right
  17. % as necessary.
  18. % default: 0.
  19. % <gentle> (optional) is
  20. % 0 means die if <var> is not found in a given file.
  21. % 1 means continue on (and do not crash) if <var> is
  22. % not found in a given file.
  23. % default: 0.
  24. %
  25. % get <var> from the file(s) named by <m> and either
  26. % concatenate different versions together or merge
  27. % different versions together.
  28. %
  29. % we issue a warning if no files are named by <m>.
  30. %
  31. % we report progress to the command window if the load
  32. % takes longer than 10 seconds.
  33. %
  34. % example:
  35. % a = {1 2 []};
  36. % save('atest1.mat','a');
  37. % a = {[] 5 3};
  38. % save('atest2.mat','a');
  39. % isequal(loadmulti('atest*mat','a',0),{1 2 3})
  40. % internal constants
  41. toolong = 10; % seconds
  42. % input
  43. if ~exist('dim','var') || isempty(dim)
  44. dim = 0;
  45. end
  46. if ~exist('gentle','var') || isempty(gentle)
  47. gentle = 0;
  48. end
  49. % transform
  50. m = matchfiles(m);
  51. % check sanity
  52. if length(m)==0
  53. warning('no file matches');
  54. f = [];
  55. return;
  56. end
  57. % do it
  58. stime = clock; didinit = 0; isfirst = 1;
  59. for p=1:length(m)
  60. % report status
  61. if etime(clock,stime) > toolong
  62. if didinit
  63. statusdots(p,length(m));
  64. else
  65. didinit = 1;
  66. fprintf('loadmulti');
  67. end
  68. end
  69. % get values from the file
  70. loaded = load(m{p},var);
  71. varnames = fieldnames(loaded);
  72. if isempty(varnames)
  73. if gentle==0
  74. error(sprintf('loadmulti: <var> was not found in some particular file (%s)',m{p}));
  75. end
  76. else
  77. vals = getfield(loaded,varnames{1});
  78. % special case
  79. if dim==0
  80. if isfirst
  81. f = vals;
  82. isfirst = 0;
  83. else
  84. [f,vals] = equalizematrixdimensions(f,vals);
  85. if iscell(f)
  86. ix = cellfun(@isempty,f);
  87. else
  88. ix = isnan(f);
  89. end
  90. f(ix) = vals(ix);
  91. end
  92. % usual case
  93. else
  94. if isfirst
  95. f = vals;
  96. isfirst = 0;
  97. else
  98. f = cat(dim,f,vals);
  99. end
  100. end
  101. end
  102. end
  103. % report
  104. if etime(clock,stime) > toolong
  105. fprintf('done.\n');
  106. end