MATLAB Answers

how to delete the previous plot obtained using slider in GUIDE

2 views (last 30 days)
Pankaj Jha
Pankaj Jha on 15 Apr 2019
Edited: Adam Danz on 17 Apr 2019
HI
I have a GUI with one axes and a slider.
I wish to do the following:
  1. Plot a sinewave of a given frequency on the axes.
  2. Plot the same sinewave using different sampling frequencies using the slider on the same axes.
I am able to do the first step and second step separtely.
But the problem is in the second step. I want that the first plot remians intact while plotting the new sine waves using slider. But I want the slider to delete the pervious plot before ploting a new sine wave. The plot of first should be intact.
How to do this ??

  2 Comments

Adam Danz
Adam Danz on 15 Apr 2019
I'm having a hard time following this. While you're drawing line number 'n', you want to keep line n-1 but deleted line n-2. Is that right?
Pankaj Jha
Pankaj Jha on 16 Apr 2019
Sorry .. if I couldnt put it correctly.
E.g. I want to plot a sine wave of 60 Hz (sampled at a very high freqiency). call this plot 0. ... it should always be visible on the axes
Now I have a slider... defining the value of the sampling frequency 'Fs'.
let the first 'Value' of the slider is Fs1. It should superimpose a sinewave of 60 Hz sampled at Fs1 on plot 0. Call this plot 1. So the axes sholud contain plot 0 and plot 1
Then let the second 'Value' of the slider is Fs2. It should superimpose a sinewave of 60 Hz sampled at Fs2 on plot 0. Call this plot 2. So the axes sholud contain plot 0 and plot 2
Then let the third 'Value' of the slider is Fs3. It should superimpose a sinewave of 60 Hz sampled at Fs3 on plot 0. Call this plot 3. So the axes sholud contain plot 0 and plot 3
And so on....

Sign in to comment.

Answers (2)

Rik
Rik on 15 Apr 2019
The easiest way to accomplish this is to create the second plot first, using the same positions as the first plot (which will then visually overlap the 'second' plot). Then you can edit the YData of the second plot in your callback.

  2 Comments

Pankaj Jha
Pankaj Jha on 16 Apr 2019
Sorry .. if I couldnt put it correctly.
E.g. I want to plot a sine wave of 60 Hz (sampled at a very high freqiency). call this plot 0. ... it should always be visible on the axes
Now I have a slider... defining the value of the sampling frequency 'Fs'.
let the first 'Value' of the slider is Fs1. It should superimpose a sinewave of 60 Hz sampled at Fs1 on plot 0. Call this plot 1. So the axes sholud contain plot 0 and plot 1
Then let the second 'Value' of the slider is Fs2. It should superimpose a sinewave of 60 Hz sampled at Fs2 on plot 0. Call this plot 2. So the axes sholud contain plot 0 and plot 2
Then let the third 'Value' of the slider is Fs3. It should superimpose a sinewave of 60 Hz sampled at Fs3 on plot 0. Call this plot 3. So the axes sholud contain plot 0 and plot 3
And so on....
Stephen Cobeldick
Stephen Cobeldick on 16 Apr 2019
@Pankaj Jha: sure, and probably the best solution is to create two plots and then write a callback function to update the YData of the second plot (i,e. line), exactly as Rik already stated. Using explicit graphics handles will make this much simpler and more efficient.

Sign in to comment.


Adam Danz
Adam Danz on 16 Apr 2019
Edited: Adam Danz on 16 Apr 2019
I understood your description as the following: You'll have a reference line that remains constant on the axes. Then you'll have an additional line that changes according to the slider value. Here's an implementation of Rik's solution.
When your GUI initializes, the reference line should be plotted along with the line that corresponds to the initial slider value parameters (use "hold on" to plot both lines). Store the handle to the second line in your GUI using guidata().
plot(x,y); %reference line
hold on
data.h = plot(x1,y1); %initial slider values
guidata(handles.GUI, data) %store handle in GUI (where handles.GUI is your GUI or any other object in GUI)
Then in your slider callback function, you just need to retrieve the handle to your slider-line and update the coordinates.
function slider_Callback(hObject, event, handles)
data = guidata(handles.GUI);
data.h.XData = newXData; %your new x coordinates (if needed)
data.h.YData = newYData; %your new y coodinates
end

  2 Comments

Rik
Rik on 16 Apr 2019
Then I must have not been clear enough: I intended to propose to plot two lines and editing the YData property of the second line to conform to the frequency selected by the slider.
Adam Danz
Adam Danz on 16 Apr 2019
Ah, my mistake then. "...visually overlap the second plot" led me to believe you were refering to overlaying two axes. I'll edit my response.

Sign in to comment.

Tags


Translated by