Calling a function from a script

조회 수: 214 (최근 30일)
Kyle Donk
Kyle Donk 2020년 1월 17일
댓글: Stephen23 2020년 1월 18일
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
James Tursa
James Tursa 2020년 1월 17일
편집: James Tursa 2020년 1월 17일
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 2020년 1월 18일
Kyle Donk's "Answer" moved here:
Yes, it's in the same file, just a different script.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 1월 18일
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
James Tursa
James Tursa 2020년 1월 18일
Caveat: Only for later versions of MATLAB. Earlier versions would not allow this.
Walter Roberson
Walter Roberson 2020년 1월 18일
Earlier versions would require that the function be stored in a separate file.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by