How to use GUI to change a value of a variable and use that variable in a script

12 ビュー (過去 30 日間)
Nora Khaled
Nora Khaled 2017 年 8 月 21 日
コメント済み: Nora Khaled 2017 年 8 月 22 日
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
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.
Nora Khaled
Nora Khaled 2017 年 8 月 22 日
thank you

サインインしてコメントする。

回答 (2 件)

Jan
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
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 ExchangeDialog Boxes についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by