Command window in MATLAB

In the command window, in front of the >> sign you can enter your commands. Then, press Enter to execute the commands in MATLAB. Without any explanation, let’s write the first command in MATLAB. We want to multiply two numbers. Multiply the numbers 3 and 5 in front of the >> sign by the 3 * 5 command :

>> 3 * 5 
ans = 

The variable ans in MATLAB

The Windows workspace look. As a reminder, this window holds the value of all defined variables. Now, by writing the 3 * 5 command, a variable called ans with a value of 15 is displayed in it. ans stands for answer. When we do not include any variables for our command, MATLAB automatically saves the answer in ans. Because, maybe we can use it later when we need to.

We said that if we do not define a variable, ans will occur. But how to define a variable for the 3 * 5 command? Let’s go to the next part of the variable definition tutorial in MATLAB…


Define a variable in MATLAB

To define a variable in MATLAB, it is enough to write the 3 * 5 command as follows:

>> 3 * 5 
ans = 

After executing the above code, look at the workspace window. Now a variable called m has been added whose value is 15. But what is the (=) sign in the above command? The equal sign (=) in MATLAB is equivalent to the assignment operator. That is, the expression to the right of the equals sign (which in the previous example is 3 * 5) is assigned to the variable to the left of the equals (which in the previous example is m). When you enter an expression such as x = 3 * 5, MATLAB first calculates 5 * 3 and then assigns the result (15) to the variable x.


Attention, in the free MATLAB training course, we have provided some exercises for you. The answer to the exercises appears by clicking on each exercise. Please, please, please solve the exercises yourself first and then see the answer! 😑


Exercise 1: Add the sum of two numbers 8 and 1 to a variable called s.

>> s = 1* 8 
s = 

Exercise 2: Create a variable called y that has a value of m / 2.

>> y = m / 2 
m = 

Exercise 3: Enter the command m = m + 1 and see what happens.

>> m = m + 1 
m = 




Well, we recommend that you practice. But we know that many people say that this is simple and I do not need to practice! We quote the above sentence from Albert Einstein to say that these are simple in their words. But when you go into coding, you see that it’s not that simple. Friends, coding is like math. Nothing happens by memorizing and reading. Hands must be on the keyboard to reach the answer.

Do the choice of variable names have rules and principles or can we choose whatever we want? In the next section, we will teach you how to easily choose the right variable name with a few simple rules.



Rules for defining variables in MATLAB

In the previous section, we talked about the definition of a variable in MATLAB. But in this section, we want to define the variables in more detail. Because although the definition of variables is very simple, many people make problematic mistakes due to their lack of information. Defining variables in MATLAB is simple. You can create your own variables in MATLAB with a few simple tips.

There are three very simple rules for defining a variable in MATLAB:

  1. Variables can only contain letters, numbers, and the “_” sign.
  2. Variables must start with letters.
  3. Variables are case-sensitive.


Note Although we said you do not have a hard time defining the names of the variables. But we recommend that the names of your variables be meaningful. Writing variables in abc, etc. may mislead you. For example, you want to create a variable for the concept of speed in your work. Do not write a variable called a or b. It is better to define a variable called speed, for example. Increase the variables in your code and if you do not define it correctly, catastrophe will occur…

Note Do not define the name of the ready-made functions in MATLAB as a variable. If we have a command called round in MATLAB, you do not use the exact same variable in your code. It will lead you astray later…


Exercise 4: Which variables are correct to use? 1) velocity,    2) s12,     3) m_n, 4) m_,     5) 23m,     6) Mm,      7) 2_m,        8) m___3,      9) s_s_s



1) True (good variable)                2) True                              3) True                                    4) False (violation of rule 2)   5) False (violation of rule 2)              6)True                    7) True               8) True                       9) True



Exercise 5: Assign the value 2 to variable A.

>> A = -2 
A = 



Exercise 6: Consider the value of the variable a 10 and calculate the value of 2 / (a ​​+ A) and assign its value to the variable avgAa.

>> a = 10 ;
>> avgAa = ( a + A ) / 2
avgAa =




Well, halfway through the second free MATLAB training session. We taught the definition of a variable in MATLAB very simply. In the following, we will mention a number of tricks in MATLAB…


The semicolon sign in MATLAB

In the commands you have written so far, the result will be displayed immediately after pressing Enter. But by adding a semicolon character in MATLAB to the end of the command, the output is no longer displayed. Before explaining further, first write the following two commands to see the result:

> p = 10 - 8 
p = 
>> q = 10 - 8 ;

In the first command, output p is displayed. But in the second-order that; Or has the same semicolon, the output value is no longer displayed. However, as you can see in the workspace, both variables have their values ​​stored and the answer is correct. But only their show is different. One without; Displays the output and the other does not.

You may be wondering, shouldn’t the result always be displayed? The answer is no! However, in these initial examples, we want to show the result. But you should know that sometimes showing the result will be a problem for us! This will become more apparent to you once you become familiar with arrays. For example, suppose the result of the code we wrote is a matrix with dimensions of 1000 × 1000. Then without a semicolon, all 1 million elements will be displayed. Let’s not forget that displaying 1 million numbers in MATLAB Command Window is not a good idea. The problem is that it takes a total of 1 million copies to print.


Exercise 7: The result of subtracting two numbers 8 and 2 in a variable called k with and without; Pour.

>> k = 8 – 2 
k = 
> k = 8 – 2;


Command History in MATLAB

By now, you have probably written about 10 commands! We want to go back to one of the first commands, m = 3 * 5. Suppose we just realized that we had written this command incorrectly, and instead of the number 3, we should have written the number 4. One way is to rewrite the command we wrote earlier:

> m = 4 * 5 
m = 

But, if we have a long command like the following command. Just want to change a number, it’s hard to rewrite all the commands:

> m = 3 * 5 * 6 * 7 * 9 * 10 ;

An easy way to access the commands we have already written is to use the Up ( Down arrow ) and Down (down arrow) keys on the keyboard. With these two keys, you can easily navigate between the commands you have written so far and change whichever you want! Now press the Up key until you reach the command m = 3 * 5 and then replace the number 3 with 4.

Reminder In the first session of MATLAB training, we got acquainted with Command History. We said that with double-click and drag-drop you can use the previous commands in MATLAB. In the above technique, we taught you another way to see previous commands.


Exercise 8: With the Up and Down keys, select the command m = 3 * 5 and put the variable k instead of 5.


>> m = 3 * k 
m =