/tags/R2002-11-30/octave-forge/main/plot/legend.m

# · MATLAB · 227 lines · 220 code · 7 blank · 0 comment · 9 complexity · 7b342c532a05df10d784d9845cde92ac MD5 · raw file

  1. ## Copyright (C) 2001 Laurent Mazet
  2. ##
  3. ## This program is free software; it is distributed in the hope that it
  4. ## will be useful, but WITHOUT ANY WARRANTY; without even the implied
  5. ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  6. ## the GNU General Public License for more details.
  7. ##
  8. ## You should have received a copy of the GNU General Public License
  9. ## along with this file; see the file COPYING. If not, write to the
  10. ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  11. ## 02111-1307, USA.
  12. ## usage: legend (string1, string2, string3, ..., [pos])
  13. ## legend ([string1; string2; string3; ...], [pos])
  14. ## legend ("off")
  15. ##
  16. ## Legend puts a legend on the current plot using the specified strings
  17. ## as labels. Legend works on line graphs, bar graphs, etc...
  18. ## Be sure to call plot before calling legend.
  19. ##
  20. ## pos: places the legend in the specified location:
  21. ## 0 = Don't move the legend box (default)
  22. ## 1 = Upper right-hand corner
  23. ## 2 = Upper left-hand corner
  24. ## 3 = Lower left-hand corner
  25. ## 4 = Lower right-hand corner
  26. ## -1 = To the right of the plot
  27. ##
  28. ## off will switch off legends from the plot
  29. ##
  30. ## REQUIRES: unix piping functionality, grep, sed and awk
  31. ## 2001-03-31 Paul Kienzle
  32. ## * use tmpnam for temporary file name; unlink to remove
  33. ## 2001-09-28 Paul Söderlind <Paul.Soderlind@hhs.se>
  34. ## * add a pause after save request to give gnuplot time to write the file
  35. ## * add comment to call plot before legend.
  36. ## 2002-09-18 Paul Kienzle
  37. ## * make the pause check every .1 seconds
  38. function legend (varargin)
  39. gset key;
  40. ## Data type
  41. data_type = 0;
  42. va_arg_cnt = 1;
  43. str = "";
  44. if (nargin > 0)
  45. str = nth (varargin, va_arg_cnt++);
  46. endif;
  47. ## Test for off
  48. if ((isstr(str)) && (strcmp(tolower(deblank(str)),"off")) && (nargin == 1))
  49. gset nokey;
  50. replot;
  51. return;
  52. endif;
  53. ## Test for data type (0 -> list of string, 1 -> array of string)
  54. if (length(str) != 0) && (isstr(str(1,:))) && (rows(str) != 1)
  55. data_type = 1;
  56. va_arg_cnt = 1;
  57. data = nth (varargin, va_arg_cnt++);
  58. nb_data = rows(data);
  59. nargin--;
  60. endif;
  61. pos_leg = 0;
  62. ## Get the original plotting command
  63. tmpfilename=tmpnam;
  64. command=["save \"",tmpfilename,"\"\n"];
  65. graw(command);
  66. awk_prog= \
  67. "BEGIN { \
  68. dq = 0; \
  69. format = \"%s\\n\"; \
  70. } \
  71. NF != 0 { \
  72. for (i=1;i<=NF;i++) { \
  73. if ($(i) == \"\\\"\") { \
  74. if (dp == 0) { \
  75. dp = 1; \
  76. if ($(i+1) != \"\\\"\") { \
  77. i++; \
  78. printf (\"%s\", $(i)); \
  79. } \
  80. format = \" %s\"; \
  81. } else { \
  82. dp = 0; \
  83. format = \"%s\\n\"; \
  84. printf (\"\\n\"); \
  85. } \
  86. } else { \
  87. printf (format, $(i)); \
  88. } \
  89. } \
  90. }";
  91. shell_cmd=["grep \"^pl \" " tmpfilename " | " \
  92. "sed -e 's/,/ , /g' -e 's/\"/ \" /g'" " | " \
  93. "awk '" awk_prog "'"];
  94. # wait for the file to appear
  95. attempt=0;
  96. while (isempty(stat(tmpfilename)))
  97. if (++attempt > 20) error("gnuplot is not responding"); endif
  98. usleep(1e5);
  99. end
  100. plot_cmd = split(system(shell_cmd),"\n");
  101. if (~length(deblank(plot_cmd(rows(plot_cmd), :))))
  102. plot_cmd = plot_cmd ([1:rows(plot_cmd)-1],:);
  103. endif;
  104. unlink(tmpfilename);
  105. ## Look for the number of graph
  106. nb_graph = 0;
  107. i = 0;
  108. while (i++ < rows(plot_cmd))
  109. line = deblank(plot_cmd(i,:));
  110. if ((strcmp(line, "pl")) || (strcmp(line, ",")))
  111. nb_graph++;
  112. endif;
  113. endwhile;
  114. ## Change the legend of each graph
  115. new_plot = [];
  116. if (data_type == 0)
  117. va_arg_cnt = 1;
  118. endif;
  119. fig = 0;
  120. i = 1;
  121. while (fig < nb_graph)
  122. ## Get the legend string
  123. if (((data_type == 0) && (nargin <= 0)) || \
  124. ((data_type == 1) && (fig >= nb_data)))
  125. leg = "\"\"";
  126. else
  127. if (data_type == 0)
  128. leg = nth (varargin, va_arg_cnt++) ;
  129. nargin--;
  130. else
  131. leg = data(fig+1,:);
  132. endif;
  133. if (!isstr(leg))
  134. pos_leg = leg;
  135. leg = "\"\"";
  136. elseif (length(deblank(leg)) == 0)
  137. leg = "\"\"";
  138. else
  139. leg=["\"" leg "\""];
  140. endif;
  141. endif;
  142. ## look for the end of the graph command i.e. ","
  143. new_line = [deblank(plot_cmd(i++,:)) " \"" deblank(plot_cmd(i++,:)) "\""];
  144. while ((i <= rows(plot_cmd)) && (!strcmp(deblank(plot_cmd(i,:)), ",")))
  145. if (strcmp(deblank(plot_cmd(i,:)), "t"))
  146. new_line = [new_line " t " leg];
  147. i++;
  148. else
  149. new_line = [new_line " " deblank(plot_cmd(i,:))];
  150. endif;
  151. i++;
  152. endwhile;
  153. if (length(new_plot))
  154. new_plot = [ new_plot new_line];
  155. else
  156. new_plot = new_line;
  157. endif;
  158. fig++;
  159. endwhile;
  160. ## Create a new ploting command
  161. new_plot = [new_plot "\n"];
  162. graw(new_plot);
  163. ## Check for the last argument if we don't already get it
  164. while (nargin-- > 0)
  165. pos_leg = nth (varargin, va_arg_cnt++) ;
  166. if (isstr(pos_leg))
  167. pos_leg = 0;
  168. endif;
  169. endwhile;
  170. ## Change the legend position
  171. if ((is_scalar (pos_leg)) && (isreal(pos_leg)))
  172. switch (pos_leg)
  173. case 1
  174. gset key right top;
  175. case 2
  176. gset key left top;
  177. case 3
  178. gset key left bottom;
  179. case 4
  180. gset key right bottom;
  181. case -1
  182. gset key right top outside;
  183. endswitch;
  184. else
  185. warning ("pos must be a scalar");
  186. endif;
  187. ## Regenerate the plot
  188. replot;
  189. endfunction;