Problem with plotting/updating the graph in the GUI.

1 次查看(过去 30 天)
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)

采纳的回答

Robert
Robert 2016-8-10
编辑:Robert 2016-8-10
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 个评论
Charan Kumar Shanmugaraj
编辑:Charan Kumar Shanmugaraj 2016-8-11
Hello Robert,
thank you. It works fine now.
I used the code below
myCallback = @(hObject,eventdata) updateAxes(hObject,eventdata,handles);
But can you explain why doesn't it work with this code below which I copied directly from the Inspector Window - Callback ?
set(handles.popupmenuY,'callback',...
@(hObject,eventdata)mainGUI('updateAxes',hObject,eventdata,guidata(hObject)))
I have another question to ask.
Is it possible to simulate a graph in matlab GUI based on time? At the moment I can see a final plot in my Axes window in GUI. But i want to simulate this plotting, for example : with a play/pause button and a slider to toggle between different time stamps of the plot.
Thanks in advance!
Robert
Robert 2016-8-11
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by