필터 지우기
필터 지우기

How do I call a function from the command window

조회 수: 49 (최근 30일)
TheSaint
TheSaint 2024년 2월 9일
댓글: Stephen23 2024년 5월 18일
myEquation(2)
function myEquation (x)
a = 0.4361836;
b = 0.1201676;
c = 0.937298;
r = exp(-0.5*(x^2))/(2*pi) ;
t = 1/(1+(0.3326*x)) ;
phi = 0.5 - r*((a*t)-(b*(t^2))+(c*(t^3))) ;
fprintf('The value of Φ(x) is: %i', phi)
fprintf('\n')
end
I have this code, and it works properly, however, I need a way to be able to call it from the command window. The line myEquation(2) auto inputs the value as 2, but I need to be able to enter other values without editing the code. Should I use an input prompt to prompt the user for a value of x to run my equation on?
  댓글 수: 1
Stephen23
Stephen23 2024년 2월 9일
"How do I call a function from the command window"
Very easily by making the file a function and not a script:
I.e. get rid of the line myEquation(2)
"Should I use an input prompt to prompt the user for a value of x to run my equation on?"
Ugh, no. Beginners love using INPUT prompts for eveything,and then have to unlearn that when they realize how much of an impedance INPUT is to writing expandable, testable, efficient code. Best avoided. Just write a function.

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

채택된 답변

Walter Roberson
Walter Roberson 2024년 2월 9일
xval = input('enter x: ');
myEquation(xval)
function myEquation (x)
a = 0.4361836;
b = 0.1201676;
c = 0.937298;
r = exp(-0.5*(x^2))/(2*pi) ;
t = 1/(1+(0.3326*x)) ;
phi = 0.5 - r*((a*t)-(b*(t^2))+(c*(t^3))) ;
fprintf('The value of Φ(x) is: %i', phi)
fprintf('\n')
end
  댓글 수: 2
Sanjna
Sanjna 2024년 5월 18일
When i put this code in command window, am getting errors.
Stephen23
Stephen23 2024년 5월 18일
"When i put this code in command window, am getting errors."
Solution: do not put this code in the command window.
Save the function in a file all by itself. Call the function.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 2월 9일
What you have right now is a script file with a function defined inside it. Functions inside script files are not directly accessible outside the script file.
Two potential options:
  1. Erase that first line in the file, to make the new first line the one with the function keyword. This makes the file a function file rather than a script file, and the first (main) function in a function file is directly accessible outside its file.
  2. Add a line that defines a function handle to the function and stores that function handle in a variable. If that function handle is created inside the script file, you will be able to call the function using the function handle. For example, I created a script file, ran it, and called the function via the function handle.
>> dbtype script2080121.m
1 fh = @fun2080121;
2 disp('Hello world!')
3
4 function z = fun2080121(x, y)
5 z = x+y;
6 end
>> script2080121
Hello world!
>> fh(3, 5)
ans =
8

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by