plot templates and options lists for set, plot etc.
Ben Abbott
bpabbott at mac.com
Sat Jun 6 10:42:23 CDT 2009
On Jun 6, 2009, at 11:21 AM, Thorsten Meyer wrote:
> 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
Would you prefer the approach below?
props = {"color", [0 1 0], "marker", "s"}
h = plot (1:10);
set (h, props{:})
If you want to continue to use your structure, you can populate props
by ...
props = [fieldnames(style), struct2cell(style)];
But that is not really very different from what you're already doing.
Ben
More information about the Help-octave
mailing list