There are 2pushbuttons in my GUI. Now, when i press pb1 sinewave signal is appearing in my graph. Similarly when i press pb2 the random signal is appearing. But pb2 signal is coming on the sinewave signal(pb1). How can i remove sinewave signal before pressing pb2 signal???
Asked
Active
Viewed 74 times
2 Answers
0
Let's suppose you have a GUI with a single axis (called Graph
) and two buttons: one for plotting a wave signal and one for plotting a random signal.
Since both buttons are plotting data on the same axis, you have to clear it before plotting new data, in order to avoid overlapping. This can be performed using the cla function.
Here is an exaple for both button handlers:
% Button handler for plotting a wave signal.
function pushbutton1_Callback(hObject, eventdata, handles)
fc = 60; % frequency
fs = 8000; % samples per second
dt = 1 / fs; % seconds per sample
st = 0.1; % seconds
t = (0:dt:st-dt)';
x = cos((2 * pi()* fc) .* t);
% Clear axis before plotting the new signal...
cla(handles.Graph);
plot(handles.Graph,t,x);
end
% Button handler for plotting a random signal.
function pushbutton2_Callback(hObject, eventdata, handles)
x = rand(100,1);
% Clear axis before plotting the new signal...
cla(handles.Graph);
plot(handles.Graph,x);
end

Tommaso Belluzzo
- 23,232
- 8
- 74
- 98
-
thank you very much. i have a another probel in GUI. the signal should move with axis. Here,i am calling the data from scope values(from simulink and it blocks data). i am mentioned my code in the above comment. can you please help me. sorry for inconvience. space is not sufficient here. – learningtakestime Feb 18 '18 at 16:18
-
Sorry but this is not clear to me. Can you be more specific please? I don't know how to reproduce the code in a test environment. – Tommaso Belluzzo Feb 18 '18 at 17:13
-
this is my code... evalin('base','sim(''testgui'')') timeData = evalin('base','ScopeData.time'); timeData = timeData'; %display(timeData); signalData = evalin('base','ScopeData.signals.values'); signalData = signalData'; %display(signalData); curve = animatedline; axis([0 1000 -1 1]); set(gca,'XLim',[0 1000],'YLim',[-1 1]); grid on; for i = 1:length(timeData) addpoints(curve, timeData(i), signalData(i)); drawnow end want to plot a signal with moving(continuous)with axis. but here i'm getting only moving signal with constant time period. – learningtakestime Feb 18 '18 at 17:19
-
That's quite clear. But if I copy-paste this into my Matlab (chances are) it will throw an error because of missing things. So you should describe what you are attempting to do instead... a signal moving with time? But how? In a window or adding points and enlarging the axis? – Tommaso Belluzzo Feb 18 '18 at 17:32
-
Anyway, I think a timer could handle this without any problem. Just remove the first element of your vector and append the new one to it. Modify the ticks accordingly. – Tommaso Belluzzo Feb 18 '18 at 17:39
-
i am using the scope data values which is from simulink scope – learningtakestime Feb 18 '18 at 17:40
-
Why don't you just extrapolate the necessary code from the exchange you linked? It looks like it works quite well. Alternatively, as I previously said, Create a timer in your GUI that pulls the data out of the model and updates the axis... – Tommaso Belluzzo Feb 18 '18 at 18:07
-
https://stackoverflow.com/questions/48853693/how-can-i-add-the-noise-signal-to-the-sine-wave-or-any-signal-on-the-same-graph.... – learningtakestime Feb 25 '18 at 16:16
-1
When your new push command is being entered you can first clear the axes. To do this you can use the cla function:
cla(handles.axes1)
plot(handles.axes1,x,y)

Oliver Smith
- 16
- 4
-
thanks, i am used just ' cla' its deletes the previous signal. can you tell how can move the signal with axis as mentioned above comment? i am getting moving signal with constant axis...it looks very clumsy in output. – learningtakestime Feb 18 '18 at 16:18