How to set an axes title within the constructor?

3 views (last 30 days)
Hi Guys,
Is it possible to set nested properties, such as a title string, within an axes constructor? For example, I can set the axes x-limits using:
ax = axes('XLim',[0,10])
without any issue. However, I cannot do the same thing with nested properties such as the Title string. Using
ax = axes('Title','Fubar')
doesn't do anything (but doesn't return an error!), whilst
ax = axes('Title.String','Fubar')
returns an error: "There is no Title.String property on the Axes class."
I understand that the axes Title is itself a handle object with its own properties - and that I could set each of these properties in separate steps. What I am looking for here is an elegant (built-in?) way to set multiple axes properties using one command. My approach works for top level properties such as ZLim, XScale etc, but I would like to use a similar approach for nested properties.
Any takers?

Accepted Answer

Jan
Jan on 18 Feb 2016
Edited: Jan on 18 Feb 2016
I do not know a direct method to do this. But you can write an M-file, which checks if the wanted poperty is nested or not and then set the value accoringly.
function AxesH = myAxes(AxesH, varargin)
Args = varargin;
if ~ishghandle(AxesH)
Args = cat(2, {AxesH}, Args)
AxesH = axes();
end
for iArg = 1:2:numel(Args)
switch lower(Args{iArg})
case 'title'
set(get(AxesH, 'Title'), Args{iArg + 2});
...
otherwise
set(AxesH, Args{iArg}, Args{iArg + 1});
end
Now call this:
myAxes('XLim', [0,10], 'Title', 'Fubar')
I cannot test this currently.
  1 Comment
Brendan
Brendan on 18 Feb 2016
Thanks Jan,
This is exactly the sort of thing I was planning on writing, but figured that it was such an obvious thing - it must be part of core Matlab!
OK, if there isn't a built in solution then I'll write something myself.
Thanks!

Sign in to comment.

More Answers (1)

Jos (10584)
Jos (10584) on 18 Feb 2016
You have to pass a handle to a title to the Title property of the axes:
ax = axes('Title',title('Fubar'))
  2 Comments
Brendan
Brendan on 18 Feb 2016
Thanks Jos,
This is a good solution for single properties, but how could it be applied to a series of property value pairs? For example:
function ax = axesWrapper(varargin)
% axesWrapper: construct axes using p-v pairs
ax = axes;
set(ax,varargin{:})
end % axesWrapper
Allows us to assign multiple properties in the wrapper, for example:
axesWrapper('XLim',[0,10],'YLim',[-1,1])
The user can specify the varargin in any order they like so we wouldn't know which properties refer to title, xlabel etc. The different properties would need to be applied in different ways - the 'set' command isn't suitable.
I realise that I may need to write an input parser to handle this - but wanted to check whether there was a built-in solution before going down that route.
Jos (10584)
Jos (10584) on 18 Feb 2016
Edited: Jos (10584) on 18 Feb 2016
With hindsight this is not a good solution at all, as it actually creates two axes on top of each other ...

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!