Real Technology Tools

MATLAB Tutorial: Learn MATLAB Fast

In this article, you’ll learn the basics of MATLAB Programming in 5 minutes, including how to:

Creating Variables, Vectors, and Matrices

How to Define a Variable in MATLAB

To define a variable, write the name of the variable followed by “=” and the value of the variable:

numberVariable = 5; % variable containing a number
stringVariable = 'string'; % variable containing a string

Furthermore, MATLAB remembers what you wrote. If you want to access a command that you wrote earlier in your workspace, use the up arrow key to get your previous command.

How to Create a Vector in MATLAB

In MATLAB, you can create row vectors and column vectors:

If you want to learn all the technical stuff on vectors, see MATLAB Vector Tutorial: Create, Add, Concatenate, and Extract

How to Create a Matrix in MATLAB

You can create a matrix in MATLAB by writing numbers or strings next to each other to define the rows and using a semicolon to go to the next row:

 matrix = [1 2 3 ; 4 5 6 ; 7 8 9];
 stringMatrix = [{'position'} {'speed'} ; {'size'} {'acceleration'}];

The result is the following matrices:

\(matrix=\begin{pmatrix}1 &2 &3\\4 &5 &6 \\7 &8 &9\end{pmatrix}~~~~~~~~~~~~~~~~~~stringMatrix=\begin{pmatrix}\{‘position’\}& \{‘speed’\}\\\{‘size’\} & \{‘acceleration’\}\end{pmatrix}\)

You can then multiply matrices as the following:

 A*B % classic matrix multiplication in MATLAB

If you want to learn more about matrices, see MATLAB Matrix: Create, Transpose, Extract, Multiply Matrices

MATLAB: Function and Script

Writing a Script

To write a new MATLAB script, follow these steps:

  1. Go to the top left of your MATLAB windows, under the “HOME” tab
  2. Click on “New Script“:
    define a MATLAB function by clicking on New Script
  3. Save your script by using the keyword shortcut “CTRL+S” or by clicking “Save” at the top left of the MATLAB editor:

Defining a MATLAB Function

Here’s the syntax to define a MATLAB function:

function [out1, out2, out3] = functionName(in1, in2)
 
Instructions
 
end

If you want to know all the nitty-gritty details of MATLAB functions, see MATLAB Function: Vector, Matrix, Or Single Input/Output Functions.

Creating Loops and Conditions

MATLAB for Loop

You can define a for loop in MATLAB as the following:

for i = 1:n % n is the number of loops you want
    Instructions; % what you want to do n times
end

For example, here’s how to create a for loop that counts to 10:

for i = 1:10
    i
end

To see more examples of for loops, see MATLAB For Loop: Example of Simple, Backward, and Nested For Loops.

While Loop

If you want to define the while loop in MATLAB, you can use the following syntax:

while condition 
      Instructions
end

For example, here’s how to create a wide loop that counts to 10:

i = 10;
while i~=10 % while i is not equal to 10
      i=i+1
end

If Else MATLAB

The basic if else MATLAB structure is:

if firstCondition
      firstInstruction
elseif secondCondition
      secondInstruction
else
      otherInstructions
end

For example, if your condition is “A is not equal to B,” you’d have:

If you want to learn more complicated examples such as switch case structures, A less than or equal to B, or strings and logical operators, see If Else MATLAB: Conditional Statements if A Is Not Equal to B.

MATLAB: Reading and Writing Files

Text Files

Excel Files

Exit mobile version