how to adjust plot with GUI slider without GUIDE

8 views (last 30 days)
I have a plot that is a sound wave and can be played. I am trying to make a slider that will change the values of w, y, b, and z which will change the linspace and then change the sound in real time. So far I have only made a slider that will adjust the W value. For some reason it will not work. I have no error message, but nothing is changing. What could be the possible issue? thanks
here is a sample of my code
figure
c1=uicontrol('style','pushbutton',...;
'position',[100 50 75 600],... ;%[ left right, up down, width, height]
'backgroundcolor','w',...;
'callback',@sound_g,...;
'string','G'),...
function sound_g(~,~)
Fs=6000;
Ts=1/Fs;
w=1000;
y=2000;
b=3000;
z=4000;
a = linspace(.5, 1, w);
d = linspace(1, .5, y );
s = linspace(0.5, 0.1, b );
r = linspace(.1, 0, z);
adsr = [a d s r];
t=[0:length(adsr)-1] ;
fc=392;
y=sin(2*pi*fc*t/Fs);
o=y.*(adsr);
sound(o,Fs)
plot(o)
SliderH = uicontrol('style','slider','position',[175 400 200 20],...
'min', -10000, 'max', 10000);
addlistener(SliderH, 'Value', 'PostSet', @slidercall);
function slidercall(source, eventdata)
end
end

Answers (1)

Rik
Rik on 8 Jun 2018
You don't use the actual value of the slider, so nothing will change.
function sound_g(hObject,~)
val=get(hObject,'Value');
And why do you create a slider inside the callback?
  3 Comments
Rik
Rik on 8 Jun 2018
I still see no indication of where you get the Value from the slider object.
Walter Roberson
Walter Roberson on 8 Jun 2018
Creating the slider inside the callback is needed for to be able to set its Callback property to a handle to the nested function slidercall . slidercall is deliberately nested because it shares variables with sound_g
Note though that you are creating a new slider in the same position every time you push the button.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!