Problem with plotting/updating the graph in the GUI.
1 view (last 30 days)
Show older comments
Charan Kumar Shanmugaraj
on 10 Aug 2016
Commented: Robert
on 11 Aug 2016
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)
0 Comments
Accepted Answer
Robert
on 10 Aug 2016
Edited: Robert
on 10 Aug 2016
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};
4 Comments
Charan Kumar Shanmugaraj
on 11 Aug 2016
Edited: Charan Kumar Shanmugaraj
on 11 Aug 2016
Robert
on 11 Aug 2016
The callback you copied in your comment above calls the function mainGUI. Do you have that function? In large GUIs, the number of callback functions can get pretty large, so it is sometimes cleaner to have a single callback function (like mainGUI) that passes the arguments to its subfunctions, probably with something like feval or a switch statement. I am guessing that you made the GUI in GUIDE; did GUIDE create a function mainGUI for you? If so, you can look there to better understand how the example wanted you to add updateAxes to the code.
As far as animating your plot, this is definitely possible. To do it yourself, you would need a callback for your play/pause button to update the button (play<->pause) and slider (to show the right time) and animate the plot and a callback for the slider to change the time of the plot.
If your plot can be created quickly, I recommend you create a function that takes in the current time and draws the appropriate plot. Then to play the animation, call that function from a timer object. And make that same function part of the callback to the slider as well.
Bonus tip: you can have the slider update while you slide it with a listener:
addlistener(handles.mySlider,'Value','PreSet',@myCallback)
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!