GUI PIANO SIN /SAWTOOTH/SQUARE

1 view (last 30 days)
W LA
W LA on 29 Jan 2019
Commented: W LA on 30 Jan 2019
Hi, I'm just starting at coding with a GUI.
I have writen a code for a piano in Matlab and it's not working like I would.
My question is how to code my pop-up button in order to change my associated function (sin/sawtooth...) for each key.
here is what I've done so far
% --- Executes on button press in do1.
function do1_Callback(hObject, eventdata, handles)
note=261.63;
signal(note,a)
--------------------------------------
function y = signal(note,a)
Fe=44100;
t=0:1/Fe:1;
if a==1
y=sin(2*pi*note*t);
elseif a==2
y=square(2*pi*note*t);
elseif a==3
y=sawtooth(2*pi*note*t);
end
sound(y,Fe);
----------------------------------------
function wave_Callback(hObject, eventdata, handles)
switch get(handles.signal,'value')
case 1
%definie a = 1
case 2
%define a = 2
case 3
%define a = 3
end

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2019
Remove your wave_Callback code; you do not need to do anything there.
function do1_Callback(hObject, eventdata, handles)
note=261.63;
a = get(handles.wave, 'Value');
signal(note,a)
  1 Comment
W LA
W LA on 30 Jan 2019
Edited: W LA on 30 Jan 2019
Well i'm not sure to understand how 'a' is taking the right value just by switching my function in my pop-up button but it appears to be working.
Thanks!

Sign in to comment.

More Answers (1)

Kevin Phung
Kevin Phung on 29 Jan 2019
Edited: Kevin Phung on 29 Jan 2019
perhaps you can create a listbox on your main script and assign it a tag 'lb1' so that you can point to it elsewhere
lb = uicontrol('style','listbox','String',{'SIN','SAW','SQ'},...
'Tag','lb1');
each time one of those keys are pressed, your keypress callback would retrieve the currently selected value from your list box.
lb =findobj(gcf,'Tag','lb1');
mode = lb.String{lb.Value} %this will return SIN, SAW, or SQ
switch mode
case 'SIN'
%blah blah
case 'SAW'
%blah
case 'SQ'
%blah
end
let me know if this helps.
a thought: I think creating 3 radiobuttons might more more aesthetically pleasing, just a tad bit more code
  1 Comment
W LA
W LA on 30 Jan 2019
thank you for the time you have taken to respond, i did not try your idea but it may be working as well thanks !

Sign in to comment.

Categories

Find more on Just for fun 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!