How to use GUI to change a value of a variable and use that variable in a script
9 次查看(过去 30 天)
显示 更早的评论
Hi!
I have a program that opens a GUI while its running, the thing is I want the user to press one of the buttons on the GUI. the user selection should change the value of the variable. and then the main program continue running after the GUI.
my problem is that I don't know how to make a button event and how to get the value of the variable back to the main program?
2 个评论
Adam
2017-8-21
gives an example. You can also do it in GUIDE, using the OutputFcn and a modal GUI utilising uiwait to ensure the OutputFcn only triggers when you close the dialog.
回答(2 个)
Jan
2017-8-21
You can either use GUIDE or create the GUI prgrammatically by code. Perhaps a simple inputdlg, questdlg or listdlg is sufficient already. See
doc inputdlg
doc questdlg
doc listdlg
Creating an own dialog and obtaining a value is easy also:
function Reply = myFirstGUI
FigH = figure('Position', [100, 100, 400, 300], ...
'Menubar', 'none');
ButtonH = uicontrol('Style', 'PushButton', 'String', 'Click me!', ...
'Position', [20, 20, 360, 30], ...
'Callback', {@myCallback, FigH});
disp('Waiting for the figure to close...');
uiwait(FigH);
disp('Waiting was resumed...');
delete(FigH);
Reply = '???' % You did not explain, how the GUI creates the value to be replied
end
function myCallback(ButtonH, EventData, FigH)
disp('Button was pressed.');
uiresume(FigH);
end
Now call this from the main function:
Reply = myFirstGUI();
Walter Roberson
2017-8-21
Example:
faster_button = uicontrol('style','push', 'UserData', 0, 'callback', @faster);
and
function faster(hObject, event)
current_speed = get(hObject, 'UserData');
current_speed = current_speed + 10;
set(hObject, 'UserData', current_speed);
and
while true
current_speed = get(faster_button, 'UserData');
x = x + current_speed;
scatter(x, current_speed);
hold on;
pause(1); %also triggers drawing
end
Note here that pushing the button does not force the script to change the value it is using: the script has to ask what the current value is.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!