Help calling my basic function

1 visualización (últimos 30 días)
Michael Vaughan
Michael Vaughan el 24 de Sept. de 2020
Respondida: Steven Lord el 24 de Sept. de 2020
So I wrote the following in a script coding window:
function [n] = n(x)
syms q
[n]=(q^(x/2)-q^(-x/2))/(q^(1/2)-q^(-1/2));
end
however I named the file QuantumInteger.m
When i type n(7) into the command window it says that the function is not defined.. but when i type QuantumInteger(7) it does work.
I would prefer the file name to be QuantumInteger.m but to be able to call the function just by typing n(5)
How do I do this? Thanks

Respuestas (2)

Steve Eddins
Steve Eddins el 24 de Sept. de 2020
You must use the filename as the function name when calling your function. MATLAB will not pay attention to the "n" inside the file.
If, in your calling code, you would like to use n(7) as "shorthand" for calling your QuantumInteger function, you can make n an anonymous function:
n = @(x) QuantumInteger(x);
n(7)
ans =
(1/q^(7/2) - q^(7/2))/(1/q^(1/2) - q^(1/2))

Steven Lord
Steven Lord el 24 de Sept. de 2020
That is the correct and documented behavior. See the first Note on this documentation page.
You could do this by either creating an n.m file that simply calls QuantumInteger:
function y = n(x)
y = QuantumInteger(x);
end
or by defining a function handle:
n = @QuantumInteger;

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by