How to increment a value with push button in a GUIDE-based GUI?

2 次查看(过去 30 天)
Hi,
I have made a GUIDE-based GUI with two push botton and an edit text window.
The edit txt window shows a Current Level.
I would like the Current level (handles.Initial_CurrentLevel) to increase with a fixed step size (handles.Initial_Step_Size) when I press the UP botton and to decrease with a fixed step size when I press the DOWN botton.
Can youone help me on this?
Thanks,
Charlotte
function varargout = gui_ThresholdEstimation(varargin)
% GUI_THRESHOLDESTIMATION MATLAB code for gui_ThresholdEstimation.fig
% GUI_THRESHOLDESTIMATION, by itself, creates a new GUI_THRESHOLDESTIMATION or raises the existing
% singleton*.
%
% H = GUI_THRESHOLDESTIMATION returns the handle to a new GUI_THRESHOLDESTIMATION or the handle to
% the existing singleton*.
%
% GUI_THRESHOLDESTIMATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_THRESHOLDESTIMATION.M with the given input arguments.
%
% GUI_THRESHOLDESTIMATION('Property','Value',...) creates a new GUI_THRESHOLDESTIMATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_ThresholdEstimation_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_ThresholdEstimation_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gui_ThresholdEstimation
% Last Modified by GUIDE v2.5 29-Nov-2019 13:41:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_ThresholdEstimation_OpeningFcn, ...
'gui_OutputFcn', @gui_ThresholdEstimation_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gui_ThresholdEstimation is made visible.
function gui_ThresholdEstimation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gui_ThresholdEstimation (see VARARGIN)
% Choose default command line output for gui_ThresholdEstimation
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_ThresholdEstimation wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_ThresholdEstimation_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in UP.
function UP_Callback(hObject, eventdata, handles)
% hObject handle to UP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.Initial_CurrentLevel = 200; % staring current level
handles.Initial_Step_Size = 5; % the step staring current level
%%% SOMETHING IS MISSING HERE
textLabel = sprintf('Current Level = %f', handles.Initial_CurrentLevel);
set(handles.edit1, 'String', textLabel)
% --- Executes on button press in DOWN.
function DOWN_Callback(hObject, eventdata, handles)
% hObject handle to DOWN (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.Initial_CurrentLevel = 200; % staring current level
handles.Initial_Step_Size = 5; % the step staring current level
%%% SOMETHING IS MISSING HERE
textLabel = sprintf('Current Level = %f', handles.Initial_CurrentLevel);
set(handles.edit1, 'String', textLabel);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

回答(1 个)

Navya Seelam
Navya Seelam 2019-12-2
Hi,
You can initialize the Current_Level in the function gui_ThresholdEstimation_OpeningFcn as shown below
function gui_ThresholdEstimation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gui_ThresholdEstimation (see VARARGIN)
% Choose default command line output for gui_ThresholdEstimation
handles.output = hObject;
handles.Initial_CurrentLevel = 100;
handles.Initial_Step_Size = 5;
% Update handles structure
guidata(hObject, handles);
Now, you can update UP_Callback function as follows
function UP_Callback(hObject, eventdata, handles)
% hObject handle to UP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.Initial_CurrentLevel=handles.Initial_CurrentLevel+handles.Initial_Step_Size;
guidata(hObject, handles);
textLabel = sprintf('Current Level = %f', handles.Initial_CurrentLevel);
set(handles.edit1, 'String', textLabel);
Same can be applied to DOWN_Callback function.

类别

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