spm_progress_bar.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function spm_progress_bar(action,varargin)
  2. % Display a 'Progress Bar' in the 'Interactive' window
  3. % FORMAT spm_progress_bar('Init',height,xlabel,ylabel,flgs)
  4. % Initialise the bar in the 'Interactive' window.
  5. % If flgs contains a 't', then use tex interpreter for labels.
  6. %
  7. % FORMAT spm_progress_bar('Set',value)
  8. % Set the height of the bar itself.
  9. %
  10. % FORMAT spm_progress_bar('Set','xlabel',xlabel)
  11. % FORMAT spm_progress_bar('Set','ylabel',ylabel)
  12. % Set the progress bar labels.
  13. %
  14. % FORMAT spm_progress_bar('Set','height',height)
  15. % Set the height of the progress bar.
  16. %
  17. % FORMAT spm_progress_bar('Clear')
  18. % Clear the 'Interactive' window.
  19. %__________________________________________________________________________
  20. % Copyright (C) 1996-2017 Wellcome Trust Centre for Neuroimaging
  21. % John Ashburner
  22. % $Id: spm_progress_bar.m 7112 2017-06-16 11:30:37Z guillaume $
  23. persistent pbar;
  24. if ~nargin, action = 'Init'; end
  25. switch lower(action)
  26. % Initialise
  27. %======================================================================
  28. case 'init'
  29. Finter = spm_figure('FindWin','Interactive');
  30. if isempty(Finter), pbar = []; return; end
  31. if nargin > 1, arg1 = varargin{1}; else arg1 = 1; end
  32. if nargin > 2, arg2 = varargin{2}; else arg2 = 'Computing'; end
  33. if nargin > 3, arg3 = varargin{3}; else arg3 = ''; end
  34. if nargin > 4, arg4 = varargin{4}; else arg4 = ' '; end
  35. if any(arg4 == 't'), interp = 'tex'; else interp = 'none'; end
  36. pb = struct('pointer',get(Finter,'Pointer'),...
  37. 'name', get(Finter,'Name'));
  38. spm_progress_bar('Clear');
  39. set(Finter,'Pointer','watch');
  40. set(Finter,'Name',pb.name);
  41. if ischar(arg2), arg2 = repmat({arg2},1,numel(arg1)); end
  42. if ischar(arg3), arg3 = repmat({arg3},1,numel(arg1)); end
  43. for i=1:numel(arg1)
  44. %-
  45. pb.ax(i) = axes(...
  46. 'Position', [((i/(numel(arg1)+1))-0.05) 0.2 0.05 0.6],...
  47. 'XTick', [],...
  48. 'Xlim', [0 1],...
  49. 'Ylim', [0 max([arg1(i) eps])],...
  50. 'Box', 'on',...
  51. 'Parent', Finter);
  52. try, set(pb.ax(i),'ClippingStyle','rectangle'); end
  53. %-XLabel
  54. lab = get(pb.ax(i),'Xlabel');
  55. if numel(arg2) < i, arg2{i} = ''; end
  56. set(lab,'string',arg2{i},'FontSize',10,'Interpreter',interp);
  57. %-YLabel
  58. lab = get(pb.ax(i),'Ylabel');
  59. if numel(arg3) < i, arg3{i} = ''; end
  60. set(lab,'string',arg3{i},'FontSize',10,'Interpreter',interp);
  61. %-Title
  62. pb.t(i) = get(pb.ax(i),'Title');
  63. set(pb.t(i),'string','0% Complete','Interpreter',interp);
  64. %-Began...
  65. t = clock;
  66. if numel(arg1) == 1, opts = {};
  67. else opts = {'Rotation',90,'HorizontalAlignment','center'}; end
  68. str = sprintf('Began %2.0f:%02.0f:%02.0f',t(4),t(5),t(6));
  69. pb.b(i) = text(2,arg1(i)/2,0,str,...
  70. 'FontSize',10,...
  71. 'Parent',pb.ax(i),...
  72. opts{:});
  73. %-Progress bar
  74. pb.l(i) = line(...
  75. 'Xdata', [0.5 0.5],...
  76. 'Ydata', [0 0],...
  77. 'LineWidth', 8,...
  78. 'Color', [1 0 0],...
  79. 'Parent', pb.ax(i));
  80. end
  81. pbar = pb;
  82. drawnow;
  83. % Set
  84. %======================================================================
  85. case 'set'
  86. if isempty(pbar) || ~all(ishandle(pbar.l)), pbar = []; return; end
  87. if nargin == 1, value = 0; else value = varargin{1}; end
  88. if ischar(value)
  89. if nargin == 2, str = ''; else str = varargin{2}; end
  90. if nargin == 3, p = 1; else p = varargin{3}; end
  91. switch lower(value)
  92. case {'xlabel','ylabel'}
  93. set(get(pbar.ax(p),value),'String',str);
  94. case 'height'
  95. t = clock;
  96. bstr = sprintf('Began %2.0f:%02.0f:%02.0f',t(4),t(5),t(6));
  97. set(pbar.b(p),'String',bstr);
  98. set(pbar.ax(p),'YLim',[0 max([str eps])]);
  99. otherwise
  100. error('Unknown action.');
  101. end
  102. else
  103. if nargin == 2, p = 1; else p = varargin{2}; end
  104. set(pbar.l(p),'Ydata',[0 value]);
  105. lim = get(pbar.ax(p),'Ylim');lim=lim(2);
  106. set(pbar.t(p),'string',sprintf('%.0f%% Complete',100*value/lim));
  107. end
  108. try, drawnow limitrate; catch, drawnow; end
  109. % Clear
  110. %======================================================================
  111. case 'clear'
  112. Finter = spm_figure('FindWin','Interactive');
  113. if isempty(Finter), pbar = []; return; end
  114. spm_figure('Clear',Finter);
  115. if isstruct(pbar)
  116. set(Finter,'Pointer', pbar.pointer);
  117. set(Finter,'Name', pbar.name);
  118. end
  119. pbar = [];
  120. drawnow;
  121. % Error
  122. %======================================================================
  123. otherwise
  124. error('Unknown action string');
  125. end