Does anyone know how to get the figure handle and subsequently get the axes labels, legend and x/y axes data for Matlab 2012
65 次查看(过去 30 天)
显示 更早的评论
Does anyone know how to get the figure handle and subsequently get the axes labels, legend and x/y axes data in Matlab 2012.
0 个评论
采纳的回答
Walter Roberson
2016-3-29
In your previous question http://www.mathworks.com/matlabcentral/answers/275613-how-to-get-the-x-axis-label-from-figure-handle you were using hgload() . That returns the figure handle. Are you no longer using hgload?
If you are not using hgload, then you might be dealing with any of several figures.
If you have only one figure and it was created in typical manner, and it is visible, then the handle for it is returned by gcf().
Otherwise,
fighandles = findall( allchild(0), 'type', 'figure');
and now fighandles will be a vector of 0 or more figure handles (0 if there are no figures) which you need to decide between somehow
Once you have decided which figure handle you are working with, then you might have any number of axes in it. If you have legends and/or colorbar then in R2014a and before those are implemented as axes, so you need to figure out which axes are which. For figure "fig", the axes can be found by
fig = fighandles(1); %or as appropriate
allaxes = findall(fig, 'type', 'axes');
The legend axes will have 'Tag', 'legend' and the colorbar axes will have 'Tag', 'Colorbar', so you can break them out more directly with
all_legend = findall(fig, 'type', 'axes', 'Tag', 'legend');
all_colorbar = findall(fig, 'type', 'axes', 'Tag', 'Colorbar');
all_plotting_axes = setdiff(allaxes, union(all_legend, all_colorbar));
For any given legend handle, the associated plotting axes can be determined:
legend_handle = all_legend(1); %or as appropriate
legend_ud = get( legend_handle, 'UserData');
legend_associated_axes = legend_ud.PlotHandle;
However, this does not generalize to colorbar. There is a method which works for both:
the_handle = all_legend(1); %or a colorbar handle
associated_axes = get(the_handle, 'axes'); %this is a hidden property!
Okay, now for any given axes:
plot_ax = all_plotting_axes(1); %or as appropriate, can also be used for legend and colorbar
xlab = get(get(plot_ax,'XLabel'),'String');
ylab = get(get(plot_ax,'YLabel'),'String');
zlab = get(get(plot_ax,'ZLabel'),'String');
Any given axes will have one xlabel, one ylabel, and one zlabel (any of which might be the empty string.)
Any given plotting axes may have multiple objects that have xdata and ydata, and the fact that you bothered with legends makes it likely that you will indeed have multiple such objects. You can get it all:
xydata = get(findall(plot_ax,'-property','xdata'), {'xdata', 'ydata'});
This will return an N x 2 cell array, where N is the number of objects with xdata. Each row will have data for one object. Column 1 will have the xdata and column 2 will have the ydata. To distinguish between them you might want to add 'type' to the list of properties to fetch, as that will give you clues about what additional properties of interest might be available -- for example lines and surfaces and patches have 'linestyle' and 'marker' properties but histograms do not.
Note that text() objects do not have xdata or ydata properties: they have Position and String properties.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!