spm_plot_convergence.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function spm_plot_convergence(action,varargin)
  2. % Display a plot showing convergence of an optimisation routine.
  3. % FORMAT spm_plot_convergence('Init',title,ylabel,xlabel)
  4. % Initialise the plot in the 'Interactive' window.
  5. %
  6. % FORMAT spm_plot_convergence('Set',value)
  7. % Update the plot.
  8. %
  9. % FORMAT spm_plot_convergence('Clear')
  10. % Clear the 'Interactive' window.
  11. %__________________________________________________________________________
  12. % Copyright (C) 2008-2017 Wellcome Trust Centre for Neuroimaging
  13. % John Ashburner
  14. % $Id: spm_plot_convergence.m 7112 2017-06-16 11:30:37Z guillaume $
  15. if ~nargin, action = 'Init'; end
  16. % Find the interactive window and exit if not
  17. %--------------------------------------------------------------------------
  18. Finter = spm_figure('FindWin','Interactive');
  19. if isempty(Finter), return; end
  20. switch lower(action)
  21. % Initialise
  22. %----------------------------------------------------------------------
  23. case 'init'
  24. if nargin > 1, arg1 = varargin{1}; else arg1 = 'Optimising'; end
  25. if nargin > 2, arg2 = varargin{2}; else arg2 = 'Chi-squared'; end
  26. if nargin > 3, arg3 = varargin{3}; else arg3 = 'Iteration'; end
  27. pb = struct('pointer',get(Finter,'Pointer'),...
  28. 'name', get(Finter,'Name'));
  29. spm_plot_convergence('Clear');
  30. set(Finter,'Pointer','watch');
  31. set(Finter,'Name',pb.name);
  32. pb.ax = axes('Position',[0.15 0.1 0.8 0.75],...
  33. 'Box', 'on',...
  34. 'Parent', Finter);
  35. set(get(pb.ax,'Xlabel'), 'string',arg3, 'FontSize',10);
  36. set(get(pb.ax,'Ylabel'), 'string',arg2, 'FontSize',10);
  37. set(get(pb.ax,'Title'), 'string',arg1);
  38. l = line('Xdata', [],...
  39. 'Ydata', [],...
  40. 'LineWidth', 2,...
  41. 'Tag', 'PlotConvOptim',...
  42. 'Parent', pb.ax);
  43. set(l,'UserData',pb);
  44. % Set
  45. %----------------------------------------------------------------------
  46. case 'set'
  47. if nargin > 1, arg1 = varargin{1}; else arg1 = 0; end
  48. br = findobj(Finter,'Tag','PlotConvOptim');
  49. if ~isempty(br)
  50. xd = get(br,'Xdata');
  51. yd = [get(br,'Ydata') arg1];
  52. xd = [xd (length(xd)+1)];
  53. set(br,'Ydata',yd, 'Xdata',xd);
  54. end
  55. % Clear
  56. %----------------------------------------------------------------------
  57. case 'clear'
  58. pb = get(findobj(Finter,'Tag','PlotConvOptim'),'UserData');
  59. spm_figure('Clear',Finter);
  60. if isstruct(pb)
  61. set(Finter,'Pointer', pb.pointer);
  62. set(Finter,'Name', pb.name);
  63. end
  64. % Error
  65. %----------------------------------------------------------------------
  66. otherwise
  67. error('Unknown action string');
  68. end
  69. drawnow;