You are on page 1of 4

MATLAB FUNDAMENTALS

1. MATLAB Environment

MATrixLABoratory, as shortly MATLAB is a program that allows engineers and scientists


to perform any type of calculations, statistical analysis, models and data visualizations.
Below you can see the graphical interface of this environment.

Figure 1. Graphical interface of MATLAB

Basically this environment consists of four main panels which are menu pane , current
folder , command window and workspace respectively. Current folder
shows which folder you are working on. It is important to right click on the folder and
select add to path current folder and sub-folders. Otherwise MATLAB can not find the .m
files or the data you want to work. Command window is the main calculation
environment. In the upcoming sections you will be familiar with the editor where your
scripts (.m files) works. Lastly the workspace shows the variables which can be the data
you’ve uploaded and/or the results of your calculations.

Let’s begin some calculations!

1.1. Scalars

You should write everything on command window.


For example, if you type

>> 24 + 12
MATLAB will display the result as

ans =

36
and stores the result in workspace as ans which is 1×1 matrix. Because MATLAB’s logic is
working with matrices. Once you store the result as ans, you can numerate any other
calculations with using ans as

>> ans /4

ans =

The problem here is the ans stored in workspace changes if you continue to numerate
calculations. Every answer overwrites on ans. If you want to store results to use them in
future calculations you have to give them names.

>> y = 4

y=

>> x = 52

x=

52

Also you can use uppercase and lowercase letters

>> X = 43

X=

43

>> x = 42

x=

42

x and X variables are different from each other.

You gave names to your variables and temporarily store them in workspace. There is no
necessity to show them answers in command window. This can be solved with using
semicolon

>> x = 42

x=

42
>> x = 42;

or

>> a = 4, b = 72;

a=

Once you gave names to variables you can call back them for calculations for example

c = b/a*x

c=

756

>> clear cleans the workspace


>> clc cleans the command window
>> pi gives the pi number and you can use it as variable in calculations
>> format long displays numbers as 14 digits after . “dot”.
>> format short gives four digits and this is default.

>> pi

ans =

3.1416

>> format long


>> pi

ans =

3.141592653589793

>> format short


>> pi

ans =

3.1416

1.2. Arrays, Vectors and Matrices

Single variable called as array and if your array is a one-dimensional data set then it is
called as vector. Vectors are also matrices for example

>>A = [4, 45, 17, 14]


A=

4 45 17 14

which is a 1×4 matrix.

Brackets [] are used to write the elements of matrices, arrays or vectors. Semicolons are
used to separate each matrix rows from each other

>>B = [12 14 15; 22 23 25; 32 33 34]

B=

12 14 15
22 23 25
32 33 34

' sign transposes the matrix

>>B'

ans =

12 22 32
14 23 33
15 25 34

If you want to call an element you should use for example

>>B(1,3)

ans =

15

which is as you may remember from your calculus courses 1 refers for the first column
and 3 refers for the third row.

>>B'

ans =

12 22 32
14 23 33
15 25 34

Assignment 1. Form a matrix which has 14 columns and 72 rows which consists of zeros.
Save your matrix as an .m file (script). Matrix and script should have their own names.
Give what you want. Keep that in mind that variable should be stored on workspace,
there shouldn’t be any answers displayed in command window. Upload the assignment
to the relevant assignment box in moodle. Deadline is 3rd March Wednesday, 09.59 am.

You might also like