I am trying to run a GUI that allows the user to choose from a popup menu. The code then gets their choice and goes through some if and else if statements.

I keep getting the following errors:
Undefined variable contents.
Error in Project1>popupmenu1_Callback (line 159)
userChoice = contents{get(hObject, 'Value')}
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Project1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)Project1('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Here is my code:
function varargout = Project1(varargin)
% PROJECT1 MATLAB code for Project1.fig
% PROJECT1, by itself, creates a new PROJECT1 or raises the existing
% singleton*.
%
% H = PROJECT1 returns the handle to a new PROJECT1 or the handle to
% the existing singleton*.
%
% PROJECT1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PROJECT1.M with the given input arguments.
%
% PROJECT1('Property','Value',...) creates a new PROJECT1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Project1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Project1_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 Project1
% Last Modified by GUIDE v2.5 03-Apr-2015 11:56:56
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Project1_OpeningFcn, ...
'gui_OutputFcn', @Project1_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 Project1 is made visible.
function Project1_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 Project1 (see VARARGIN)
% Choose default command line output for Project1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Project1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Project1_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 edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
userChoice = contents{get(hObject, 'Value')};
if (userChoice == 1)
print(1)
elseif(userChoice == 2)
print(2)
else
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

Answers (1)

James - you have left out the line of code in the popupmenu1 callback that initializes the local variable contents. Your callback should be
function popupmenu1_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
userChoice = contents{get(hObject, 'Value')};
Note that userChoice will be a string representing the selection from the popup menu. If you are just looking to use the index of that selection, then your code becomes
function popupmenu1_Callback(hObject, eventdata, handles)
userChoiceIdx = get(hObject, 'Value');
if (userChoiceIdx == 1)
print(1)
elseif(userChoiceIdx == 2)
print(2)
else
% do something
end
Try the above and see what happens!

2 Comments

I changed it so that userChoiceIdx represents the index, but I am still getting the following errors:
Undefined variable contents.
Error in Project1>popupmenu1_Callback (line 159)
userChoice = contents{get(hObject, 'Value')}
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Project1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)Project1('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
James - this is the same error message as before and your code (given the error message) still uses userChoice and not the userChoiceIdx. The error message is telling you that you are trying to use a variable that is undefined...and that variable is named contents. The first part of my answer indicates that (if you are going to use this variable) you need to define it as
contents = cellstr(get(hObject,'String'));
Either use the above line of code, or remove the unneeded reference to contents.

Sign in to comment.

Categories

Asked:

on 3 Apr 2015

Commented:

on 5 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!