Home

How to Write Your First MATLAB Function

|
|  Updated:  
2016-03-26 08:21:58
|   From The Book:  
MATLAB For Dummies
Explore Book
Buy On Amazon

Creating a function in MATLAB is only slightly more work than creating a script. In fact, the two processes use the same editor, so you’re already familiar with what the editor can provide in the way of help. The various Editor features you’d use for creating a script all work the same way with functions, too. (You have access to the same double percent sign (%%) for use with sections.)

The following steps get you started creating your first function.

  1. Click the arrow under the New entry on the Home tab of the MATLAB menu and select Function from the list that appears.

    You see the Editor window. Notice that the editor already has a function header in place for you, along with the inputs, outputs, and documentation comments.

    image0.jpg

    This may look a little complex, but that’s because MATLAB includes a number of optional elements. A function has three requirements:

    • A function always begins with the word function.

    • You must include a function name.

    • A function must always end with the keyword end.

  2. Delete output_args.

    Functions aren’t required to have output arguments. In order to keep things simple for your first function, you’re not going to require any inputs or outputs

    An argument is simply a word for an individual data element. If you supply a number to a function, the number is considered an argument. Likewise, when you supply a string, the entire string is considered just one argument. A vector is considered a single argument. Any single scalar or object that you provide as input or that is output from the function is considered an argument.

  3. Delete input_args.

    Functions aren’t required to have input arguments.

  4. Change the function name from Untitled to SayHello.

    Your function should have a unique name that reflects its purpose. Avoiding existing function names is essential. Before you name your function, test the name you’re considering by typing help(‘NameOfYourFunction’') and pressing Enter. If the function already exists, you see a help screen. Otherwise, MATLAB denies all knowledge of the function, and you can use the function name you have chosen.

    Always provide help information with the functions you create. Otherwise, the help() function won’t display any help information and someone could think that your function doesn’t exist.

    If you want to be absolutely certain that there is no potential conflict between a function you want to create and an existing function (even a poorly designed one), use the exist() function instead, such as exist(‘SayHello’). When the function exists, you see an output value of 2. Otherwise, you see an output value of 0.

  5. Change the comments to read like this:

    %SayHello()
    % This function says Hello to everyone!

    Notice that the second line is indented. The indentation tells MATLAB that the first line is a title and the second is text that goes with the title. Formatting your comments becomes important when working with functions. Otherwise, you won’t see the proper help information when you request it.

  6. Add the following code after the comment:

    disp(‘Hello There!’);

    The function simply displays a message onscreen.

  7. Click Save.

    You see the Select File for Save As dialog box.

    image1.jpg
  8. Select a directory, type SayHello.m in the File Name field and then click Save.

    MATLAB saves your function as SayHello.m.

    The filename you use to store your function must match the name of the function. MATLAB uses the filename to access the function, not the function name that appears in the file. When there is a mismatch between the function name and the filename, MATLAB displays an error message.

About This Article

This article is from the book: 

About the book author:

Jim Sizemore is Professor of Physics and Engineering at Tyler Junior College. For over 25 years he s worked in the semiconductor and software industries as a process engineer, device physicist, and software developer and has been teaching college physics, engineering, and math for the last 13 years.

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.