|
From: | Benjamin Lindner |
Subject: | Re: plot templates and options lists for set, plot etc. |
Date: | Sat, 06 Jun 2009 18:15:17 +0200 |
User-agent: | Thunderbird 2.0.0.18 (Windows/20081105) |
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, ...?
Easier to deal with are cell arrays in the first place, e.g. style = { "linewidth", 2, "marker", "x", "markersize", 12 }; plot(1:5, 5:-1:1, style{:} ); If you want to stick with structures I don't now of a more "elegant" way.
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
this should read function c = struct2opt(s) c = reshape(cat(2, fieldnames(s), struct2cell(s)).',1,[]); endfunction then plot(1:5, 5:-1:1, struct2opt(style){:}) should work benjamin
[Prev in Thread] | Current Thread | [Next in Thread] |