Skip to Main Content Skip to Search
Product Documentation

Program Development

Overview

As you write a MATLAB function or script, you save it to a file that has a .m file extension. There are two types of these files you can write: scripts and functions. This section covers basic program development, describes how to write and call scripts and functions, and shows how to pass different types of data when calling a function.

For more ideas on good programming style, see Program Development in the MATLAB Programming Tips documentation. The Programming Tips section is a compilation of useful pieces of information that can show you alternate and often more efficient ways to accomplish common programming tasks while also expanding your knowledge of MATLAB.

Creating a Program

You can type in your program code using any text editor. This section focuses on using the MATLAB Editor/Debugger for this purpose.

The first step in creating a program is to open an editing window. To create a file for a new function, type the word edit at the MATLAB command prompt. To edit an existing file, type edit followed by the file name:

edit drawPlot.m

MATLAB opens a new window for entering your program code. As you type in your program, MATLAB keeps track of the line numbers in the left column.

For more information on the Editor/Debugger, see Create, Open, Save, and Close Files.

Saving the Program

It is usually a good idea to save your program periodically while you are in the development process. To do this, click File > Save in the Editor/Debugger. Enter a file name with a .m extension in the Save file as dialog box that appears and click OK. It is customary and less confusing if you give the file the same name as the first function in the file.

Running the Program

Before trying to run your program, make sure that its file is on the MATLAB path. The MATLAB path defines those folders that you want MATLAB to know about when executing files. The path includes all the folders that contain functions provided with MATLAB. It should also include any folders that you use for your own functions.

Use the which function to see if your program is on the path:

which drawPlot
   D:\user5\matlab\mywork\drawPlot.m

If not, add its folder to the path using the addpath function:

addpath('D:\user5\matlab\mywork')

Now you can run the program just by typing the name of the file at the MATLAB command prompt:

drawPlot(xdata, ydata)

Getting the Bugs Out

In all but the simplest programs, you are likely to encounter some type of unexpected behavior when you run the program for the first time. Program defects can show up in the form of warning or error messages displayed in the command window, programs that hang (never terminate), inaccurate results, or some number of other symptoms. This is where the second functionality of the MATLAB Editor/Debugger becomes useful.

The MATLAB Debugger enables you to examine the inner workings of your program while you run it. You can stop the execution of your program at any point and then continue from that point, stepping through the code line by line and examining the results of each operation performed. You have the choice of operating the debugger from the Editor window that displays your program, from the MATLAB command line, or both.

The Debugging Process

You can step through the program right from the start if you want. For longer programs, you will probably save time by stopping the program somewhere in the middle and stepping through from there. You can do this by approximating where the program code breaks and setting a stopping point (or breakpoint) at that line. Once a breakpoint has been set, start your program from the MATLAB command prompt. MATLAB opens an Editor/Debugger window (if it is not already open) showing a green arrow pointing to the next line to execute.

From this point, you can examine any values passed into the program, or the results of each operation performed. You can step through the program line by line to see which path is taken and why. You can step into any functions that your program calls, or choose to step over them and just see the end results. You can also modify the values assigned to a variable and see how that affects the outcome.

To learn about using the MATLAB Debugger, see Debugging Process and Features. Type help debug for a listing of all MATLAB debug functions.

For programming tips on how to debug, see Debugging in the Programming Tips documentation.

Cleaning Up the Program

Even after your program is bug-free, there are still some steps you can take to improve its performance and readability. The MATLAB Code Analyzer utility generates a report that can highlight potential problems in your code. For example, you might be using the element-wise AND operator (&) where the short-circuit AND (&&) is more appropriate. You might be using the find function in a context where logical subscripting would be faster.

MATLAB offers the Code Analyzer and several other reporting utilities to help you make the finishing touches to your program code. These tools are described in Using MATLAB Reports.

Improving Performance

The MATLAB Profiler generates a report that shows how your program spends its processing time. For details about using the MATLAB Profiler, see Profiling for Improving Performance. For tips on other ways to improve the performance of your programs, see Techniques for Improving Performance.

Three types of reports are available:

Summary Report

The summary report provides performance information on your main program and on every function it calls. This includes how many times each function is called, the total time spent in that function, along with a bar graph showing the relative time spent by each function.

Detail Report

When you click a function name in the summary report, MATLAB displays a detailed report on that function. This report shows the lines of that function that take up the most time, the time spent executing that line, the percentage of total time for that function that is spent on that line, and a bar graph showing the relative time spent on the line.

File Listing

The detail report for a function also displays all code for that function. This listing enables you to view the time-consuming code in the context of the entire function body. For every line of code that takes any significant time, additional performance information is provided by the statistics and by the color and degree of highlighting of the program code.

Checking It In

Source control systems offer a way to manage large numbers of files while they are under development. They keep track of the work done on these files as your project progresses, and also ensure that changes are made in a secure and orderly fashion.

If you have a source control system available, you will probably want to check your files into the system once they are complete. If further work is required on one of those files, you just check it back out, make the necessary modifications, and then check it back in again.

MATLAB provides an interface to external source control systems so that you can check files in and out directly from your MATLAB session. For more information, see:

Protecting Your Source Code

Although MATLAB source (.m) code is executable by itself, the contents of MATLAB source files are easily accessed, revealing design and implementation details. If you do not want to distribute your proprietary application code in this format, you can use one of these more secure options instead:

In general, if you want to run the code as a standalone application outside of MATLAB, it is best to use the MATLAB Compiler™ to make your code secure . If you plan to run the code within the MATLAB environment, there is no need to run the Compiler. Instead, convert to P-code those modules of your source code that need to be secure.

Building a Content Obscured Format with P-Code

A P-code file behaves the same as the MATLAB source from which it was produced. The P-code file also runs at the same speed as the source file. Because the contents of P-code files are purposely obscured, they offer a secure means of distribution outside of your organization.

Building the P-Code File.  To generate a P-code file, enter the following command in the MATLAB Command Window:

pcode file1 file2, ...

The command produces the files, file1.p, file2.p, and so on. To convert all .m source files residing in your current folder to P-code files, use the command:

pcode *.m

See the pcode function reference page for a description of all syntaxes for generating P-code files.

Invoking the P-Code File.  You invoke the resulting P-code file in the same way you invoke the MATLAB .m source file from which it was derived. For example, to invoke file myfun.p, type

[out, out2, ...] = myfun(in1, in2, ...);

To invoke script myscript.p, type

myscript;

When you call a P-code file, MATLAB gives it execution precedence over its corresponding .m source file. This is true even if you happen to change the source code at some point after generating the P-code file. Remember to remove the .m source file before distributing your code.

Running Older P-Code Files on Later Versions of MATLAB.  P-Code files are designed to be independent of the release under which they were created and the release in which they are used (backward and forward compatibility). New and deprecated MATLAB features can be a problem, but it is the same problem that would exist if you used the original MATLAB input file. To fix errors of this kind in a P-code file, fix the corresponding MATLAB input file and create a new P-code file.

P-code files built using MATLAB Version 7.4 and earlier have a different format than those built with more recent versions of MATLAB. You still can use these older P-code files when you run MATLAB 7.4 and later, but this capability could be removed in a future release. MathWorks recommends that you rebuild any P-code files that were built with MATLAB 7.4 or earlier using a more recent version of MATLAB, and then redistribute them as necessary.

Building a Standalone Executable

Another way to protect your source code is to build it into a standalone executable and distribute the executable, along with any other necessary files, to external customers. You must have the MATLAB Compiler and a supported C or C++ compiler installed to prepare files for deployment. The end user, however, does not need MATLAB.

To build a standalone application for your MATLAB application, develop and debug your application following the usual procedure for MATLAB program files. Then, generate the executable file or files following the instructions in Steps by the Developer to Deploy to End Users in the MATLAB Compiler documentation.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS