How do I retrieve object handles from a figure that was opened with a callback function?

5 Ansichten (letzte 30 Tage)
So I'm trying to make a GUI. I have my main figure, with an edit box to type the number of inputs that I need, and a button whose callback function creates another figure with the required number of inputs. I can't seem to access any of the object handles in this second figure. findobj and findall doesn't seem to return the actual handle name that I can use a get() function on. How do I access object handles that weren't created with the main code?
Here's a striped down version of what I'm trying to do. Here's the main program code:
fig = figure('position',[300 50 1000 500],'Name','Figure 1');
% Enter number of inputs needed
inputNumber = uicontrol('Parent',fig,'style','edit','units','normalized','position',[0.4 .6 0.2 0.05],'callback',@inputFunction);
The inputFunction then creates a new figure with the number of inputs designated by the user.
function = inputFunction(object_handle,event)
% Retrieve number of inputs needed
N = str2double(get(object_handle,'String'));
% Create new figure
input = figure('position',[300 50 350 500],'Name','Figure 2');
% Create N number of input boxes
for i = 1:N
inputTime(i,1) = uicontrol('Parent',input,'style','edit','position',[50 475-30*i 100 25]);
end
Now when I try to retrieve data from the inputTime objects, the get() function says inputTime doesn't exist.

Antworten (1)

Walter Roberson
Walter Roberson am 8 Apr. 2016
findobj and findall never return handle names only handles . With R2014a or earlier those handles look like doubles; with R2014b or later those handles do not have an external representation and will display out as brief summaries that might not provide any individually identifying information.
If you have an R2014b or later handle and you want to convert it to some kind of visually distinct information, then at least for current releases you can double() the handle and you will get back something that looks like the earlier handles.
Remember, if you have a handle then you can get() its Tag or UserData -- though those of course are not guaranteed to be unique.
Possibly you are accustomed to programming in GUIDE, which offers the convenience of organizing most graphics objects in the "handles" structure. The code that GUIDE uses for that is only used once the graphics objects from the .fig are finished opening, so only the statically built objects are recorded in the "handles" structure. The field name of "handles" that are used correspond to the Tag field of the object; any graphics object whose Tag is empty or is not valid field name will not be recorded in "handles" in this way.
This creation of field names in the "handles" structure is not maintained dynamically; if you create a new object at run-time then if you want it put into "handles" then you need to create a field yourself (and remember to use guidata() to update the master copy.)
The field names used are not really handle "names": they are just convenient ways to get to the graphic handles, an organizing method. Handles do not have "names". (Well, other than that figures can have a Name property which can be any string and will be displayed in the figure border.)
  2 Kommentare
Alec Holmes
Alec Holmes am 8 Apr. 2016
Bearbeitet: Alec Holmes am 8 Apr. 2016
Thank you for the reply. My problem was that when I created an object in my second figure, the get() function didn't work on that handle. It would say the variable doesn't exist. My best guess is because the object wasn't created in the main program code, it was created by a callback function.
Here's a striped down version of what I'm trying to do. Here's the main program code:
fig = figure('position',[300 50 1000 500],'Name','Figure 1');
% Enter number of inputs needed
inputNumber = uicontrol('Parent',fig,'style','edit','units','normalized','position',[0.4 .6 0.2 0.05],'callback',@inputFunction);
The inputFunction then creates a new figure with the number of inputs designated by the user.
function = inputFunction(object_handle,event)
% Retrieve number of inputs needed
N = str2double(get(object_handle,'String'));
% Create new figure
input = figure('position',[300 50 350 500],'Name','Figure 2');
% Create N number of input boxes
for i = 1:N
inputTime(i,1) = uicontrol('Parent',input,'style','edit','position',[50 475-30*i 100 25]);
end
Now when I try to retrieve data from the inputTime objects, the get() function says inputTime doesn't exist.
I think I found a workaround though, using uiwait. Instead of opening a new figure for the inputs, I wait until the user dictates the number of inputs I need, and then build those inputs on the main interface.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by