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

20 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Stephen23
Stephen23 el 15 de Abr. de 2018
Editada: Stephen23 el 15 de Abr. de 2018
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:

Más respuestas (1)

Walter Roberson
Walter Roberson el 15 de Abr. de 2018
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.

Categorías

Más información sobre Audio Processing Algorithm Design en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by