MATLAB Programming/The MATLAB Command Prompt

IntroductionEdit

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.

ExamplesEdit

MATLAB can perform the functions of a simple calculator from the command line. Here is a simple mathematics 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


External ResourcesEdit

ControlTheoryPro.com