I am preparing simple GUI, but at the beginning I have faced serious problem.
Mygui consists of 2 groups of buttons:
groupA
(buttona1, buttona2)
groupB
(buttonb1, buttonb2)
And 2 'edit text' fields:
density
volume
Idea is that chosing buttona2
from groupA
, turns off first "edit text" (make it gray), and clicking on buttona1
turns it on again.
At this moment everything works fine.
The problem is that groupB
should work the same way with the second "edit text", but it doesn't.
I have noticed that clicking in groupB
doesnt even couse calling function:
function groupB_SelectionChangeFcn(hObject, eventdata, handles)
Whole code:
function varargout = testgui(varargin)
% TESTGUI MATLAB code for testgui.fig
% TESTGUI, by itself, creates a new TESTGUI or raises the existing
% singleton*.
%
% H = TESTGUI returns the handle to a new TESTGUI or the handle to
% the existing singleton*.
%
% TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTGUI.M with the given input arguments.
%
% TESTGUI('Property','Value',...) creates a new TESTGUI or raises
% the existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before testgui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to testgui_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 testgui
% Last Modified by GUIDE v2.5 04-Dec-2014 16:56:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @testgui_OpeningFcn, ...
'gui_OutputFcn', @testgui_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 testgui is made visible.
function testgui_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 testgui (see VARARGIN)
% Choose default command line output for testgui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
initialize_gui(hObject, handles, false);
% UIWAIT makes testgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testgui_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 during object creation, after setting all properties.
function density_CreateFcn(hObject, eventdata, handles)
% hObject handle to density (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
function density_Callback(hObject, eventdata, handles)
% hObject handle to density (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 density as text
% str2double(get(hObject,'String')) returns contents of density as a double
density = str2double(get(hObject, 'String'));
if isnan(density)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
% Save the new density value
handles.metricdata.density = density;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function volume_CreateFcn(hObject, eventdata, handles)
% hObject handle to volume (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
function volume_Callback(hObject, eventdata, handles)
% hObject handle to volume (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 volume as text
% str2double(get(hObject,'String')) returns contents of volume as a double
volume = str2double(get(hObject, 'String'));
if isnan(volume)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
% Save the new volume value
handles.metricdata.volume = volume;
guidata(hObject,handles)
% --- Executes when selected object changed in groupA.
function groupA_SelectionChangeFcn(hObject, ~, handles)
% hObject handle to the selected object in groupA
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (hObject == handles.buttona1)
set(handles.density, 'Enable', 'on');
else
set(handles.density, 'Enable', 'off');
end
% --- Executes when selected object changed in groupB.
function groupB_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in groupB
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (hObject == handles.buttonb1)
set(handles.volume, 'Enable', 'on');
else
set(handles.volume, 'Enable', 'off');
end
% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the reset flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to reset the data.
if isfield(handles, 'metricdata') && ~isreset
return;
end
handles.metricdata.density = 0;
handles.metricdata.volume = 0;
set(handles.density, 'String', handles.metricdata.density);
set(handles.volume, 'String', handles.metricdata.volume);
set(handles.groupA, 'SelectedObject', handles.buttona1);
set(handles.groupB, 'SelectedObject', handles.buttonb1);
% Update handles structure
guidata(handles.figure1, handles);
.fig
file is included.