Listener ignores some part of the code and creates unwanted figure
Show older comments
Hello,
I have a GUI that takes values from a slider and makes plots using them. I'm using a listener to get all the plots for all the values along the way when the slider is dragged. It is working, however, I have a little problem.
function slider1_Callback(hObject, eventdata, handles)
%unnecessary code here
D_son = evalin('base','D_son');
axes(handles.axes1);
plot(handles.axes1, D_son);
xlim([1,360]) %LINE ONE
set(gca,'XTick',[1 45 90 135 180 225 270 315 360])%LINE TWO
addlistener(handles.slider1,'ContinuousValueChange',@(hObject, event) slider1_Callback(hObject, eventdata, handles));
My problem is, when I drag the slider;
1. It places the graph on the correct axes, however, it brings up a blank Figure page.
2. It completely ignores LINE ONE and LINE TWO I marked up there.
Other than these two, it works just fine. How can I fix these problems? Thank you in advance.
Regards
Accepted Answer
More Answers (1)
dpb
on 3 Aug 2014
I don't do GUIs so anything specific to that is out of scope, but...
axes(handles.axes1);
plot(handles.axes1, D_son);
xlim([1,360]) %LINE ONE
set(gca,'XTick',[1 45 90 135 180 225 270 315 360])%LINE TWO
Why do you call axes, specifically, when you use the same handle in plot? Altho it shouldn't really matter seems like overkill to do both.
axes(handles.axes1);
plot(D_son);
xlim([1,360])
set(gca,'XTick',[1 45:45:360])
Mayhaps you need a drawnow to refresh the plot?
I'm not sure what is meant by "a blank Figure page"?
4 Comments
As said, "I don't do windows" :) so can't really help too much on the UI stuff but it appears your location for actually creating the figure isn't in the right spot so it's getting called multiple times. Having that somewhere in an initialization or a singly-called callback or inside a logic branch that checks whether the figure/axes already exists and doesn't make another one if it does is the solution.
deniz
on 3 Aug 2014
I don't think it's a "code on a wrong line" thing; I think it's having code that creates a figure/plot/axes (the latter two which create the first automatically) in the wrong callback or at least in a place in a callback that should test for whether the desired figure already exists or not.
It's an overall application architecture question of "who does what and when?" that needs solving is what I'm getting at.
ADDENDUM
And, to specifically point it out, it's NOT the code in this callback that's the problem -- the figure/axes have already been created when you get here or the handle to the axes you reference here would not be valid and you'd error.
Categories
Find more on 2-D and 3-D Plots 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!