GUI PIANO SIN /SAWTOOTH/SQUARE
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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)
More Answers (1)
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!