How to get the tag name?

I want to use the following code to find the tag name of the axes1
axes(handles.axes1);
Tag = get(gca,'Tag');
but it returns an empty matrix.Is there anything wrong?

回答(2 个)

Sven
Sven 2013-2-27
编辑:Sven 2013-2-27
To find the tag of axes1, you can just get it directly:
Tag = get(handles.axes1,'Tag')
However I would expect that the code you had (set the current axes to handles.axes1 and then get the current axes tag) would also work.
Are you sure that this axes actually has a tag set? If no tag has been set, it will return an empty string.
UPDATE BASED ON COMMENT:
Aha! The scatter() function (like most of the high-level plotting functions), when called on an axis that is not held, will internally clear the axis (deleting it and its Tag) before plotting to a freshly made axes.
figure, plot(3,4), set(gca,'Tag','qqqqq')
get(gca,'Tag')
scatter(4,3)
get(gca,'Tag')
Instead, you can call hold the axes before you call scatter, which will keep your old axes including its axes tag:
figure, plot(3,4), set(gca,'Tag','qqqqq')
get(gca,'Tag')
hold on
scatter(4,3)
get(gca,'Tag')
If you still need to work from a clean slate (ie, you want to clear the axis, but keep its Tag), then you can selectively delete all children of that axis:
delete(get(gca,'Children'))
scatter(...)

4 个评论

QiQin Zhan
QiQin Zhan 2013-2-27
编辑:QiQin Zhan 2013-2-27
There are two callback functions in my code.The 'pushbutton_plot_Callback' plot some scatters on the axes1,while 'pushbutton_getTag_Callback' get the tag of axes1 and axes2. And nothing is done to the axes2.After I test the code,I find that I can get the tag of axes2,but I can't get the tag of axes1?Why is it so strange?
function pushbutton_plot_Callback(hObject, eventdata, handles)
X = rand(1,10);Y = rand(1,10);
axes(handles.axes1);
scatter(X,Y,gca);hold on
function pushbutton_getTag_Callback(hObject, eventdata, handles)
Tag = get(handles.axes1,'Tag');
Tag2 = get(handles.axes2,'Tag');
Hi Chan, I've updated my answer. Does it clear up what's happening?
Instead of "hold on" you can set the required property directly also:
function pushbutton_plot_Callback(hObject, eventdata, handles)
X = rand(1,10);Y = rand(1,10);
set(handles.axes1, 'NextPlot', 'add'); % "hold on" *before* scatter!
scatter(handles.axes1, X, Y);
@Jan Simon: Thank you very much!You just give me the exact answer I want!

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

标签

提问:

2013-2-27

Community Treasure Hunt

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

Start Hunting!

Translated by