Info

This question is closed. Reopen it to edit or answer.

How can I give a predefined function as a parameter in a new function?

1 view (last 30 days)
I want to calculate if any arbitrary signal is an energy signal or a power signal. Is there a way in which I can give the function as a parameter for the energy/power finding function so that I can find the energy/power of any signal without changing the code?

Answers (1)

Star Strider
Star Strider on 8 Sep 2014
You can pass function handles as arguments.
Example:
function h = wave(x)
h = sin(2*pi*sin(x));
end
function y = rms(fun,x)
y = sqrt(mean(fun(x).^2));
end
x = linspace(0,2*pi);
y = rms(@wave,x);
fprintf(1, '\n\tPower = %f\n', y)
This code produces:
Power = 0.645782

Tags

Community Treasure Hunt

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

Start Hunting!