| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Selecting the Right Data Structures |
When planning how to write a program, take the problem you are trying to solve and break it down into a series of smaller, independent tasks. Implement each task as a separate function. Try to keep functions fairly short, each having a single purpose.
You may find it helpful to write the initial draft of your program in a structured format using your own natural language. This pseudo-code is often easier to think through, review, and modify than using a formal programming language, yet it is easily translated into a programming language in the next stage of development.
Look at what classes and data structures are available to you in MATLAB and determine which of those best fit your needs in storing and passing your data.
For more information: see Classes (Data Types) in the Programming Fundamentals documentation.
A few suggested programming practices:
Use descriptive function and variable names to make your code easier to understand.
Order subfunctions alphabetically in an M-file to make them easier to find.
Precede each subfunction with a block of help text describing what that subfunction does. This not only explains the subfunctions, but also helps to visually separate them.
Do not extend lines of code beyond the 80th column. Otherwise, it will be hard to read when you print it out.
Use full Handle Graphics property and value names. Abbreviated names are often allowed, but can make your code unreadable. They also could be incompatible in future releases of MATLAB.
To avoid choosing a name for a new function that might conflict with a name already in use, check for any occurrences of the name using this command:
which -all functionname
For more information: See the which function reference page.
Be sure to document your programs well to make it easier for you or someone else to maintain them. Add comments generously, explaining each major section and any smaller segments of code that are not obvious. You can add a block of comments as shown here.
%--------------------------------------------- % This function computes the ... <and so on> %---------------------------------------------
For more information: See Comments in the MATLAB Programming Fundamentals documentation.
Do not try to write the entire program all at once. Write a portion of it, and then test that piece out. When you have that part working the way you want, then write the next piece, and so on. It's much easier to find programming errors in a small piece of code than in a large program.
When making modifications to a working program, do not make widespread changes all at one time. It's better to make a few small changes, test and debug, make a few more changes, and so on. Tracking down a difficult bug in the small section that you've changed is much easier than trying to find it in a huge block of new code.
If you have a function that is called by only one other function, put it in the same M-file as the calling function, making it a subfunction.
For more information: See Subfunctions in the MATLAB Programming Fundamentals documentation.
One suggested practice for testing a new program is to step through the program in the MATLAB debugger while keeping a record of each line that gets executed on a printed copy of the program. Use different combinations of inputs until you have observed that every line of code is executed at least once.
![]() | Function Arguments | Debugging | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |