''Too many input arguments'', trouble using callback to call my function?

17 ビュー (過去 30 日間)
bella bridges
bella bridges 2018 年 4 月 15 日
I'm making a GUI piano so that when a key is pushed it plays a note. I'm not very good with user defined functions and can't get mine to work.
This is my code for my GUI
pfig=figure('position',[500 150 1000 500 ] )
pbutton2=uicontrol('style','pushbutton',...
'position',[175 20 75 400],... %separate keys by 75
'callback',@sound_a)
and this is my function
function sounda
Fs=8000; %sample rate ranging from 1000 to 384000 in Hz.default is 8192 higher number=higher quality audio.
Ts=1/Fs; %average number of samples obtained per second is 1/t
t=[0:Ts:.3];% durration of play time. 0- how many seconds in increments of ts. shortening time streches out wave resulting in lower sound. .5 = octave down. *2 = octave up
fc=261; %Frequency of middle c is 261 aprx
c=sin(2*pi*fc*t); %the graph of a note. f_a is the hurtz and t is the durration. simple sine wave similar to a tuning fork
sound(c,Fs);
end
the function file name is sound_a. What is wrong with the function?
thanks

採用された回答

Stephen23
Stephen23 2018 年 4 月 15 日
編集済み: Stephen23 2018 年 4 月 15 日
Graphics callback functions require atleast two input arguments: these are automatically supplied by MATLAB, and give information about the event that caused that callback to be triggered. So you need to define your function to have two input arguments (which can be ignored if you want):
function sound_a(~,~)
This is explained in the MATLAB documentation:

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 4 月 15 日
Callbacks must always accept at least two parameters. MATLAB automatically supplies the handle of the object that it is the callback for as the first parameter, and automatically passes information about the details of the callback as the second parameter.
Therefore you callback must be prepared to accept a minimum of two parameters. It is not required to use the parameters, but it must accept them.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by