help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

plot templates and options lists for set, plot etc.


From: Thorsten Meyer
Subject: plot templates and options lists for set, plot etc.
Date: Sat, 06 Jun 2009 17:21:57 +0200
User-agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103)

Hi,

at work, I often have to generate the same kind of plots with particular labels,
line styles, etc.. I would like to create templates for these plots. For this, I
 collect plot options in a struct like it is returned by the get function. E.g.
  style = struct("linewidth", 2, "marker", "x", "markersize", 12);

Now, I want to pass these options to the plot function. At the moment, I do it
like this:
  plot(1:5, 5:-1:1, {fieldnames(style)'{:}; struct2cell(style)'{:}}{:});

Or:
  h=plot(1:5, 5:-1:1);
  set(h, {fieldnames(style)'{:}; struct2cell(style)'{:}}{:});

Does anyone know a more elegant way of converting a structure to a cs-list of
key, value, ...?

Also, I would like to wrap this transformation into a function. I tried it like
this (in octave compiled from the current tip):

function varargout = struct2options (structure)
  varargout = {fieldnames(structure)'{:}; struct2cell(structure)'{:}};
  nargout
endfunction

And, indeed, the function works in a way:
  [a, b, c, d] = struct2options(style)
  ans = 4
  a = linewidth
  b =  2
  c = marker
  d = x

However, used within a plot command, the following happens:
octave:177> plot(1:5, 5:-1:1, struct2options(style))
ans =  1
error: plot: properties must appear followed by a value
error: called from:
error:   /home/thorsten/local/share/octave/3.1.55/m/plot/__plt__.m at line 62,
column 8

Apparently, in that context, the function struct2options settles on delivering
only one return argument.
Is there any way to force a function to deliver all its output arguments? I
tried to set nargout within the function but that didn't change anything.

Apart from that, whouldn't it be good to extend the syntax of the plot functions
(or at least the set function) such that they also accept options in a
structure? Then, the above commands could also be:
  plot(1:5, 5:-1:1, style)
respectively
  h = plot(...)
  set(h, style)

regards

Thorsten



reply via email to

[Prev in Thread] Current Thread [Next in Thread]