How do I call a function from the command window (2024)

41 views (last 30 days)

Show older comments

TheSaint on 9 Feb 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window

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

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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#answer_1405381

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#answer_1405381

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

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

  • Link

    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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#answer_1405401

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2080121-how-do-i-call-a-function-from-the-command-window#answer_1405401

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:

  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

0 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

Tags

  • function
  • duplicate post

Products

  • MATLAB

Release

R2023b

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.


How do I call a function from the command window (7)

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

How do I call a function from the command window (2024)
Top Articles
Timeroom: January 2023
Timeroom: Summer 2023
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Joanna Gaines Reveals Who Bought the 'Fixer Upper' Lake House and Her Favorite Features of the Milestone Project
Tri-State Dog Racing Results
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Lincoln Financial Field Section 110
Free Stuff Craigslist Roanoke Va
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5639

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.