MATLAB: Function not recognizing uibuttongroup for switch statement in matlab gui

MATLABmatlab gui

function varargout = GUI_Personality_Impressions(varargin)
% GUI_PERSONALITY_IMPRESSIONS MATLAB code for GUI_Personality_Impressions.fig
% GUI_PERSONALITY_IMPRESSIONS, by itself, creates a new GUI_PERSONALITY_IMPRESSIONS or raises the existing
% singleton*.
%




% H = GUI_PERSONALITY_IMPRESSIONS returns the handle to a new GUI_PERSONALITY_IMPRESSIONS or the handle to
% the existing singleton*.
%
% GUI_PERSONALITY_IMPRESSIONS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_PERSONALITY_IMPRESSIONS.M with the given input arguments.
%
% GUI_PERSONALITY_IMPRESSIONS('Property','Value',...) creates a new GUI_PERSONALITY_IMPRESSIONS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_Personality_Impressions_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_Personality_Impressions_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_Personality_Impressions
% Last Modified by GUIDE v2.5 14-Mar-2016 15:45:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_Personality_Impressions_OpeningFcn, ...
'gui_OutputFcn', @GUI_Personality_Impressions_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_Personality_Impressions is made visible.
function GUI_Personality_Impressions_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_Personality_Impressions (see VARARGIN)
% Choose default command line output for GUI_Personality_Impressions
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_Personality_Impressions wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_Personality_Impressions_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)
set(handles.axes1,'units','pixels');
set(handles.axes2,'units','pixels');
scrz=get(0,'ScreenSize')
% pos2=[(scrz(3)-800)/2 (scrz(4)-600)/2 800 600];
fig_hr = 326;
fig_vr = 493;
pos1 = round((scrz(3)-fig_hr)/4)
pos2 = round((scrz(4)-fig_vr)/2)
% fig_xcoord = (ScreenSize(3) - fig_width)/2;
pos3 = [pos1 pos2 fig_hr fig_vr]
set(handles.axes1,'pos',[pos3]);
axes(handles.axes1);
imshow('Chinese_eyes+2.tif');
% pos1 = round((scrz(3)-fig_hr)/ 3)
posa = pos1 +1.5* round(fig_hr);
pos4 = [posa pos2 fig_hr fig_vr]
set(handles.axes2,'pos',[pos4]);
axes(handles.axes2);
imshow('Chinese_eyes+2.tif');
% myui
% % Get default command line output from handles structure
varargout{1} = handles.output;
hBtnGrp = uibuttongroup('Position',[ 0 0 0.1 0.1], 'Units','Normalized');
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[(pos1+326+pos1)/2, pos2-70,70 ,50],'Value',0, 'String','A', 'Tag','A')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','Position' ,[(posa+326+posa)/2, pos2-70,70 ,50],'Value',1, 'String','B', 'Tag','B')
uicontrol('Style', 'pushbutton','Callback', @pushbutton1,'Units', 'pixels','Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ],'String','Next');
function pushbutton1(src,ev)
global data
switch get(get(hBtnGrp,'SelectedObject'),'Tag')
case 'A', data = 1;
case 'B', data = 2;
end
It shows an error that "Undefined function or variable 'hBtnGrp'" … The variable exists how to solve this issue

Best Answer

You have to go through your code, and everywhere you have a "function" statement, you have to add an "end" statement after the last line of the function, so that the structure looks like
function this
...
end
function that
...
end
However, when you get to function pushbutton1, instead of placing the "end" for function GUI_Personality_Impressions_OutputFcn before the "function pushbutton1" line, add a second end after the "function pushbutton1" function. The structure would then look like
function this
...
end
function that
...
end
function GUI_Personality_Impressions_OutputFcn
...
function pushbutton1
...
end
end
This defines the function pushbutton1 inside of GUI_Personality_Impressions_OutputFcn . When you do that, the nested function, pushbutton1, is able to access any variables defined in GUI_Personality_Impressions_OutputFcn before the "function pusbutton1" statement.
Alternately, without needing to add those "end" at all, you could change your
uicontrol('Style', 'pushbutton','Callback', @pushbutton1,'Units', 'pixels','Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ],'String','Next');
to be
uicontrol('Style', 'pushbutton','Callback', {@pushbutton1, hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ], 'String', 'Next');
and change the
function pushbutton1(src,ev)
to
function pushbutton1(src, ev, hBtnGrp)
then, each time the callback is invoked, the value of hBtnGrp that was set will be passed as a third parameter to pushbutton1 where it can be accessed from the argument list.
Related Question