/trunk/octave-forge/extra/gnuplot/inst/g_data.m

# · Objective C · 145 lines · 128 code · 17 blank · 0 comment · 23 complexity · b4da6e3d4fb2f7a836ca147fa0eca297 MD5 · raw file

  1. ### Copyright (c) 2007, Tyzx Corporation. All rights reserved.
  2. function g = g_data (g, varargin)
  3. ### g = g_data (g, <options>, <filename>, <matrices>, ... )
  4. ###
  5. ### ARGUMENTS:
  6. ### <filename>, <mat1>,... : Write <mat1>,... (they should all have same width)
  7. ### in a file named <string>. Matrices are separated by an
  8. ### empty line (so that gnuplot chops lines up), unless the
  9. ### "-join" option is used.
  10. ###
  11. ### OPTIONS:
  12. ### "-step", <num> : Write a blank line every <num> lines in the next
  13. ### written file (so that gnuplot chops lines up)
  14. ###
  15. ### "-stops",[pos1, pos2,...] : Put a blank line after the pos1'th row, pos2'th
  16. ### row etc.
  17. ###
  18. ### "-join", : Do not put an empty line between the various matrices
  19. ### of the next file.
  20. ##
  21. ### "-label", yesno :
  22. ###
  23. ### "-uint8", : Save data as uint8 (e.g. to plot as image later).
  24. ###
  25. _g_check (g);
  26. if nargout<1, error ("g_data called in void context"); endif
  27. i = 1;
  28. step = 0; # Size of blocks; 0 means all
  29. join = 0;
  30. data = [];
  31. stops = 0;
  32. raw_data = 0;
  33. chosen_stops = 0;
  34. label = 0;
  35. while i <= length(varargin)
  36. name = varargin{i++};
  37. if !ischar (name)
  38. error ("Data arg %i is not char, but %s", i-1, typeinfo (name));
  39. endif
  40. if strcmp (name,"-step") # Step option
  41. step = varargin{i++};
  42. continue;
  43. elseif strcmp (name,"-stops") # Step option
  44. chosen_stops = [0,varargin{i++}(:)'];
  45. continue;
  46. elseif strcmp (name,"-join") # Step option
  47. join = 1;
  48. continue;
  49. elseif strcmp (name,"-uint8") # Step option
  50. raw_data = 1;
  51. continue;
  52. elseif strcmp (name,"-label") # Step option
  53. label = varargin{i++};
  54. continue;
  55. elseif name(1) == "-" # Bad option
  56. error ("Unknown option '%s'",name);
  57. endif
  58. # It's data
  59. while i <= length(varargin) && isnumeric (varargin{i})
  60. stops = [stops,rows(varargin{i})];
  61. if isempty (data)
  62. data = varargin{i};
  63. else
  64. if columns (varargin{i}) == columns (data)
  65. data = [data;varargin{i}];
  66. else
  67. error ("Data was %i columns wide until now, but arg %i is %i wide",\
  68. columns(data), i, columns(varargin{i}));
  69. endif
  70. endif
  71. i++;
  72. ##size(data)
  73. endwhile
  74. stops = cumsum(stops);
  75. # NOPE! Assume that a column is meant when a single
  76. # row was passed.
  77. ##if rows(data)==1, data = data(:); endif
  78. if !raw_data
  79. ##save("-ascii", [g.dir,"/",name], "data");
  80. if join
  81. if step, stops = [0:step:rows(data), rows(data)];
  82. else stops = [0, rows(data)];
  83. endif
  84. else
  85. if step,
  86. tmp_stops = zeros(1,0);
  87. for j = 2:length(stops)
  88. tmp_stops = [tmp_stops,stops(j-1):step:stops(j),stops(j)];
  89. endfor
  90. stops = tmp_stops;
  91. endif # If step == 0, stops are fine as they are
  92. endif
  93. if length (chosen_stops) > 1, stops = chosen_stops; endif
  94. ibad = find (any (isnan(data') | isinf(data')));
  95. if length (ibad)
  96. printf ("Getting rid of some NaN or Inf data\n");
  97. igood = find (all (!isnan(data') & !isinf(data')));
  98. data = data(igood,:);
  99. stop0 = zeros (1,1+rows(data));
  100. if !isempty (stops),
  101. stop0(stops+1) = 1;
  102. end
  103. stop0(ibad) = 1;
  104. stops = cumsum(!stop0)(find (stop0));
  105. endif
  106. stops = unique (stops);
  107. _g_save_data ([g.dir,"/",name], data, stops,label);
  108. else # Raw data
  109. filename = [g.dir,"/",name];
  110. [fid, msg] = fopen (filename,"w");
  111. if fid<0,
  112. error ("Can't open raw data file '%s': %s", filename, msg);
  113. endif
  114. count = fwrite (fid, uint8(data), "uint8");
  115. if count != prod (size (data))
  116. error ("Could only write %i out of %i bytes of data.",\
  117. count, prod (size (data)));
  118. endif
  119. fclose (fid);
  120. endif
  121. # Reset step etc
  122. step = 0;
  123. join = 0;
  124. data = [];
  125. stops = 0;
  126. raw_data = 0;
  127. chosen_stops = 0;
  128. endwhile