I made a Matlab GUI through guide, and almost everything is working but I think something is wrong with my callbacks. Any help would be greatly appreciated.
1 view (last 30 days)
Show older comments
Its a simple gui that selects an excel file and you are able to plot different variables of data vs each other. I am able to select the excel file and the dropdown boxes become populated with variables, but when i click which variable i want, it doesn't plot it and in the command window this pops up: ans = @(hObject,eventdata)MainGUI('updateAxes',hObject,eventdata,guidata(hObject))
this is my code, i believe its something with the callback.

% --- Executes on button press in pushbuttonLoadXLS.
function pushbuttonLoadXLS_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonLoadXLS (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.fileName = uigetfile('*.xls');
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))')
set(handles.radiobuttonRaw,'callback','@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
set(handles.radiobuttonNormalized,'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,xColNum,yColNum)
a = xlsread(fileName);
x = a(:,xColNum); % make time be x axis for data
y = a(:,yColNum); % put data in y axis
function updateAxes(hObject,eventdata,handles)
xColNum = get(handles.popupmenuX,'value');
yColNum = get(handles.popupmenuY,'value');
fileName = handles.fileName;
[x,y] = readExcelColumns(fileName,xColNum,yColNum);
flagWantsNormalized = ...
get(handles.radiobuttonNormalized,'value');
if flagWantsNormalized
y = (y - min(y)) / range(y);
end
plot(handles.axes1,x,y)
2 Comments
Jan
on 21 Aug 2017
The code is not readable, if you do not format it using the "{} Code" button. The description "but when i click which variable i want, it doesn't plot it" is not clear enough. Please post more details.
Accepted Answer
Jan
on 21 Aug 2017
Edited: Jan
on 21 Aug 2017
It is a very ugly idea, to define a callback like this:
'@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))'
This is a string, which is interpreted in the base workspace and defines an anonymous function there. Why do you use such an obfuscated and indirect method to call a callback? Prefer:
set(handles.popupmenuX, 'callback', @updateAxes)
and get the current value of the handles struct there:
function updateAxes(hObject, EventData)
handles = guidata(hObject);
...
It looks strange, that the Excel file is read multiple times. Reading in once and storing the data in the handles struct will be more convenient. But I'm not sure, what happens in the code exactly, because it is hard to read without a proper formatting.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!