I actually found the issue. Buy using `handles.ampmin` as name of the variable I was effectively overwriting the `uicontrol` handle thus causing the described behavior. By changing the name of the variable to `handles.ampminvalue`, the behavior changed to the expected one.
Matlab: set default values for GUI edit text and use them in push button callback
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrea Vaccari
am 14 Aug. 2015
Beantwortet: Andrea Vaccari
am 14 Aug. 2015
I'm trying to initialize a GUI (built with GUIDE) with default values and then, if the user does not change the defaults, use these values in a function triggered by a push button callback.
To do this, inside the `_CreateFcn`, I first store the default values within the `handles`, then set the default of the GUI with `set(hObject, ...)` and finally update the guidata with `guidata(hObject, handles);`
If the user changes the value, I store the updated value in the handles inside the `_Callback` function reading the value with `get(hObject, ...)` and updating the guidata with `guidata(hObject, handles);`
When the button is pushed, inside the button `_Callback` function, I extract the value from the `handles`.
What happen is the following:
- if the user *does not* change the value on the GUI and simply pushes the button, what I read out from the variable stored in the handles is *not* the value of the variable, but the actual handle to the variable. This will return, for example: `27.0098876953125` or, for the newer versions -R2015b-:
UIControl (ampmin) with properties:
Style: 'edit'
String: '1'
BackgroundColor: [1 1 1]
Callback: @(hObject,eventdata)GUI('ampmin_Callback',hObject,eventdata,guidata(hObject))
Value: 0
Position: [15.6000 14.6154 10.2000 1.6923]
Units: 'characters'
Use get to show all properties
- If, on the other hand, the user *does* change the value before pushing the button, then everything works fine and I get the actual variable value.
What am I missing?
UPDATE
I've noticed the following behavior:
- If I start the `.fig` file, the behavior is as expected
- If I start the `.m` file by pushing the `run` button, then I observe the strange behavior described above.
Here is the code (I've also attached a copy of the `.m` and `.fig` files):
function varargout = GUI2(varargin)
% GUI2 MATLAB code for GUI2.fig
% GUI2, by itself, creates a new GUI2 or raises the existing
% singleton*.
%
% H = GUI2 returns the handle to a new GUI2 or the handle to
% the existing singleton*.
%
% GUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI2.M with the given input arguments.
%
% GUI2('Property','Value',...) creates a new GUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI2 before GUI2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI2_OpeningFcn via varargin.
%
% *See GUI2 Options on GUIDE Tools menu. Choose "GUI2 allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI2
% Last Modified by GUIDE v2.5 14-Aug-2015 10:39:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI2_OpeningFcn, ...
'gui_OutputFcn', @GUI2_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 GUI2 is made visible.
function GUI2_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 GUI2 (see VARARGIN)
% Choose default command line output for GUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI2_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 startAnalysis.
function startAnalysis_Callback(hObject, eventdata, handles)
% hObject handle to startAnalysis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp(handles.ampmin)
function ampmin_Callback(hObject, eventdata, handles)
% hObject handle to ampmin (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 ampmin as text
% str2double(get(hObject,'String')) returns contents of ampmin as a double
handles.ampmin = str2double(get(hObject,'String'));
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function ampmin_CreateFcn(hObject, eventdata, handles)
% hObject handle to ampmin (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
handles.ampmin = 1.0;
disp(handles.ampmin)
set(hObject, 'String', num2str(handles.ampmin))
% Update handles structure
guidata(hObject, handles);
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!