MATLAB Programming/The MATLAB Command Prompt

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

MATLAB Programming

Chapter 1: A Tutorial Introduction

Chapter 2: Basic MATLAB Concepts

Saving and loading a MAT-file
MATLAB's Command Prompt
Basic Reading and Writing Data from a File

Chapter 3: Data Storage and Manipulation

Data Types and Operations on Point Values

Boolean and Rational
Strings
Portable Functions
Complex Numbers

Arrays and Matrices

What is an array?
Introduction to array operations
Vectors and basic vector operations
Struct Arrays
Cell Arrays
Sparse Matrices

Chapter 4: Graphics

Basic Graphics Commands
Annotating Plots

Chapter 5: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
MATLAB Caveats
Debugging M Files

Chapter 6: Mathematical Manipulations

Linear Algebra

Simple matrix manipulation
More complicated matrix operations

Differential Equations

Ordinary Differential Equations
Partial Differential Equations

Chapter 7: More advanced I/O

Writing and Reading to a Serial Port
Writing to a USB Port

Chapter 8: Examples

Filtering
Controls
Phase Vocoder and Encoder

Chapter 9: Object-Oriented Programming

Struct arrays
MATLAB Classes

Chapter 10: An alternative to MATLAB: Octave

What is Octave ?
Octave/MATLAB differences

Chapter 11: Toolboxes

Symbolic Math Toolbox
GUIDE
Simulink
Psych Toolbox

edit this box

Contents

[edit] Introduction

MATLAB is interesting in that it is dynamically compiled. In other words, when you're using it, you won't run all your code through a compiler, generate an executable, and then run the executable file to obtain a result. Instead, MATLAB simply goes line by line and performs the calculations without the need for an executable.

Partly because of this, it is possible to do calculations one line at a time at the command line using the same syntax as would be used in a file. It's even possible to write loops and branches at the command line if you want to! Of course this would lead to a lot of wasted effort a lot of the time, so doing anything beyond very simple calculations, testing to see if a certain function, syntax, etc. works, or calling a function you put into an .m file should be done within an .m file.

[edit] Calculator

MATLAB, among other things, can perform the functions of a simple calculator from the command line. Let us try to solve a simple problem: Sam's car's odometer reading was 3215 when he last filled the fuel tank. Yesterday he checked his odometer and it read 3503. He filled the tank and noticed that it took 10 gallons to do that. If his car's gas tank holds 15.4 gallons, how long can he drive before he is going to run out of gas, assuming the gas mileage is the same as before?

First let us compute the distance Sam's car has travelled in between the two gas fillings

>> 3503-3215
ans =
   288

Gas mileage of Sam's car is

>> 288/10
ans =
   28.8

With this, he can drive

>> 28.8 * 15.4
ans =
  443.5200

443.52 miles before he runs out of gas.

Let us do the same example, now by creating named variables

>> distance = 3503-3215 
distance = 
   288 
>> mileage = distance/10
mileage =
   28.8000 
>> projected_distance = mileage * 15.4
projected_distance =
  443.5200

To prevent the result from printing out in the command window, use a semicolon after the statement. The result will be stored in memory. You can then access the variable by calling its name. Example:

>>projected_distance = mileage * 15.4;
>>
>>projected_distance
projected_distance = 
  443.5200

[edit] Using the command line to call functions

The command line can be used to call any function that's in a defined path. To call a function, the following general syntax is used:

>> [Output variable 1, output variable 2, ...] = functionname(input 1, input 2, input 3,...)

MATLAB will look for a file called functionname.m and will execute all of the code inside it until it either encounters an error or finishes the file. If the former is true, you'll hear an annoying noise and will get an error message in red. In the latter case, MATLAB will relinquish control to you, which you can see when the >> symbol is visible on the bottom of the workspace and the text next to "start" button on the bottom-left of the screen says "ready".

Use this in order to call homemade functions as well as those built into MATLAB. MATLAB has a large array of functions, and the help file as well as this wikibook are good places to look for help on what you need to provide as inputs and what you will get back.

BE CAREFUL because the syntax for functions and for indexing arrays is the same! To avoid confusion, just make sure you don't name a variable the same as any function. To ensure this, type the name of the variable you want to define in the command prompt. If it tells you:

Error using ==> (functionname)
Not enough input arguments.

then you'll have a conflict with an existing function. If it tells you:

??? Undefined function or variable '(functionname)'

you'll be OK.

[edit] External Resources

ControlTheoryPro.com