Calling a function from a script

I'm trying to call a function from a script, but everything that I have seen and read on how to do so has not been helpful. Would someone please show me an example of calling a function from a script? I would really appreciate it.

2 Kommentare

James Tursa
James Tursa am 17 Jan. 2020
Bearbeitet: James Tursa am 17 Jan. 2020
You call the function just like you would call it from the command line or from another function. There is no difference in the syntax. Are you trying to have the function code in the same file as the script code? Or ...?
Stephen23
Stephen23 am 18 Jan. 2020
Kyle Donk's "Answer" moved here:
Yes, it's in the same file, just a different script.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 18 Jan. 2020

1 Stimme

Example of calling a function from a script:
x = input('Input x ');
y = xsquared(x);
disp(y)
function y = xsquared(x)
y = x.*x;
end

2 Kommentare

James Tursa
James Tursa am 18 Jan. 2020
Caveat: Only for later versions of MATLAB. Earlier versions would not allow this.
Walter Roberson
Walter Roberson am 18 Jan. 2020
Earlier versions would require that the function be stored in a separate file.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 17 Jan. 2020

Kommentiert:

am 18 Jan. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by