MATLAB Answers

Matlab GUI switch statement not working properly

3 views (last 30 days)
Muhammad Farhan  Mughal
Muhammad Farhan Mughal on 31 May 2016
Commented: Walter Roberson on 31 May 2016
I am writing MATLAB GUI code where i show the image and give them two options to select one of them. But in my code Switch statement (at the end block of the program)is not working properly and MATLAB give the error "Not enough input arguments". "Error while evaluating uicontrol Callback" mu MATLAB code is as follows
function modified_GUI_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 modified_GUI (see VARARGIN)
% Choose default command line output for modified_GUI
handles.output = hObject;
%%axes position
set(gcf, 'units','normalized','outerposition',[0 0 10 10]);
fig_hr = 326;
fig_vr = 493;
pos1 = round((scrz(3)-fig_hr)/4);
pos2 = round((scrz(4)-fig_vr)/1.5);
posa = pos1 +1.5* round(fig_hr);
random_arr = randperm(90);
handles.text1 =uicontrol('Style','Text', 'HandleVisibility','off','Position' ,[pos1, pos2,500 ,300],'Value',0,'String','One image with Two options will shown to you select your option by looking at the face.Enter user number to start ','FontSize',25,'BackgroundColor',[0.5,0.5,0.5]);
handles.edit1 = uicontrol('Style','Edit', 'HandleVisibility','off','Position' ,[4*pos1, pos2,100 ,30]);
handles.button1 = uicontrol('Style', 'pushbutton','Callback', {@pushbutton1_Callback, ancestor(hObject, 'figure')} ,'Units','pixels', 'Position', [4*pos1, pos2-60,100 ,30 ], 'String', 'Enter');
handles.button2 = uicontrol('Style', 'pushbutton','Callback', {@pushbutton2_Callback, ancestor(hObject, 'figure')} ,'Units','pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2+70, pos2- 140,70 ,50 ], 'String', 'Next');
handles.img = 1; = 1;
pos3 = [2.3*pos1 pos2 fig_hr fig_vr];
varargout{1} = handles.output;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes modified_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = modified_GUI_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;
function pushbutton1_Callback (hObject,eventdata,GUIfig)
handles = guidata(GUIfig);
random_arr= getappdata(0,'random_arr'); % obtain the random sequence
pos1= getappdata(0,'pos1');
pos2= getappdata(0,'pos2');
posa= getappdata(0,'posa');
vec_A = {'Attractive','Sociable','Healthy','EasyGoing'};
vec_B = {'UnAttractive','Ausitic','UnHealthy','Fussy'};
hBtnGrp = uibuttongroup('Position',[ 0 0 0.1 0.1], 'Units','Normalized','BackgroundColor',[0.5,0.5,0.5]);
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','BackgroundColor',[0.5,0.5,0.5], 'Position',[(pos1+326+pos1)/2+150, pos2-70,350 ,50],'Value',0, 'String',vec_A(1),'FontSize',20,'FontWeight','bold', 'Tag','A')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','BackgroundColor',[0.5,0.5,0.5], 'Position',[(posa+326+posa)/2+150, pos2-70,350 ,50],'Value',0, 'String',vec_B(1),'FontSize',20,'FontWeight','bold', 'Tag','B')
guidata(GUIfig, handles)
function pushbutton2_Callback (hObject,eventdata,GUIfig, hBtnGrp)
handles = guidata(GUIfig);
%%selected option
global data;
switch get(get(hBtnGrp,'SelectedObject'),'Tag');
case 'A', data = 1;
case 'B', data = 2;
img_select_1 = 0;
img_select_2 = 0;
if ( data == 1)
img_select_1 = 1;
if ( data == 2)
img_select_2 = 1;
% = +1;
random_arr= getappdata(0,'random_arr');
guidata(GUIfig, handles)


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 31 May 2016
Where is the hBtnGrp parameter coming from in button 2 callback? You are only passing on one extra parameter when you configured
handles.button2 = uicontrol('Style', 'pushbutton','Callback', {@pushbutton2_Callback, ancestor(hObject, 'figure')} ,'Units','pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2+70, pos2- 140,70 ,50 ], 'String', 'Next');
The ancestor(hObject, 'figure') becomes the (only) extra parameter.


Walter Roberson
Walter Roberson on 31 May 2016
You assign to a variable in the workspace of pushbutton1_Callback but you do not change the Callback property of handles.button2 so the value is not going to be passed to pushbutton2_Callback . Remember that just naming a variable as a parameter in a function definition does not mean that the calling environment is going to be searched for a variable of the same name. (Besides, pushbutton1_Callback is not even in the calling environment of pushbutton2_Callback)
If you were to assign hBtnGrp to a field in handles then you could pull it out of handles in pushbutton2_Callback

Sign in to comment.