41 views (last 30 days)
Show older comments
TheSaint on 9 Feb 2024
Commented: Stephen23 on 18 May 2024
Accepted Answer: Walter Roberson
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Stephen23 on 9 Feb 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#comment_3059131
"How do I call a function from the command window"
Very easily by making the file a function and not a script:
https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
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.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Walter Roberson on 9 Feb 2024
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
Sanjna on 18 May 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#comment_3164676
When i put this code in command window, am getting errors.
Stephen23 on 18 May 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#comment_3164706
"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.
Sign in to comment.
More Answers (1)
Steven Lord on 9 Feb 2024
Open in MATLAB Online
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:
- 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.
- 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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABData Import and AnalysisData Import and ExportWorkspace Variables and MAT-Files
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office