Hello peeps! Welcome to another tutorial on MATLAB where we are starting the code learning and its procedures in this lecture. In the previous sessions, we have seen the windows through which you can work in different ways for specific tasks. The focus of that lecture was on the figure window and editor window. Now, we are coding in these windows. You will see the implementation of these tasks in the command prompt because we are dealing with arrays as well. So, have a look at the topics that will be cleared in your mind after having the knowledge of this lecture.

• What are the variables in MATLAB?

• What is the relationship between the matrix and variable?

• How to use the built-in functions of the MALTAB in command prompt?

• Give the example of built-in functions.

• How can you input a string message or store the results in the form string.

We’ll try to get the example fo each case and make sure to provide you with the easiest but accurate information.

## Variables in MATLAB

In the prerequisites of this course, we have told you that one must have a basic knowledge of programming language. So, I am assuming that you know what actually variables are. Yet, for the sake of revision, I am discussing the concept of variables. The structural unit of the programming in MATLAB is the array. As you know, MATLAB work very closely with arrays and variables are also saved in the form of arrays in MATLAB. We have a detailed series about the matrices in MATLAB and you will see the implementation of the arrays in the command prompt window.

### Naming the Variable in MATLAB

If you are confused that how MATLAB do all its task in the form of arrays, then have a look at the description of the procedure that we are giving you now. The assignment statement can be elaborated well with the help of this statement:

var = expression;

Consider the case when we save any value in any variable say A. The following statement is used in the command prompt.

A= 23

Unlike some other languages such as C++, there is no need to define the type of value in every case when declaring the variables' name in MATLAB. We have simply told MATLAB that there is a variable A with the data type int because we are saving an integer value in it. MATLAB stores this variable in the form of a one-dimensional array where the integer value 23 is saved with the name A. Now the question arises, what if we do not declare any variable name and just declare the operation on the value? This can be illustrated by the statement given below:

sqrt(22)

When you put the above statement in MATLAB’s command prompt, you will get the required results you were expecting. Yet, as you have not given this value any name, by default, your answers are shown with the name “ans” if you do not specify the name of the variable. All the above discussion will be cleared up with the help of the following image.

The above discussion was made to provide the firm basis of variables and matrix in MATLAB, but now have a look deep into this procedure and check the link between these two terms.

### Relation of Variable and Matrix

We all know that matrices are the types of arrays that vary from one-dimensional structures to multi-dimensional according to the declaration and needs of the user. The dimensions of the matrices depend upon the number of rows and columns that we declare when introducing the matrices in MATLAB. In the case discussed above, the matrix was one-dimensional because we used a single value in the matrix, so basically, we used one row and one column only.

The area of the memory in MATLAB, where the variable is sorted, is saved with the customer-specified name, as we have seen before in this topic, but here, it is important to notice that you can change the name of the variable at any time and the data saved in the variable will not be affected.

While naming the arrays and, in return, we can say any variable, we have to keep in mind that the name of any variable starts with a letter. You can use special signs such as underscore after the first variable but not in the first place.

## Using Built-in Functions of MATLAB

The real magic of MATLAB lies in its built-in functions. It seems that the designers and programmers of MATLAB have made a special effort to make the working of the mathematical functions easy and interesting, so they have made the built-in functions of the mathematical functions that, in real life, are difficult to perform and take time. A slight mistake in any step can alter the results, and it becomes difficult to recognize the mistake. Yet, with the help of built-in functions, you can easily put the values in the formulas and you are good to go. To explain this well, let me tell you about a simple type of matrix.

### Zero Matrix in MATLAB

A unit matrix is a type of matrix that has all the values zero in it. It is not empty, but at every place of the value, zero is saved. At the start, you must be thinking, why do we use this matrix? But these types of matrices are the basis of important functions and features of mathematics. Well, let’s design your own zero matrices of your own choice. The values obviously remain zero in every case, but the size of the zero matrices varied and, for practice, we are using the three examples.

To apply the zero matrix at the command prompt window, we use the following formula:

x= zeros(3)

This will create a square zero matrix with three rows and three columns. It makes sense because we are providing MATLAB with only a single value; therefore, it uses it for the informational rows and columns.

In the case when we want the rectangular matrix, we can use the same formula with two values inserted into it.

y = zeros(3,2)

Another way to use the zeros matrix with the indirect declaration is to simply save the values that will be inserted in the zero matrix formula into a separate variable, and then simply enter that variable into the square matrix formula.

c = [1 2; 3 4]

z = zeros(size(c))

In this way, I have taught you the usage of the size variable, which is another built-in function that does not look at the values saved in the matrix but just picks the size of the matrix.

We hope it is clear to you now. There are certain matrices mentioned in the previous session; therefore, I am not explaining more. For your practice, check and implement the table given elbow in your MATLAB command prompt.

So, have you noticed that in our third example, we have made a matrix of size 2 by 2 and MATLAB just picked the size and used it for the formation of a zero matrix of the same size. It is one of the examples of a matrix into a matrix.

 Pre-defined Function Output of the function zeros(n) It creates a square zero matrix with the order nxn. zeros(m,n) It creates a zero matrix with the order mxn. zeros(size(arr)) It creates a zero matrix with an order equal to the size of the array mentioned before using this function. eye(n) It creates a square identity matrix with the order nxn. eye(m,n) It creates an identity matrix with the order mxn. ones(m) It creates a square ones matrix with the order nxn. ones(m,n) It generates a ones matrix of order mxn. length(arr) It tells the length of the array. size(arr) The size of the array is examined with the help of this command.

## Variables with the Keyboard Input in MATLAB

Up till now, we were dealing with the variables that were related to numbers and the functions related to numerical operations. But, just like other programming languages such as C#, MATLAB also provides you with the facility to get the output in the form of a string.

If you want to print the message on the screen or want to have the functionality of taking input from the user, then you can do so easily by following the instructions given below:

To print the message in the form of a string when you want to take the input from the user, you use the following command:

user_input=input(‘message from the input’)

Here, we have specified the general representation of the whole command, and if it is not clear to you right now, you will get it when you have a look at the example given next. This command does two tasks:

1. It prints the exact words on the screen that are written into inverted commas.

2. It stops the compiler here and waits for the user to input the required values so that the compiler may use it for further calculations.

It is interesting to note that the user has to provide valid input all the time. MATLAB has the functionality to check the input type and it throws an error instantly. Another important point to notice is that we will write the code at the command prompt in our case. Yet, you can also write the same code in the editor window or live script, and in that case, the input stream will be shown on the command prompt presented just below the live editor. Let’s see all this discussion in action with the help of MATLAB.

We are using the simple addition operation in the command prompt to explain the workings of the input stream. Here is the code for it.

The user then gives the input properly and gets the results instantly.