Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

subplot_tight.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function vargout=subplot_tight(m, n, p, margins, varargin)
  2. %% subplot_tight
  3. % A subplot function substitude with margins user tunabble parameter.
  4. %
  5. %% Syntax
  6. % h=subplot_tight(m, n, p);
  7. % h=subplot_tight(m, n, p, margins);
  8. % h=subplot_tight(m, n, p, margins, subplotArgs...);
  9. %
  10. %% Description
  11. % Our goal is to grant the user the ability to define the margins between neighbouring
  12. % subplots. Unfotrtunately Matlab subplot function lacks this functionality, and the
  13. % margins between subplots can reach 40% of figure area, which is pretty lavish. While at
  14. % the begining the function was implememnted as wrapper function for Matlab function
  15. % subplot, it was modified due to axes del;etion resulting from what Matlab subplot
  16. % detected as overlapping. Therefore, the current implmenetation makes no use of Matlab
  17. % subplot function, using axes instead. This can be problematic, as axis and subplot
  18. % parameters are quie different. Set isWrapper to "True" to return to wrapper mode, which
  19. % fully supports subplot format.
  20. %
  21. %% Input arguments (defaults exist):
  22. % margins- two elements vector [vertical,horizontal] defining the margins between
  23. % neighbouring axes. Default value is 0.04
  24. %
  25. %% Output arguments
  26. % same as subplot- none, or axes handle according to function call.
  27. %
  28. %% Issues & Comments
  29. % - Note that if additional elements are used in order to be passed to subplot, margins
  30. % parameter must be defined. For default margins value use empty element- [].
  31. % -
  32. %
  33. %% Example
  34. % close all;
  35. % img=imread('peppers.png');
  36. % figSubplotH=figure('Name', 'subplot');
  37. % figSubplotTightH=figure('Name', 'subplot_tight');
  38. % nElems=17;
  39. % subplotRows=ceil(sqrt(nElems)-1);
  40. % subplotRows=max(1, subplotRows);
  41. % subplotCols=ceil(nElems/subplotRows);
  42. % for iElem=1:nElems
  43. % figure(figSubplotH);
  44. % subplot(subplotRows, subplotCols, iElem);
  45. % imshow(img);
  46. % figure(figSubplotTightH);
  47. % subplot_tight(subplotRows, subplotCols, iElem, [0.0001]);
  48. % imshow(img);
  49. % end
  50. %
  51. %% See also
  52. % - subplot
  53. %
  54. %% Revision history
  55. % First version: Nikolay S. 2011-03-29.
  56. % Last update: Nikolay S. 2012-05-24.
  57. %
  58. % *List of Changes:*
  59. % 2012-05-24
  60. % Non wrapping mode (based on axes command) added, to deal with an issue of disappearing
  61. % subplots occuring with massive axes.
  62. %% Default params
  63. isWrapper=false;
  64. if (nargin<4) || isempty(margins)
  65. margins=[0.04,0.04]; % default margins value- 4% of figure
  66. end
  67. if length(margins)==1
  68. margins(2)=margins;
  69. end
  70. %note n and m are switched as Matlab indexing is column-wise, while subplot indexing is row-wise :(
  71. [subplot_col,subplot_row]=ind2sub([n,m],p);
  72. height=(1-(m+1)*margins(1))/m; % single subplot height
  73. width=(1-(n+1)*margins(2))/n; % single subplot width
  74. % note subplot suppors vector p inputs- so a merged subplot of higher dimentions will be created
  75. subplot_cols=1+max(subplot_col)-min(subplot_col); % number of column elements in merged subplot
  76. subplot_rows=1+max(subplot_row)-min(subplot_row); % number of row elements in merged subplot
  77. merged_height=subplot_rows*( height+margins(1) )- margins(1); % merged subplot height
  78. merged_width= subplot_cols*( width +margins(2) )- margins(2); % merged subplot width
  79. merged_bottom=(m-max(subplot_row))*(height+margins(1)) +margins(1); % merged subplot bottom position
  80. merged_left=min(subplot_col)*(width+margins(2))-width; % merged subplot left position
  81. pos=[merged_left, merged_bottom, merged_width, merged_height];
  82. if isWrapper
  83. h=subplot(m, n, p, varargin{:}, 'Units', 'Normalized', 'Position', pos);
  84. else
  85. h=axes('Position', pos, varargin{:});
  86. end
  87. if nargout==1
  88. vargout=h;
  89. end