필터 지우기
필터 지우기

How to update a "child" figure?

조회 수: 6 (최근 30일)
Paul Kelly
Paul Kelly 2012년 1월 17일
I have a manually generated gui which consists of a parent figure with a number of child figures laid out on uipanels.
If I click something in one of the child figure I can cause something to happen in the parent figure using a figure handle passed to the child figure as an argument when I call the function that creates it.
Is there are way of triggering in the other direction, i.e. once the child has been created, if I click something in the parent figure how do I cause something to happen in the child figure?

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 1월 17일
Save the handle to the child figure in appdata and set it as necessary:
setappdata(hParent,'hChild1',hChild);
On click:
hChild = getappdata(hParent,'hChild');
set(hChild,...)
  댓글 수: 3
Paul Kelly
Paul Kelly 2012년 1월 17일
sorry *right
Sean de Wolski
Sean de Wolski 2012년 1월 17일
Sure you can save the figure handle in appdata, that's what my above setappdata command did.
doc setappdata/doc getappdata for more info

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul Kelly
Paul Kelly 2012년 1월 18일
Thanks Sean, I understood your example but I missed some details.
For anyone who is interested, this is the example I created to check how to do it.
This is the parent GUI
function exampleParentGUI()
% Create the GUI parent
hParent = figure( ...
'Name', 'Parent', ...
'NumberTitle', 'off', ...
'Units', 'pixels', ...
'Position',[200 200 640 480], ...
'MenuBar', 'none', ...
'Resize', 'off');
% Place a button on the parent
uicontrol(hParent, ...
'Style', 'pushbutton', ...
'Units', 'pixels ', ...
'Position', [12, 12, 96, 24], ...
'String', 'Parent action', ...
'Callback', {@callback_ParentAction});
% Allocate an area for the child (normalised units)
childPos = [0 0.5 1 0.5];
createChildPanel(hParent, childPos)
% Grab the function handle for the child function
parentDoSomething = getappdata(hParent, 'childActionFn');
function callback_ParentAction(hObj, ~)
parentAct = ...
sprintf('Someone clicked the %s button\n', get(hObj, 'String'));
parentDoSomething(parentAct)
end
end
and this is the child
function createChildPanel(hParentArg, normalisedPos)
% Create panel
childPanel = uipanel(hParentArg, ...
'Units', 'normalized ', ...
'Position', normalisedPos, ...
'BackgroundColor', 'red');
% Place a button on the child panel
uicontrol(childPanel, ...
'Style', 'pushbutton', ...
'Units', 'pixels ', ...
'Position', [12, 12, 96, 24], ...
'String', 'Child action', ...
'Callback', {@callback_ChildAction});
% Place a static text
hText = uicontrol(childPanel, ...
'Style', 'text', ...
'Units', 'pixels ', ...
'Position', [120, 12, 96*5, 24], ...
'HorizontalAlignment', 'left', ...
'String', 'Nobody has done anything yet');
% Make childDoSomething available to the parent
setappdata(hParentArg, 'childActionFn', @childDoSomething);
function childDoSomething(childAct)
set(hText, 'String', childAct);
end
function callback_ChildAction(hObj, ~)
actStr = ...
sprintf('Someone clicked the %s button\n', get(hObj, 'String'));
childDoSomething(actStr)
end
end

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by