Skip to main content Accessibility help
×
Hostname: page-component-78c5997874-g7gxr Total loading time: 0 Render date: 2024-11-18T20:42:45.417Z Has data issue: false hasContentIssue false

6 - MATLAB Programming

Published online by Cambridge University Press:  05 September 2012

Brian R. Hunt
Affiliation:
University of Maryland, College Park
Ronald L. Lipsman
Affiliation:
University of Maryland, College Park
Jonathan M. Rosenberg
Affiliation:
University of Maryland, College Park
Kevin R. Coombes
Affiliation:
University of Texas, M. D. Anderson Cancer Center
John E. Osborn
Affiliation:
University of Maryland, College Park
Garrett J. Stuck
Affiliation:
University of Maryland, College Park
Get access

Summary

Every time you create an M-file, you are writing a computer program using the MATLAB programming language. You can do quite a lot in MATLAB using no more than the most basic programming techniques that we have already introduced. In particular, we discussed simple loops (using for) and a rudimentary approach to debugging in Chapter 3. In this chapter, we will cover some further programming commands and techniques that are useful for attacking more complicated problems with MATLAB. If you are already familiar with another programming language, much of this material will be quite easy for you to pick up!

✓ Many MATLAB commands are themselves M-files, which you can examine using type or edit, e.g., enter type isprime to see the M-file for the command isprime. You can learn a lot about MATLAB programming techniques by inspecting the built-in M-files.

Branching

For many user-defined functions, you can use a function M-file that executes the same sequence of commands for each input. However, one often wants a function to perform a different sequence of commands in different cases, depending on the input. You can accomplish this with a branching command, and, as in many other programming languages, branching in MATLAB is usually done with the command if, which we will discuss now. Later we will describe the other main branching command, switch.

Branching with if

For a simple illustration of branching with if, consider the following function M-file absval.m, which computes the absolute value of a real number.

Type
Chapter
Information
A Guide to MATLAB
For Beginners and Experienced Users
, pp. 85 - 102
Publisher: Cambridge University Press
Print publication year: 2006

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×