How to Use Your New MATLAB Function - dummies

How to Use Your New MATLAB Function

By Jim Sizemore, John Paul Mueller

You have a shiny new MATLAB function and you’re just itching to use it. Before you can use the function, you must make sure that the directory containing the function file is part of the MATLAB path. You can achieve this goal in two ways:

  • Double-click the directory entry in the Current Folder window.

  • Right-click the directory entry in the Current Folder window and choose Add to Path→Selected Folders and Subfolders from the context menu.

You can try your new function in a number of ways. The following lists contains the most common methods:

  • Click Run in the Editor window, and you see the output in the Command window. However, there is a little twist with functions. You can’t always click Run and get a successful outcome, even though the function will always run.

  • Click Run and Advance in the Editor window. (This option runs the selected section when you have sections defined in your file.)

  • Click Run and Time in the Editor window. (This option outputs profiling information — statistics about how the function performs — for the function.)

  • Type the function name in the Command window and press Enter.

Your function also has help available with it. Type help(‘SayHello’) and press Enter. MATLAB displays the following help information:

 This function says Hello to everyone!

The output is precisely the same as it appears in the function file. The doc() function also works. Type doc(‘SayHello’) and press Enter. Notice how the title is presented in a different color and font than the text that follows.