Pages - Menu

Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Thursday, 11 October 2012

Basic MATLAB commands

My previous article was about defining scalar variables and evaluating arithmetical expressions in MATLAB.

Now, we're going to practice that and I will show you some basic commands.

Let's say we want to keep track of what we do in this session. For this, we have the diary command.
>> diary

This function will create a text file named diary that you can open later with any text editor.

If you want to give the file a name of your choise, use
>> diary FileName

(MATLAB automatically colours the file name in magenta).

More information about this command can be found by typing 
>> help diary

To see the current directory, where this file has been saved, use the the cd command.

You can change the current directory by specifing the path between apostrophes.
>>cd 'D:/'

Let's make the following four variables: a=2.5×1023, b=2+3i (i being the imaginary number), c=ej2π/3, d=√36.
>> a=2.5*10^23, b=2+3*i
a =
  2.5000e+023
b =
  2.0000 + 3.0000i

In this case, the e notation is used for a very large number.

Notice that to enter multiple statements at once you can use commas.

For defining the c and d variable, we'll need some help from MATLAB and we'll have to search for the exponential function, for the pi constant and for a function that computes the square root of a number. 
>> lookfor exponential
Lookfor searches in the description of each function for a match, while help searches a function with the exact same name.
>> c=exp(j*2*pi/3); d=sqrt(36)
d =
  6

As you can see, semi-colons also allow placing several statements on one line, but, unlike commas, they will supress the output. 

For a list of the variables in the current workspace type:
>> who
Your variable names are:
a b c d

To see even more details about the variables, including size, class and attributes, use whos.

Remember that at the beginning of this session, you started a diary. Now, since the article is coming to an end, we should stop recording. For this, enter
>> diary off

 To view the diary file use
>> type('FileName')

 To remove all the variables from memory, MATLAB has the clear command.
>> clear

If you want to clear the Command Window, type
>> clc