Nested GUI functions to get information

Hey all,
I'm creating a series of GUI-element windows to work through processing a series of different data types. I've been using what is essentially the following process, with information gathered from 'previous' dialog boxes in the information flow used to influence the results of the 'next' dialog boxes:
function resultNumber=testNestedGUIData()
multiplyNumber=0;
initialNumber=5;
resultNumber=0;
theResult='Please select a new number';
optionsToProcess='';
figure('Tag','firstFigure','Position',[50 50 400 125]);
uicontrol('Tag','getInputInformation','Style','edit','String',... initialNumber,'Position',[5 100 390 20]);
uicontrol('Tag','processInputInformation','Style','pushbutton','String','Process','Position',[5 75 390 20],'Callback',@processTheInformation);
uicontrol('Tag','addStuff','Style','pushbutton','String','Add','Position',[100 50 200 20],'Callback',@addTheNumbers);
uicontrol('Tag','resultsOfInformation','Style','text','String',theResult,'Position',[5 25 390 20]);
uicontrol('Tag','closeStuff','Style','pushbutton','String','Close','Position',[100 5 200 20],'Callback',@closeTheBox);
function processTheInformation(hObj,event)
if initialNumber<50
optionsToProcess='5|10';
whichClassOfProcess=1;
else
optionsToProcess='50|100|200|500';
whichClassOfProcess=2;
end
multiplyNumber=selectMultiplyNumber(optionsToProcess,whichClassOfProcess);
end
function addTheNumbers(hObj,event)
if multiplyNumber~=0
resultNumber=multiplyNumber*initialNuber;
theResult=num2str(resultNumber);
set(findobj('Tag','resultsOfInformation'),'String',theResult);
else
warndlg('Please press the process button first');
end
end
function closeTheBox(hObj,event)
close(findobj('Tag','firstFigure'));
end
end
function multiplyNumber=selectMultiplyNumber(optionsToProcess,whichClassOfProcess)
funkyNumber=0;
figure('Tag','secondFigure','Position',[75 75 200 50]);
uicontrol('Tag','secondFigurePopup','Style','popup','Position',[5 25 190 20],'String',optionsToProcess,'Callback',@selectAnOption);
uicontrol('Tag','secondFigureDone','Position',[5 5 190 20],'String','Done','Callback',@finishSelection);
function selectAnOption(hObj,event)
val=get(hObj,'Value');
if whichClassOfProcess==1
switch val
case 1
funkyNumber=5;
case 2
funkyNumber=10;
end
else
switch val
case 1
funkyNumber=50;
case 2
funkyNumber=100;
case 3
funkyNumber=200;
case 4
funkyNumber=500;
end
end
end
function finishSelection(hObj,event)
if funkyNumber==0
warndlg('Please select an option');
else
multiplyNumber=funkyNumber;
close(findobj('Tag','secondFigure'));
end
end
end
However, the 'child' GUI doesn't seem to define the data in time to return any data to the 'parent' GUI, presumably since the return data is dependent on some kind of user input. Is there a workaround that would allow this kind of thing?
Thanks for your help! I've been searching for solutions for this, but haven't found a lot (maybe I am searching under the wrong topics?)

回答 (1 件)

Voss
Voss 2021 年 12 月 27 日

0 投票

You are exactly right about the reason this happens. The function that creates the 'child' GUI (selectMultiplyNumber) executes and does not return anything to the parent function, because the output variable (multiplyNumber) has not been defined yet, because it is set in a function which has not yet been called at the time the child GUI is created.
One solution is to use uiwait() on the child GUI. That is, to pause execution until the child GUI is deleted (presumably due to a valid selection - but in general you would need to check that and handle invalid/missing selections) or until uiresume() is called on the child GUI.
See below for modified code using uiwait().
function resultNumber=testNestedGUIData()
multiplyNumber=0;
initialNumber=5;
resultNumber=0;
theResult='Please select a new number';
optionsToProcess='';
figure('Tag','firstFigure','Position',[50 50 400 125]);
uicontrol('Tag','getInputInformation','Style','edit','String',initialNumber,'Position',[5 100 390 20]);
uicontrol('Tag','processInputInformation','Style','pushbutton','String','Process','Position',[5 75 390 20],'Callback',@processTheInformation);
uicontrol('Tag','addStuff','Style','pushbutton','String','Add','Position',[100 50 200 20],'Callback',@addTheNumbers);
uicontrol('Tag','resultsOfInformation','Style','text','String',theResult,'Position',[5 25 390 20]);
uicontrol('Tag','closeStuff','Style','pushbutton','String','Close','Position',[100 5 200 20],'Callback',@closeTheBox);
function processTheInformation(hObj,event)
if initialNumber<50
optionsToProcess='5|10';
whichClassOfProcess=1;
else
optionsToProcess='50|100|200|500';
whichClassOfProcess=2;
end
multiplyNumber=selectMultiplyNumber(optionsToProcess,whichClassOfProcess);
end
function addTheNumbers(hObj,event)
if multiplyNumber~=0
resultNumber=multiplyNumber*initialNumber;
theResult=num2str(resultNumber);
set(findobj('Tag','resultsOfInformation'),'String',theResult);
else
warndlg('Please press the process button first');
end
end
function closeTheBox(hObj,event)
close(findobj('Tag','firstFigure'));
end
end
function multiplyNumber=selectMultiplyNumber(optionsToProcess,whichClassOfProcess)
funkyNumber=0;
f = figure('Tag','secondFigure','Position',[75 75 200 50]);
uicontrol('Tag','secondFigurePopup','Style','popup','Position',[5 25 190 20],'String',optionsToProcess,'Callback',@selectAnOption);
uicontrol('Tag','secondFigureDone','Position',[5 5 190 20],'String','Done','Callback',@finishSelection);
uiwait(f);
function selectAnOption(hObj,event)
val=get(hObj,'Value');
if whichClassOfProcess==1
switch val
case 1
funkyNumber=5;
case 2
funkyNumber=10;
end
else
switch val
case 1
funkyNumber=50;
case 2
funkyNumber=100;
case 3
funkyNumber=200;
case 4
funkyNumber=500;
end
end
end
function finishSelection(hObj,event)
if funkyNumber==0
warndlg('Please select an option');
else
multiplyNumber=funkyNumber;
close(findobj('Tag','secondFigure'));
end
end
end

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

質問済み:

2011 年 7 月 28 日

回答済み:

2021 年 12 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by