MATLAB Programming/The MATLAB Command Prompt

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction[edit]

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 often lead to a lot of wasted efforts, 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.

Calculator[edit]

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

Using the command line to call functions[edit]

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:

>> [Output1, Output2, ..] = functionname(input1, input2,..)

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. In the former case, it produces a noise and displays 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; 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.

External Resources[edit]

ControlTheoryPro.com