필터 지우기
필터 지우기

Increment a variable every time a push button is pressed:

조회 수: 11 (최근 30일)
Mohammad Ayoub
Mohammad Ayoub 2017년 12월 2일
편집: Jan 2017년 12월 2일
Greetings.
If I want to create a simple GUI which contains only one push button, and each time I press this push button I want a variable x to be increased by 1, how can I do that in MATLAB? I have worked with simple GUIs but I am not that professional. (Please note that I want the variable x to increase, not a display in the interface itself.)
Thanks in advance.
Regards.
  댓글 수: 4
Rik
Rik 2017년 12월 2일
In the callback you can load and save data with guidata, so make x a field in that struct and you can easily do this.
Walter Roberson
Walter Roberson 2017년 12월 2일
In MATLAB, a GUI is technically a MATLAB figure() . Could you confirm that you want to associate the value x with a figure "somehow" ?

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

답변 (2개)

Walter Roberson
Walter Roberson 2017년 12월 2일

Jan
Jan 2017년 12월 2일
편집: Jan 2017년 12월 2일
This is such easy, that GUIDE is not needed.
function FigH = CreateTheGUI
FigH = figure;
handles.X = 0;
handles.button = uicontrol('Style', 'PushButton', 'String', 'Increase X', ...
'Units', 'normalized', 'Position', [0.1, 0.1, 0.8, 0.1], ...
'FontSize', 20, 'Callback', @buttonCB);
guidata(FigH, handles);
end
function buttonCB(ButtonH, EventData)
handles = guidata(ButtonH);
handles.X = handles.X + 1;
guidata(ButtonH, handles);
end
Now call this e.g. from the command line:
FigH = CreateTheGUI;
Press the button how often you want and obtain the current value:
handles = guidata(FigH);
disp(handles.X)

카테고리

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