GUI PIANO SIN /SAWTOOTH/SQUARE

1 回表示 (過去 30 日間)
W LA
W LA 2019 年 1 月 29 日
コメント済み: W LA 2019 年 1 月 30 日
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

採用された回答

Walter Roberson
Walter Roberson 2019 年 1 月 29 日
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 件のコメント
W LA
W LA 2019 年 1 月 30 日
編集済み: W LA 2019 年 1 月 30 日
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!

サインインしてコメントする。

その他の回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 1 月 29 日
編集済み: Kevin Phung 2019 年 1 月 29 日
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 件のコメント
W LA
W LA 2019 年 1 月 30 日
thank you for the time you have taken to respond, i did not try your idea but it may be working as well thanks !

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by