MATLAB: Problem with plotting/updating the graph in the GUI.

excel in matlab guimatlab gui

I have an excel file with 3 columns 'time', 'X-axis' and 'y-axis' and I want to read this Excel sheet via GUI in matlab and plot a graph in GUI. I used two popupmenus (popupmenuX and popupmenuY). I want to display the column names in these popupmenus which I was fine with. But the plot was not generated after I selected the column names in the corresponding axis.
This is my code:
handles.filename = uigetfile('*.xlsm')
guidata(hObject, handles)
setPopupmenuString(handles.popupmenuX, eventdata, handles)
setPopupmenuString(handles.popupmenuY, eventdata, handles)
set(handles.popupmenuX,'callback','@(hObject,eventdata)mainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
set(handles.popupmenuY,'callback','@(hObject,eventdata)mainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
function setPopupmenuString(hObject, eventdata, handles)
filename = handles.filename;
[numbers, colNames] = xlsread(filename);
set(hObject,'string',colNames);
function [x,y] = readExcelColumns(filename,xCol,yCol)
a = xlsread(filename);
x = a(:,xCol);
y = a(:,yCol);
function updateAxes(hObject, eventdata, handles)
xCol = get(handles.popupmenuX,'value');
yCol = get(handles.popupmenuY,'value');
filename = handles.filename;
[x,y] = readExcelColumns(filename, xCol, yCol);
plot(handles.axes1, x,y)

Best Answer

Your callback appears to be wrapped in single quotes. When the callback is a string, MATLAB evaluates the string. Your string evaluates to an anonymous function handle. In order to call that function rather than create it, remove the single quotes.
set(handles.popupmenuX,'callback',...
@(hObject,eventdata)mainGUI('updateAxes',hObject,eventdata,guidata(hObject)))
set(handles.popupmenuY,'callback',...
@(hObject,eventdata)mainGUI('updateAxes',hObject,eventdata,guidata(hObject)))
or better yet, avoid the repetition:
myCallback = @(hObject,eventdata)mainGUI('updateAxes',hObject,eventdata,guidata(hObject));
set(handles.popupmenuX,'Callback',myCallback)
set(handles.popupmenuY,'Callback',myCallback)
In either case, the way I read your code, you really want the function handle to call updateAxes, in which case you need something like
myCallback = @(hObject,eventdata) updateAxes(hObject,eventdata,handles);
or the equivalent in cell syntax
myCallback = {@updateAxes,handles};
Related Question