user3283995
user3283995

Reputation: 11

Find handles with a specific pattern

I have some handles that look like:

ans = 

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

How can I easily look for those handles that start with radiobutton__? In the future, I will have many more radiobuttons that I would like to retrieve easily.

Upvotes: 1

Views: 163

Answers (3)

chappjc
chappjc

Reputation: 30589

To search for a specific pattern in a handle's name, the best approach would be to follow Luis Mendo's advice in his answer. However, consider searching based on the type of object you are looking for. In this case, it would seem that all radiobutton objects need to be located.

The most straightforward way to find handles of for a certain style of uicontrol, is to search for handles with their Style Property set to radiobutton. Consider a figure with two controls: a text box and a radio button, with their handles stored in an ordinary array uih:

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

Here are my test handle vales (yours will be different):

>> uih
uih =
    3.0012    4.0012

The first is the text box, and the second is the radio button.

Search by parent handle

If you have the parent handle (i.e. the figure handle, hf), you don't even need the list of handles uih! Just call findobj as follows:

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

Search handle array

If you don't have the parent handle, but do have a list of handles to search, that's no problem either:

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

Search struct of handles

In your case, you have the handles stored as fields in a struct:

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

Don't worry, this finds all radio buttons!

Upvotes: 3

Luis Mendo
Luis Mendo

Reputation: 112759

Assuming you have a struct with each handle stored in a different field:

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

returns the desired result:

selected =

     1
   333

Upvotes: 2

A. Donda
A. Donda

Reputation: 8476

It's not really clear where you get that ans from and what its structure is, and what exactly is supposed to "start with" "radiobutton_". However, if you want to get handles to all existing radiobuttons, findobj is the way to go:

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

You can restrict the search to children e.g. of the current figure using

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

and you can restrict the search to a given list of handles oh (without children) using

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

Upvotes: 1

Related Questions