Search code examples
matlabparameter-passingsetdefault

Matlab: set default values for GUI edit text and use them in push button callback


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 (for example: 27.0098876953125)
  • 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

Following oro777 comment I've added the rest of the code for better analysis. I've also tried with a more recent (R2015b) version of MATLAB and the result is the same, with the difference that now the disp inside the button callback function shows the entire handle structure instead of just the id:

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

I've also noticed the following: - If I start the .fig file everything works fine - If I push the run button on the .m file, the strange behavior described above occurs

Here is the code:

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);

Solution

  • Thanks @Hoki, that was the problem. Since I didn't see any direct reference to handles.ampmin I didn't think it would be used to store the handle, but looking at the actual .fig exported code, it became apparent that indeed that's were the uicontrol handle is stored.