查找具有特定模式的句柄

时间:2014-02-07 13:24:24

标签: matlab user-interface matlab-figure handles

我有一些看起来像这样的句柄:

ans = 

                    figure1: 189.0205
             sampleNameEdit: 17.0216
             selectCatPopup: 16.0237
             radiobutton_Al: 14.0266
              radiobutton_O: 190.0183
                     output: 189.0205

如何轻松查找以radiobutton__开头的句柄?在未来,我将有更多的轻量级按钮,我想轻易找回。

3 个答案:

答案 0 :(得分:3)

要在句柄名称中搜索特定模式,最好的方法是遵循Luis Mendo's advice in his answer。但是,请考虑根据您要查找的对象类型进行搜索。在这种情况下,似乎需要找到所有radiobutton个对象。

查找特定样式uicontrol句柄的最直接方法是搜索Style Property设置为radiobutton的句柄。考虑一个带有两个控件的figure:一个文本框和一个单选按钮,其句柄存储在普通数组uih中:

hf = figure; % parent of uicontrols
uih(1) = uicontrol('style','text');
uih(2) = uicontrol('style','radiobutton');

以下是我的测试句柄vales(你的不同):

>> uih
uih =
    3.0012    4.0012

第一个是文本框,第二个是单选按钮。

按父句柄搜索

如果你有父句柄(即图形句柄,hf),你甚至不需要句柄列表uih!只需按以下方式致电findobj

hr = findobj(hf,'style','radiobutton')
hr =
    4.0012

搜索句柄数组

如果您没有父句柄,但确实有搜索句柄列表,那也没问题:

hr = findobj(uih,'style','radiobutton')
hr =
    4.0012

搜索句柄struct

在您的情况下,您将句柄存储为struct

中的字段
handles = 
    ht: 3.0012
    hr: 4.0012
hr = findobj(structfun(@(x)x,handles),'style','radiobutton')
hr =
    4.0012

别担心,这会找到所有单选按钮!

答案 1 :(得分:2)

假设你有一个结构,每个句柄存储在不同的字段中:

s.radiobutton_1 = 1; %// example data: struct with several fields
s.otherfield    = 22;
s.radiobutton_2 = 333;

names = fieldnames(s); %// get all field names
ind = strmatch('radiobutton_',names); %// logical index to matching fields
selected = cellfun(@(name) s.(name), names(ind)); %// contents of those fields

返回所需的结果:

selected =

     1
   333

答案 2 :(得分:1)

目前还不清楚你从哪里得到ans及其结构是什么,以及究竟应该以“radiobutton_”开头。但是,如果您想获得所有现有无线电按钮的句柄,findobj是可行的方法:

h = findobj(findobj('Type', 'uicontrol'), 'Style', 'radiobutton');

您可以将搜索限制为儿童,例如当前数字使用

h = findobj(findobj(gcf, 'Type', 'uicontrol'), 'Style', 'radiobutton');

您可以使用

将搜索限制在给定的句柄列表oh(没有孩子)
h = findobj(findobj(oh, 'flat', 'Type', 'uicontrol'), 'Style', 'radiobutton');
相关问题