Lesson finished
Lesson finished
Lesson finished
Lesson finished
Up next: MATLAB Tables, Functions, Callbacks and Scripted Simulink Models
Unlock all 12 lessons of Introduction to Model-Based Development using MATLAB and SimulinkFull course ₹40,000 · paid
MATLAB Scripting Basics: Interface, Variables and Data Types
A free lesson from Skill-Lync's Introduction to Model-Based Development using MATLAB and Simulink course, on Grow by Skill-Lync · Last updated 16 Nov 2026 · Session: “1.0 - Introduction to MATLAB scripting and Syntax Basics”
What will you learn in this free Introduction to Model-Based Development using MATLAB and Simulink lesson?
This lesson introduces MATLAB as Matrix Laboratory and tours the home screen, including the command window, workspace and command history. It then covers commands, variables and data types, colon notation, operators and decision-making statements, and ends with strings and cell arrays demonstrated in the tool.
- Roles of the command window, workspace and history
- Default double type and casting with uint8
- Colon notation such as 1:10 and 100:-5:50
- Cell arrays for mixing data types in one array
What comes after lesson 1?
The next free lesson is lesson 2, “MATLAB Tables, Functions, Callbacks and Scripted Simulink Models” (133 min). Later in Skill-Lync's Introduction to Model-Based Development using MATLAB and Simulink course (paid): lesson 4, Ports & Subsystems; Atomic Subsystems; lesson 5, Mathematical Model Representation & System Models Creation in Simulink; lesson 6, Overview of Automotive Industry. The full course has 12 lessons (28 h 4 min of video); see the course overview or its price and syllabus.
Lesson transcript
Machine transcript of the lesson audio (lightly cleaned). Technical terms may contain errors; the video is the reference.
Part 1
Welcome to today's session on ADAS model-based design-related concepts. So before going into the ADAS, which is Advanced Driver Assistance System and Model-based Design-related concepts, we will have a complete overview on the basics of MATLAB, which includes the MATLAB scripting as well as Simulink model development, which forms as the prerequisite and the base for the future topics and the development of projects and ADAS-related applications. So we start off with the basics of MATLAB scripting. So today's class involves the following topics, which is the basics of syntax, the variables, commands, and the long assignments.
So the list of topics for the session today would start off with about MATLAB. So for people who are completely new to MATLAB, they will have an overview of what MATLAB is, what kind of a simulation software it is, and what are the prerequisites to start your course on MATLAB. That would be followed by how a MATLAB user interface looks like and the various basic settings of the MATLAB. Then the next technical topics that we would be covering here is like about the basic syntaxes. That will be followed by the different MATLAB commands.
So there are n number of commands that are in use. We will have a look at a few of those commands, basically what they are. That will be followed by the MATLAB variables, their definitions, their basic level of manipulations, declaration of those variables in your command window. So that would be followed by long assignment of variables. So about MATLAB, MATLAB is expanded as Matrix Laboratory. This is a high-level programming language tool, which is widely used for computational analysis, control, signal processing, and in communication fields. Also, MATLAB is owned, developed, and supported by MathWorks.
So MATLAB, as it says here, it's a high-level programming language. It is not merely a tool that is only for programming purposes. That is, you can also do simulation, and it has a very, very good and understandable user interface where you can develop real-time models and try to test them, validate them all at the computer level. And also, you can go for auto-code generation and others. And as far as MATLAB is concerned, it is one of the most widely used tool in automotive-embedded software development domain.
As far as the stage before automotive software development is basically the design and development of the algorithms and ideas, which is all done on the MATLAB platform. The initial level of testing for automotive applications is also done at the MATLAB platform at different stages, which we will have a detailed view of it in the forthcoming classes as well. So this is the overview about MATLAB here. And what are the prerequisites to start MATLAB? Any end user who would like to get a hands-on experience on MATLAB is required to have a basic idea on what differential equations are and about linear algebra.
Basic programming skills with respect to C programming will be very useful. Basic computational concepts will also be very useful. And the complete course content of ADAS, that is Advanced Driver Assistance System, will be delivered using MATLAB and Simulik, basically. So this is how the MATLAB home screen looks like. So the screenshot here shows it is from MATLAB 2015. And this thing, as my mouse is pointed over here, the current working directory is the folder, which is the default folder that appears when you open the MATLAB.
We can also set it to a user-defined folder as per our requirements. So the current folder, whatever that is there in this part, the list of different files and folders, whichever is available in it, is listed out in this column on the left-hand side. So the center part of it is called as the command window, which is the space for command execution, basically. If you want to load any data into your workspace, if you want to define any variable, all of this can be, if you wanted to open any specific application, if you wanted to open a program, open a model, etc., anything and everything here can be done through this command window.
Command window is basically the talking point between the developer and the tool. It is through the command window that we basically execute all our applications and other things. So on the right-hand side, we have two things. One is the workspace. Workspace is any variable that you load into your command window will automatically be loaded here. For example, you're declaring a variable in your command window with a specific initial value. That variable with its given variable name and its initial value will be loaded into my workspace.
In any other further place, in any other program or in any other model in the future, if that variable is to be used, that value will automatically be taken from this workspace. So this command window and workspace basically are two very, very important components of a MATLAB tool. A basic understanding of how they are and how they look like is actually necessary. And a command history is something which will have all the history and the list of commands whichever we type in MATLAB. For example, I am typing something like a clear screen or I type clean CLC or clear or I declare something like A is equal to 5, B is equal to 4, etc, etc.
Whatever variables I declare or any other MATLAB functions that I try to, MATLAB commands that I try to execute will all be listed out in my command history, which usually remains at the bottom of the screen. If you wish to change the layout of your MATLAB home screen, that is again possible with the layout block, which is shown here. There is this Simulink library block, which will give you the list of Simulink library palettes, which you will be using it in your model development. But in detail about Simulink library, we will be discussing in the future chapters.
Now, I think we can have live view of this MATLAB. So this is my current working folder in desktop, ADAS MATLAB mscripting. We can change any other folder as well here. If you wish to set any other folder as your working folder location, I think that is again very much possible. We can see here. I would like to go to documents. I would like to go to MATLAB and if you wish to have MATLAB as your current working folder, you can simply select that folder name.
You can paste it here. That's it. The folder gets set here. If you wish to go back to the previous folder location, you can click on this. The immediate folder folder here is appearing at the top, which is basically desktop ADAS MATLAB mscripting. I can go and I can select it over here. Which version of MATLAB that you are currently using is displayed at the top left corner. In this case, it is MATLAB 2019A that we are using. And also, as we have seen in the PPT, the list of files, whichever is present in my current folder is available on the left hand side here.
Likewise, this is the workspace. Currently, we have not loaded any value into my workspace. So, it is empty and blank. And this is the command history. The latest list of commands, whichever we have executed here, is at this bottom right corner. So, this is as far as the MATLAB tool is concerned. Now, the next set of topics that we are going to look at is the syntax basics. So, these are some of the basic syntax which we can type and use in our command window. So, these syntax are, see, there are n number of syntax and basic commands that one must take care of.
But these are some of the very, very basic ones which we can actually get reminded of. So, one is the semicolon which indicates that it is the end of statement. Like after any variable declaration or after any other command you execute, any inbuilt MATLAB command that you execute, you can put a similar link, a semicolon which denotes that it is the end of the current statement. Then there is percentage which is again a very, very important and a useful syntax which means that anything that follows after this percentage denotes that it is a comment.
Okay. So, that appears in light green color which we can see. So, anything that appears after this percentage will be a comment and any text that you type between two percentages stand as comment. So, anything after the second percentage in that case won't count as a comment usually or anything that you type from the next line after this percentage comment does not come under a comment. That comes under any other MATLAB command. On the multiple pages will end that. And they remain numbers. Most important or the most frequently and commonly used MATLAB commands are the CLC, clear all, and the close all.
The CLC stands for clearing the command window. Clear all stands for clears all the variables, whichever is loaded in the workspace. And close all stands for close all currently open and running applications of MATLAB. So this is about how the MATLAB variables are declared. So in MATLAB, every variable is an array or a matrix. OK. And for example, if A is equal to 2 is executed, it returns a value of variable as 2. That is, whenever you're going and typing in my command window as A is equal to 2, the return string would be that that variable A has been assigned to a value of 2.
OK. Also, MATLAB defines a variable and initializes the same with an expression. That is, say I am having B is equal to square root of 9. This will now return me a value of the square root of 9 is 3. So the B value, whatever that is coming as an output would be as B is equal to 3 here. In this screenshot, it is shown as X is equal to 3. So this will be done. Likewise, directly, instead of assigning it to a specific variable as B or X, you can directly give this square root of 25, which means that in the workspace, it will be stored inside an expression called ANS, which means that it is not assigned to any specific variable.
But currently, it is stored in my workspace as something like a local one. It denotes that. So we can have a quick hands-on session on how we declare the variables into my MATLAB. So let's move to MATLAB. So this is my command window. All that we have seen here is A is equal to, say, 5. It returns me this value as 5. And likewise, if you can see in my workspace, it has been defined as this is a variable. If I have located the pointer on top of A, you can see here it is displaying as 1x1 double, a pop-up is coming.
So the 1x1 double here denotes the dimension of it and double denotes the data type of this variable A, which is all taken by default in MATLAB. You can double-click on your workspace command and see that it has been assigned to 5. As we have already seen in the presentation slide, the variables are usually taken in the form of array or matrix. That's why it is denoted as 1x1 here. And that's why it is loaded in the first row and the first column of this variable.
The value of 5 is now loaded. If you can remember that, we have also seen about certain syntaxes like semicolon and the others. Now let us see what happens when we try to use semicolon here. I am now go for a declaration as D is equal to 6. OK. And I am putting a semicolon. Now when I give enter, the return string as B is equal to 6 does not appear in my MATLAB. So which means as operation of semicolon suggests that that is the end of statement.
The statement MATLAB considers that B is equal to 6 is the end of statement. It loads the variable into my workspace, but it does not pass a return string back in my command window. So this is how it usually works. So this is the function of semicolon. If you want any while executing a program, if you want any particular variable which and what you have assigned to be displayed on your command window, you need not use semicolon for that particular statement, else you can finish the statement with a semicolon which and when while running it will load the values into the workspace and it takes.
So this is about the basic of assigning the variable values into my workspace. And likewise, we can also see about this square root. X is equal to square root of 9. Now when I load, the value X is equal to 3 is returned. Likewise, I am giving something as square root of 25. When I give square root of 25, it returns answer is equal to 5. As we have already seen in workspace, it is a variable. It is not assigned to any particular variable name. But this expression called ANS, which is answer, is loaded in my workspace with a value as 5.
Likewise, multiple variable assignments is also possible with MATLAB. We have already seen here declared A is equal to 5 and B is equal to 6. Now we can give a mathematical expression like C is equal to A plus B, right? When I give C is equal to A plus B in my workspace, A is defined as 5, B is now defined as 6. When I give C is equal to A plus B, the return string would be C is equal to 11. So likewise, the mathematical basic operations like this can be done in my workspace.
So this is the basic head start on how you will start working and playing with MATLAB mscripting. So this is exclusively for the beginners. So let's move to the next set of topics. So about the multiple variable assignment, we have already seen. The next one is about the WHO command. So there are two different commands, WHO and WHOSE. We will be having a look at them. So the command WHO, when you type WHO in command window, it displays all the variable names that you have used so far.
And when you type WHO in command window, it will not only display the variable names, but also it will display the variable size, their dimension, their memory allocation, etc. So we can quickly have a look at how that is done. So I am going to simply type WHO. It tells that in my workspace, these are the different variables that is available. So A, A and S, that is answer, B, C and X. Now I am going to type as WHOSE. So when I type WHOSE, it is going to throw me a return string.
It displays a table like one in my command window. It lists out all my variables that I have declared in the workspace on my first column. That next column contains the variable size. The third column contains the byte information. The fourth column contains what is the data type that is being used. The default data type that MATLAB takes is double data type. And the dimension of this is one cross one because it is a single numeric value that we have given. So the next we would be going.
OK, we have also seen about the CLC and the clear all. Now I have already said that, OK, if I wish to make my workspace clean, I will use CLC. If I wish to clear all the variables in my workspace, if I wish to clear out all the information that is available in my command window, I will use CLC. If I wish to clear out all the information in my workspace, I will use clear all. So first I will use CLC to see my command window has become clean.
Also, if I wish to call back all the variables, whichever I have already loaded in workspace, I can simply type A. It has been already declared as 5. So it comes if I give C, C comes as 11. So which means that and the values are stored in my workspace and that comes back that is actually called back here. But now when I use the clear command, it will completely wipe out that particular variable from my workspace. As you can see, that a set of variables are wiped out from my workspace.
And now when we give A, it throws the string as undefined function or variable A. So this is how the basic toggling operations as far as MATLAB with playing with my command window, my workspace usually happens. OK. Other set of basic commands that we use in MATLAB are the CLC. We have already seen about that clears command window. Clear removes the variable from workspace memory, checks for the existence of variable or file global, declares variables to be global. Help searches for a help topic. Look for searches help entries for a keyword.
Quit. It stops the MATLAB. Who? It lists out the current set of variables from the workspace. Whose? It lists out the current variables, data type, dimension, and other necessary information related to it from the workspace. The other set of commands. CD. It denotes the change of directory. Delete. It deletes a file. DIR. It lists out all the file which is present in the current directory. Load. It loads all workspace variables from a specific file path. It displays the search path. PWD displays the current directory. Save.
It saves the workspace variables into a separate MAT file. Type, it displays the contents of a file. What, it lists all the variables in the current directory. And WKL read, it reads the WK1 spreadsheet file. The last topic that we are going to see in this current session is about the long assignments. By default, MATLAB displays numbers with four decimal points. So this is known as format short. So the default data type that is available with MATLAB is the short representation of the decimal values. If we want to have more precision, we need to use the format command. Okay. So the format long command displays 16 digits after decimal. The format bank command displays up to two digits after the decimal point.
Here we can see in the right hand side of the screen. We give format long and when we give say 22 by 7. 22 by 7 is pi which is basically 3.14 something. This 3.14 will be followed by 14 more digits which is basically 16 digits would be available after the decimal point. For format short, it is the default one. For format short, it is the default one. Four numerical values after the decimal point. And as far as format bank is concerned, it only rounds it up to two decimal places here.
Likewise, MATLAB also displays large numbers with exponential notation which is basically short E, long E and rat. Short E allows in displaying in exponential form with four decimal points plus the exponential factor plus the exponential factor and format rat gives the closest rational expression. Say, for example, we are doing two decimal number multiplication. It gives the rational expression output which is in the form of a fractional value as its output for format rat. So, this we can now quickly move into MATLAB to see how it works. So, basically 22 by 7, it gives 3.1429. So, as we had seen here, it displays numbers up to four decimal places.
So, the next one we are going for format long. Okay. Format long is given. Now, the same 22 by 7, it displays up to 16 digits after the decimal point. Likewise, we are going for format bank. Same 22 divided by 7. Same 22 divided by 7. It gives only up to two decimal places which is 3.14. Now, the next one is about format short E, long E and format rat which is rational values. So, I will give something like format long E. Now, again 22 divided by 7. It gives 3.14 which will be followed by 13 more digits which is basically 15 digits after my decimal value plus the exponential factor.
And similarly, format short E. I give something like this. Same 22 by 7. It gives 3.1429 E plus 0, 0. It truncates itself to exponential form. It truncates itself to four decimal places. Likewise, we will check about format rat works. So, format rat we are giving something like 5.679 into 3.45. So, this is the closest fractional value that comes when 5.679 into 3.45 gets multiplied. Now, since the start of this tutorial, we have only been seeing about opening the MATLAB and executing all the commands in the command window.
Now, we can see how to open a MATLAB script, mscript and then start typing. Okay. So, when you open in the home tab of my MATLAB home screen, there is something called as new script or blank script which is at the top left corner. Just click on the new script. Editor basically opens up. So, this is how the editor window looks like where we can start typing and defining things. So, the ideal programming practice which one needs to have is always what kind of a program it is, for what purpose this application or the program that we are going to write.
So, that should be specified at the top as a comment. So, if we are going to directly type it basic MATLAB tutorial, it comes in the colored form which means that the MATLAB tool is recognizing this as an executable command. No, this is not an executable command. So, what we have to do is that we will now put a percentage symbol such that the total color of it changes to green, which means that anything which comes after this percentage here is a comment. So, this is the basic MATLAB. So, this is the basic MATLAB tutorial comment here. The next set of statements would be to ideally have CLC and clear all. So, it cleans my command window and it clears my workspace.
So, this is the basic programming practice which one needs to have here. And now, let's start executing some of the basic addition of two numbers or multiplication of two numbers. We will define them in the workspace and then we can see. So, example on variable assignment A is equal to five. You can also comment at this end. Like after it has been declared, you can type something like declare A and B is equal to six. So, let's declare B and that will be followed by C is equal to A into B, which is basically multiply. So, once this is done, you can save it. The default path where it asks to be saved is your current working directory.
You can give any name here. You can give any name here. I am just giving it as exercise one. And it saves with the suffix as one second. Okay. It saves with an extension of dot M. And now, we can start executing. I think it is saying that the name is not valid. So, I can quickly rename it to EX1. EX1. So, now the name has been changed. So, there was an error message that was displayed. So, the error message is now gone. So, once this is saved all, you can directly click on the run button. It will run this MATLAB script.
And yes, previously, there were some commands in my command window. In my workspace, a variable A and S was loaded. Now, the workspace is cleared. Only variables A, B and C are there after the execution. And surprisingly, in command window, A is equal to 5 has come as a return string. That is because here we have deliberately not entered a semicolon, which means end of statement. And now, if I wish to enter semicolon, which is end of statement, and then run again. Yes, it is gone. The same set of values are reloaded again.
So, this is how we open a basic script. And we execute, we type a program and we execute the commands in this. So, that comes to the end of the first session on the syntax basics, variables, commands, and long assignments. So, let's click on theBeCAN Atlant kit. So, let's Ar Серг Desde which we created the same sets and 4-1
Part 2
Today's session on MATLAB scripting covers vectors, matrices, and data types. So MATLAB creating vectors. So vectors is a one-dimensional array of numbers. There are two types of vectors which can be created, which is row vector and column vector. Row vectors are created by enclosing the set of numbers in a square bracket using a space or comma to delimit these elements. As you can see in this screenshot which is taken from the command window, AA is the variable name and AA is defined as AA equal to inside the square brackets 1 followed by space 2 followed by space 3.
So this is basically a row vector or row-wise representation. Likewise, BB is declared as 4, 5, and 6. And now CC is equal to AA plus BB. Here we can also see that both AA and BB vectors are row vectors as we have already said and they follow similar dimension and size. And CC has the similar properties of AA. And CC has the similar properties of AA. That is, it inherits the properties of AA and BB here, which means like the same size and same dimensions as AA and BB. So this is how vectors are represented.
That was row vectors definition. Likewise, column vectors. So one column to another column, it is separated by using a semicolon here. In the row vectors, as we had seen in the previous slide, it was a space to delimit between two different numbers in a given row. So for numbers in a semicolon. For numbers in a semicolon, for numbers in a semicolon, to delimit them, we use a semicolon. So that is one semicolon space two semicolon space three. That's how we give it for column vector representation.
So similarly, DD, EE is declared. DD is one, two, three, column wise. EE is four, five, six, column wise. And FF is DD plus EE. Again, as we have seen the same for row vectors, column vectors also has the properties as same as its parent. That is FF has the properties similar to its parent of DD and EE in terms of dimension and the size. So let's continue with the other topics of MATLAB vectors about like accessing the elements. So before going into this topic, let's quickly have a hands-on demo with the MATLAB tool like how we have given.
So before starting any topic, let's clear the workspace and clear the command window. Now we will start about with declaring vectors as we have screenshots. So let's give something like AS one, two, three. Okay. And B is equal to four, five and six. Now C is equal to A plus B, which means it is summation of A and B. And it is again a one row, three column row vector that comes as output. Similarly, when we go for column vector representation, it is one, two, three, which is here separated by a semicolon. Now E is equal to four, five, six.
F is equal to D plus E. This is how the column vectors are represented in MATLAB. Now let's go back to the other topic, accessing the elements. So if we wish to access a particular element in a given vector or a matrix, it is again possible by specifying which position that it belongs to. Okay. Let's take this example screenshot, which is shown here. So here A is defined as a one row, five column element, which is basically one cross five. And if you wish to know what is the value of the fourth element, you can give us A of four, where here A followed by curly braces, and you give four in the between. Likewise, if you wish to access the third element, you give it as A of three, second element, A of two, and so on.
In the same way, if you wish to alter the value of a particular element in an array, that can also be seen. Here A of two is already four, as we know. But when you give A of two is equal to 88, then your A becomes 188, 5, 7, and 9. So the four value is now replaced by value 88. So that again, quickly, we will see in our MATLAB tool of how we are going to access the vector elements. So here for accessing vector elements, I am giving it as AB. AB is equal to 1, 4, 5, 7, 9.
So which is a one cross five, AB of three, when I give AB of three, returns a value of five, which is basically the third element of my one cross five vector. Likewise, if I wish to change any one specific value, that is AB of two is equal to 20. Now AB returns 1, 25, 7, 9. Basically, four is replaced by 20. Also, if we wish to know the size of the given vector, we can simply type size of followed by the vector name. So it returns me 1 and 5, which is basically one row and five column.
Likewise, this is a row vector. So it is easy. If you wish to know for a column vector, similarly, you can find it out like this. OK, it is variable F. Sorry, it is not FF. So I am again giving it as size F. So F is already declared as 579. This comes as three rows and one column, basically. So the next topic that we would like to see is the size of the vectors we have seen. And simple mathematical operations like multiplication can also be applied.
So now I am giving the value of B is equal to say 10. So what becomes is C is equal to A into B. It gives A's value is already 1, 2 and 3. Now C value becomes 10, 20 and 30. So this is the basic vector manipulation. Likewise, for matrices vectors, as we had already seen, they are one dimensional. Now let's go for matrices. A matrix is a two dimensional array of numbers. In MATLAB, a matrix is created by entering each row as sequence of elements, which will be separated by space or comma.
And then it will be completed by a semicolon. That is that specific row. When we have completed filling out the values, it will be stopped with a semicolon. And the next row data will be followed after the semicolon. If you can see in this screenshot image, matrix is a three dimensional matrix. It's a three cross three matrix where one, two and three semicolon four, five, six, semicolon seven, eight, nine, which means the first row values are one, two, three. The next row values are four, five, six, separated by semicolon.
And that is followed by seven, eight, nine. Like vectors in matrix two, we can access the particular position of a matrix. For example, if you wish to access the second row and the third column element, it can be accessed as matrix of two comma three is equal to six. Likewise, vectors, the matrix dimension can also be known by using the size of command. So when you're giving something as size of matrix, it is going to return me with string three cross three. Here, one interesting thing that we can note is that there is an error message screenshot, which comes here, which basically says that index exceeds matrix dimension.
We will see how to replicate this error message screenshot in first place. So what I am doing is in first place, I am clearing my command window. Then mat one, I'm giving it as one, two, three, end of my first row, four, five, six, six, end of my second row, seven, eight, nine, end of my third row. It is a three cross three matrix. So I give enter, mat one is loaded into my workspace. Now, if I wish to access a particular element, say second row and third column, as we have seen in our presentation slide, we can write it as mat.
One of two comma three. So I give enter. It returns a value six because mat one again is one, two, three, four, five, six, seven, eight, nine, which is the first row, second row and third row. So which is second row, third column six. That is the string that is returned here. If you wish to access specific row elements, you can use a colon notation here. For example, mat one of colon comma one. It lists out all the first column elements. So basically, you're using a colon operator comma followed by which column you wish to access. That gives you the first set of in a row. So then that will be followed by mat one of two comma colon.
So giving it like this will actually list out my second row. So this is what we have said. Accessing the specific row elements and the column elements of a matrix is possible here by using a colon operator. Now, similar to vectors, if we wish to replace the particular value in a matrix, that also is possible. Now, say I'm giving mat one of one comma one is equal to 11. Mat one returns as 11, two, three, four, five, six, seven, eight, nine, which the value of one is replaced with 11.
Likewise, we can build matrices with zeros and ones as well. Okay, a two dimensional matrix or a three dimensional matrix, which is filled completely with zeros and ones. Yes, we can do it here. And also in the presentation, we had seen about a topic of index exceeding matrix dimension. So here the size of mat one is three cross three, which means three rows and three columns. You can access the first row, first column, first row, second column, first row, third column, second row, first, second row, second, second row, third, third row, first, third row, second, and third row, third.
When you're trying to access anything beyond with higher value of row, third row. It returns an error. So what I am trying to do here is I am giving math off 2 comma five. OK index in position to exceeds a boundary must not exceed three. as we have already discussed here the size of the matrix is 3 cross 3 so the second position index value must not exceed 3 so maximum you can give is 2 comma 3 or the topmost value that you can give for both index positions that is row comma column is only 3 and not more than that if you're giving any other higher value here it will return with this following error message so this is about the index exceeds matrix dimension error which one needs to be aware of so this is about the specific row and column access that we have already seen accessing a specific row or column in a matrix is possible using a colon and a comma operator to access the row it is something matrix name of colon comma row number so to access a column it is matrix name of column number comma colon so that we have given so like vectors the replacing the specific element in a matrix again that we have seen a matrix which is filled with zeros and ones that can be seen here zeros of 2 comma 2 will return a two-dimensional matrix which is completely filled with zeros ones of 2 comma 2 returns a one two-dimensional matrix filled with value 2 the first element of a matrix is always the first row first column second row second column and they are replaced by colon as we have already seen using a simple colon operator we can list out all the elements in the form of a matrix matrix if we understand the number of elements that is present for a three cross three matrix there are nine elements which is available so the representation can be matrix of colon that is it does not have any row or column information it simply has a colon where we are initially not specifying any size here you give it as one is to one is to nine which means increment of one value from one to nine must be here so this for all the same dimensional matrices like a one cross one or a two cross two or a three cross three this way of representation is possible that is matrix of colon is equal to one is to one is to nine okay so similar to the vectors we have seen when you try to access a value that is out of boundary it throws an error the same example is again shown here matrix two is basically a two row three column element which is one two three four five six when you specify something like matrix two of colon is equal to one is to one is to one is to nine it throws a return string saying that the number of elements in a and b must be same so when you return as matrix two of colon is equal to one is to one is to six it identifies that the total number of elements which is present in this matrix must be same and it throws this value at the output so the next major topic that we are going to move into is the matlab data types matlab provides a 15 fundamental data types every data type stores the data in the form of a matrix or an array the size of this matrix or an array is a minimum by zero cross zero and this can grow up to a matrix or a matrix or array of any size so the basic and the common data types which is used in matlab is listed here integer eight uint eight integer 16 uint 16 integer 32 uint 32 integer 64 and uint 64 integer eight is an eight bit signed integer where its range of value is from minus 128 to plus 127 uint 8 is basically an 8 bit unsigned integer whose value ranges between 0 to 255 which is basically 2 power 8 is 256 minus 1 of that is 0 to 255 here likewise for int 16 uint 16 int 32 uint 32 and int 34 uint 34 the range of it is represented here in the range column and the other different data types and some of the operations that is very common with matlab data types here are the logical character cell array etc but before that there are two other numerical data types which we must be aware of one is single and the other is double single is double is double precision numerical data double is double precision numerical data any value that is defined in your matlab workspace will carry a data type of type double now going to logical logical values of one or zero is here which represents true or false character is character data where the strings are stored as vector of characters cell array is array of index the cell each capable of storing an array of different data type and dimension structure they are c like structure each structure having name field capable of storing an array of a different dimension and data type function in handle which is pointer to a function user classes are objects constructed from a user defined class and java classes are objects defined from java class the most important data types which one needs to be aware of is the int 8 uint 8 int 16 uint 16 int 32 uint 32 in 64 uint 64 and then that is followed by single and double apart from the different data types of matlab we also need to have an idea of the different data type conversions which one does so these are some of the following commands for a data type conversion int to str converts integer to string conversion mat to str matrix to string conversion num to str number to string conversion str to double string to double precision value str to number uh string to number conversion cell str creates a cell array of strings which is taken from a different Characters. MAT to cell, it converts an array of cell array with potentially different sizes. Numeric to cell, it converts array to cell array with potentially same size cells. And struct to cell is convert structure to a cell array. Likewise, MATLAB also provides a determination of different data types.
So it provides various functions for identifying what data type, if it is an integer or if it is a float, if it is a character, if it is a cell, STR, if it is a cell. What you wish to search for a variable, it searches and it returns value of either 0 or 1, saying whether it is true condition or false condition. So let's have a quick hands-on session on the MATLAB data types and data type conversions, etc. So before that, let me clean my command window.
Then I am going to clear my workspace as well. So I am giving something as STR equal to hello, which is a string. OK. N is equal to 1, 2, 3, 4. N type is equal to class of N. So if you wish to know what is this class of N, you type it like this. The default data type of MATLAB is double. So it is stored into double here. Now let me give D is equal to double of N. Now D is assigned to the value of 1, 2, 3, and 4 of data type double. Likewise, we are giving something as UN is equal to Uint8 of 789.50.
Now it returns a data type Uint8 for the variable UN and it returns the maximum value of 255 because for Uint8 data type, it returns only the value to 0 to 255. So since 789.50 here is a value which is much higher than 255, the max value of 255 is returned back here. Likewise, let's check for UN type is equal to the class of UN. So it throws that the data type of it is Uint8, which is as shown here. It is defined here as Uint8 and the same value is returned in the form of a string here.
Now in order to know whether the given one is an integer or not, we use isinteger command and if it returns 1, it means that it is integer value. Type is equal to isinteger of UN. It returns a logical expression for value 1, which means that it is a Uint8, which is an integer value. So it returns 1 here. Now N is a double. So let's give the same command and see what it gives for N is equal to isinteger of N. It returns a logical output 0, which means that N is a double and it is not an integer.
So these are the commands, which one gets to know from the determination of data type is integer is it will determine if the input is in integer array is character. It will determine whether the item is a character array and is cell SDR is character likewise work on similar levels. So the next topic that we have here to be covered is the colon notation. So during the matrix manipulation or accessing the specific rows or columns of a matrix, we used the colon notation. That is colon followed by a comma followed by a row number or like a row number followed by a comma and followed by a colon.
Something like that is what we have used with the MATLAB colon here. So colon is one of the most prominent data parsing operators in MATLAB. It is used to convert subscribe arrays and specify the iteration. For example, if one is to 10 is given in my command window, it lists out incremental values between one to 10. So 10 values of one, two, three, four, five, six, seven, eight, nine, 10 will be stored in my workspace. Default one is the iteration increment for decrement cases. It looks like the example shown below.
That is 100 is to minus five is to 50, which means from 100 negative values of minus five, every minus five value, it must be separated and it must come until 50. So when you run 100 is to minus five is to 50. It is 195, 90, 85, 80 until 50. It is mentioned here. So these are some of the different colon notations that we follow in the MATLAB to select the specific rows, columns or specific array of elements. So let's quickly move to the MATLAB tool to have a quick understanding of how the colon notation works.
So input one is equal to one is to 10, right? So when you give something like this, one row, 10 columns would be represented in this way for arrays and matrices. So the next one is input two is equal to 100 is to minus five is to 50. So in decrement of fives, it lists out every value from 100 to 50. So that is pretty much about the topics related to the vectors, matrices and data types.
Part 3
In today's session, we will be covering about the MATLAB operators and decision-making statements. MATLAB operators, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. So in simple terms, an arithmetic operator is an addition symbol or a subtraction symbol. Likewise, any AND gate or OR gate, whatever that we use, comes under a logical operators. Likewise, any intersect commands that we use, they come under bitwise operators. So this is what the logical or bitwise operations basically do. MATLAB is designated to operate primarily on matrices and arrays.
Therefore, the operations in MATLAB work for both scalar and non-scalar values. Here, the non-scalar values are basically vectors or matrices. So the different types of elementary operators are arithmetic, relational, logical, bitwise, and set operators. In detail about each types of operators, we will be having a look in this presentation. And also, what are the different types that is present? We will be having a walkthrough as seen here about different operators we will see. And also, at the tool level, we will have a demonstration on how they work.
So MATLAB allows two different types of arithmetic operations, which is basically matrix arithmetic operators and array-related arithmetic operations. Matrix arithmetic operations are same as defined in linear algebra. And the array operations here for both one-dimensional and multidimensional are executed element-wise and element-by-element. The matrix operators and array operators usually are differentiated by a period, which is a dot symbol here. However, as the addition and subtraction operation is the same for matrices and the operator is same for both the cases. So these are the different operator symbols, which can be seen on the left side of the column, which is plus, minus, multiplication.
And there is something called as array multiplication. Also, slash array right division, backslash array left division. So we will see what each does first in theory. Then we can move into the tool to have a hands-on demonstration on how it works. Plus is addition or unary plus. We know how it is. Just the prerequisites are A and B must have the same size unless one is scalar. Scalar can be added to a matrix of any size. Minus is a subtraction. A minus B subtracts the value of B from A.
A and B must have the same size unless one is scalar. Array multiplication is like multiplies C is equal to A star B is the linear algebraic product of A and B. For non-scalar values of A and B, the number of columns of A must be equal to the number of rows of B. Right. And then array multiplication. Array multiplication is written or is represented at the code level as A dot star B, which is an element by element product multiplication. Here, both A and B must be of same size unless one of them is a scalar value.
Likewise, slash is the matrix right division, which is B divided by A or that is the equivalent of B multiplied by inverse of A. So, array right division is A dot slash B is the matrix with element A of I comma J slash B of I comma J. So, array right division is applicable only when you have both the A vector and B vector or as an A matrix or B matrix must be of same size unless and until one of them is scalar. Backslash or matrix left division.
Backslash or matrix left division is like if A is a square matrix, then A slash B is like inverse of A into B. If A is an n by n matrix and B is a column vector or a matrix with several columns, then X is equal to A slash B. So, array left division, array left division is like A dot slash B. To know more about them, we will have a detailed analysis with the MATLAB. Now, let's move on to the MATLAB tool. So, in the MATLAB tool, this is the command window which is like cleared off all the other commands.
So, now let's declare two vectors for arithmetic operation and its manipulation. So, X is equal to 1, 2, 3. So, this is basically three rows and one column. Likewise, Y is equal to 4, 5, 6. Here, let's note that X and Y are of same dimension. Now, let's look at the different arithmetic operators, how they perform. I am giving something as Z0. Z0 is equal to X plus Y. Yes. Z1 is equal to Y minus X. Scalar addition, how it is going to work. So, basically, take either X or Y and add it with a numerical value.
Not Y basically, but add it with some other numerical value. This is X plus 2. So, Z2 comes. Likewise, Z3 is equal to scalar subtraction, which is basically Y minus 2. Then, array multiplication Z4 is equal to X into Y. We can see that there is an error message that pops up here saying that incorrect dimension of matrix multiplication. The reason why it comes is both are column. That is, both have three columns and one row. And your normal multiplication is not possible here. One, what we can do is we can take a transpose of X and we can give it to some other variable, say Z.
And then, we can give Z4 is equal to Z into Y. Something like that. So, what we are going to do here is Z is equal to X transpose. The operator for transpose here is my apostrophe. And now, it comes as one row, three column. It is transpose. Now, I am giving Z4 is equal to Z into Y. Yes. The scalar multiplication, it works as Z5 is equal to 2 into X. Yes. And same dimension array multiplication. We have already seen that the same dimension array multiplication will happen if we are using a dot operator.
In previous case, when we tried to do the multiplication, it was not possible. It said that incorrect matrix dimensions are there. And to perform this element-wise multiplication, we have to use the dot operator followed by the multiplication symbol. So, what we are doing here is Z6 is equal to X dot star Y. So, this is the element-wise multiplication here, right? So, the next one we are going to have a look at is the array division. So, Z7 is equal to Y divided by X. Works fine.
Same dimension array division is Z8 is equal to Y dot slash X. This returns for only the first column. So, here it is to be noted that both this Y and X are three rows and one columns. And hence, when they are divided, the final output will be three cross three, where the first and the second columns that is being taken into consideration are filled with zero values. Likewise, for element-wise, it retains the same dimension as both the numbers that we are going to use here, that is X and Y.
So, Z8 will have the same dimensions as X and Y since they are element-wise division. Now, let's go for the backslash, which is the left division of it. So, this would be the last set of examples that we are having a look at with array division. So, let's go for X backslash Y gives an output and Z10 is X dot backslash Y, which is element-wise here. And it retains the same set of values. Now let's go back to MATLAB and see what they are. So the backslash or matrix left division is if A is a square matrix then A slash B is same as invert of A into B and if A is an n cross n matrix and B is a column vector or a matrix with several such columns then A slash B is something like A into X is equal to B.
That's the same that has happened here for matrix left division. Array power, matrix transpose and array transpose. The array transpose we have already seen in one of the examples about the matrix power and the array power multiplication we will have a short look at it. So it's basically a matrix power is X to the power of P where the P value will be a scalar value. Here if P is an integer value the power is computed by repeated squaring of it. Array power is something like A dot this key symbol here followed by the B element where A and B again will be of same size where the same product operator like how we have used for division and the others is being considered.
So we can quickly move here. So Z11 is equal to X dot Y. Yeah. So the next operator that we are going to have a look at is the relational operators. Relational operators again like arithmetic operators can also work on both scalar and non-scalar data. Relational operators for array perform element by element comparison between two arrays and returns a logical array of same size. It returns a logical expression of one or two when the relational comparison is true or it returns a value of zero or false when they are not the same.
So the basic operators that we have are less than or equal to greater than greater than greater than or equal to equal to and not equal to likewise instead of using the symbols we can also use the following commands. equation that we have a ruler of the celebrations. The 역تيuri variants for arrays of equal to the most experimental type is equal to and it becomes the same table as above and using the symbols that we have to 연준 let us look at them and as theredit It checks for array of equality even for NAN values if they are present in the array.
So we'll have a quick roundup on relational operators in MATLAB. So let's CLC and clear all. Right. So capital X and defining is equal to we are going to have a three cross three matrix with one, two, three, four, five, six, seven, eight, nine. Right. Y is equal to five into ones of three cross three. So what does my Y return here? It's a three dimensional matrix of value five. It is basically ones of three cross three gives you one, one, one completely filled on all the nine elements, which is multiplied by five.
So this is the value that comes here. So let's do a quick comparison of less than is than or equal to greater than, greater than or equal to, equal to and not equal to cases. So Z1 is equal to X less than Y. So these are the values where X is less than Y, that is one, two, three and until four. So since this is just equal to from the fifth element, it returns a value of zero. The first four elements here, it returns a value of one.
And the same command can be used as Z2 is equal to LT of X comma Y. But the most common coding practice is using these operator symbols only. Yes. Now, less than or equal to Z3 is equal to X less than or equal to Y. Z4 is equal to LE of X comma Y. Z5 is equal to X greater than Y. Z6 is equal to Z6 is equal to G T of X comma Y. Likewise, for greater than or equal to Z7 is equal to X greater than or equal to Y.
Z8 is equal to Z8 is equal to G E of X comma Y. Z9 is equal to X equal to Y. and z10 is equal to eq of x comma y so we have seen equal to and the last one is not equal to operator which is z11 equal to x not equal to y and z12 is equal to ne of x comma y so this is how it is all the corresponding values since z1 to z12 they are all logical expressions they are loaded into workspace as three cross three logical where they are all logical arrays so they are loaded as three cross three logical your inputs x and y are basically your numerical array data that we have used here so the next operator that we are going to discuss is the logical operator matlab offers two types of logical operators and functions they are element wise and short circuit element wise operators operate element by element on logical arrays the symbols that as like for and operator not operator and or operators are the logical array operators here short circuit logical operators allow short circuit on logical operations which is basically short-circuited and that is double ambison and double or symbols will be used so the list of operators and the symbols are like and or not and then short-circuited and and short-circuited not which we can see here so before moving into the next one which is the bitwise operators we will quickly have in matlab about what logical operators can uh do here so let's clear matlab i'm going to give something as a is equal to 5 and b is equal to 20 let's go for if a and if a and if a and b display line one condition is true end if a or b display of line two condition is true end the loop so in detail about what the for loop does here is like we will uh discuss it a little later but right now it's all about uh the manipulation of uh the operators the logical operators and yes now let's change the values of a and b a i'm going to give it as a 0 b i'm going to give it s 10 if a and b is there Display of condition is true. Else, display of condition is false. So here, since A and B are not same, it displays condition is false, right?
So here, we are using the operator, but we have only given one condition. Hence, it just displays that condition. But here, we are using an if statement effectively to compare between the operators, the values A and B we have given. If A and B, we are giving it as a short-circuited and operator here, it says that the condition is true. Else, the condition is false. Likewise, we give if of A or B, display of condition is true. Else, display of condition is false. And end the statement. Since it is an or operand, it considers either of the inputs, either A or B.
If any one of them is true, the output will be true. That's what is meant here. Whereas, as far as the short-circuited and operand is concerned, you need to have both A and to be equal to true. Only if A and B are exclusively true, the output condition will be true. Since they are not true here, it is false. But when you are using an or operand, any one of the conditions can be satisfied. And that is what can be seen here. So the next example that we will be looking at the MATLAB is like the bitwise operators. So let me go back to the slides.
Bitwise operators work on bits and perform bit-by-bit operation. The truth table can be seen here. And bitwise and bit complement, bit get, bit op, bit r, bit set, bit shift, bit xr, all can be seen here. These are the commands and their corresponding descriptions can be seen here. And a quick look at what the bitwise operators can do. So let me declare a couple of, let me take two arrays. So I'll clear my workspace before that. Yes. So I'm giving A is equal to 0, 1, which is basically a 2 cross 2 matrix.
And B is equal to the same set of values, 0, 0, the same dimensional values, 1, 1, that is given. So let's do a bitwise and C1 is equal to bit and of A, B. If you can see here with higher versions of MATLAB as and when we type, the MATLAB starts showing what could be the possible cases or conditions that is there. Here you can give it a split and of A, B or you can also define an assumed type operator and that handle operator you can specify and more can be seen.
If you wish to know more about what they are and what it does, we can always go to the help document here, click it and learn more about them. So the bitwise or operator, we will see C2 is equal to bit or of A, B and C3 is equal to bit XOR operator of A, B. So if you can see the values of C1 and C2, here it compares this 0 with this 0, this 1 with this 0, this 0 with this 1 and this value will be compared with this value.
So when such comparison happens for C1, since it is an AND operand, 1, 1 only will be true. It returns the 2 cross 2 element will be 1 true for bit or except for the 1 cross 1 element, the others will be true. And for bit XOR, which is exclusively R, either one of the two cases can be true, the output will be true. The similar output is expected and it is shown here at the C3 level. So this is about the bitwise operators. So the next set of operators that we are going to have a look at is about the set operators, which is intersect of A, B, intersect A, B of rows, is member of A, B and is member of A, B, rows.
So MATLAB provides various functions for set operations like union, intersection and testing for set membership, etc. So intersect of A, B basically performs set operation for two arrays, A and B. The value will be returned in a sorting order. Intersect of A, B, rows will set intersection will do set intersection of two arrays and it returns the value which is common to both A and B. This member of A, B, it returns the array of same size as A, which contains the true value when the elements of A is found in B.
Else, it returns the false value. Basically, it searches for the value of A and B and if they are available, it returns true condition. Else, it returns false conditions here. And likewise, we have is sorted set difference, set difference of A, B, set difference of A, B, row. So what we will do is like we will again switch back to MATLAB and we will have a look at some of the basic set operations that we can do. So I'll quickly clear the workspace. CLC is done.
So A is equal to A is equal to, we will give say 7, 13, 8, some set of numbers for this array. B is equal to 259 and okay, let's put the brackets, semicolon. Now, C1 is equal to intersect of A, B. It returns an empty vector since A is not found in B. So C2 is equal to union of A, B. It returns the mutually exclusive values by combining them in the order of lowest to highest and it returns them. So basically, in intersect, the value of A cannot be found in B.
So it returned an empty array. For union, it combines both A and B and in the order of increasing, it says. So a one row, three column becomes a one row, six column element here. So the same way, C3, let me give for is sorted basically. So when I'm giving it as is sorted of A, it returns 0 if A is not in sorted order. Yes, A is not in a sorted order. So it returns this way. Likewise, I'll give it for B. It should return 1 here because B is in sorted order.
If you give B or if you give C2, it returns correctly. Yes, it returns a logical value 1 since the inputs are in increasing order. So this is for set operations. So the basic operators we have covered, which is arithmetic, relational, logical, bitwise and set operators with some hands-on experience at the tool level. So the next set of topics that we would be having a look at is about the decision-making statements in MATLAB. The decision-making statements will require the programmer to choose between one or more conditions based on the input requirements.
So there would be conditions or cases where the developer or the programmer would have to choose one among the given options, either this or that, something like that. So to use that, MATLAB has some set of decision-making statements. They are like if-else statement, if-else-if-else statement, nested-if switch and nested-switch statements. So as far as the C programming is concerned, how your if operators are there, in similar way, it is there in this case as well. So this is if statement. An if statement consists of an if command, which is followed by a Boolean expression, and one or more statements followed by the end statement.
So what it usually does is that it checks. For the condition, if the condition is true, it executes this part. Else, it executes this part. So if condition is basically this. So if else statement, it is one second. We can quickly edit the same. Yeah. So in if else condition, it is an if statement will be followed by an else statement here. So if the condition is tested, if it is true, the true case works out. Else, there is a false code which is there. The false part of the code works out and it goes. Just to iron out the confusion, in the previous case, it checks for the true. The true part of code is executed. Else, the false case, it directly goes. Here, it executes for the true case. If in case it is true, else it goes to the false case and it executes.
If else if else if is basically in cases where you have multiple options to choose where one or more optional else if statements can be added to the given condition. And as far as nested if statement goes, where there is one if else statement inside another if else statement that stands for the nested if statement. So we can quickly go to MATLAB and we can quickly see how it is done. So for the if else if statements, it is better. We open a new MATLAB script.
We have it here. We have it here. We will give it CLC. Clear all. And then we can start executing. So basically, it is something like with greater than or equal to or with less than or equal to with these operators and operands. We can manipulate and we can manipulate and we can see how the if statement or if else statement can be given here. OK, so I am defining something as a is equal to 10. All right. So the first one is my if statement.
If a is less than 15 printf a is less than 20. OK, we are closing it with end. So as you can see here, if a is less than 15, f printf a is less than 20. We have given a value is defined as 10. So it just checks for one condition and then it breaks out to the end statement. So how if else is different from this? We can just see if else we can just see if else we can give something as B is equal to 100.
If B is less than 20, f printf of D is less than 20. Else f printf of D is not less than 20. All right. And now we can break the statement with end. Right. So we will try to save this script as if if else and we will try to run it. It tells that A is OK. This is a mistake from my end. I have given it as 15 here. I have typed here as 20. So I'll correct it. And then I will rerun my thing.
It says that A is less than 15 and this is B is not less than 20. So it takes the else part here and it returns. Whereas here it's only one condition is checked. That condition is true. And in those cases, you can directly use an if end statement. So this is how it is going to be the same way for the other operators like nested if or switch cases or others as well. So now quickly we can move into the other cases of a switch statement.
A switch statement is again where a switch block conditionally executes one set of statements from several choices. So each choice would be covered by a case statement here. So how a switch statement basically works is like, say, you have to evaluate the grade. OK, you have the grade value will be given grade value will be something like a user input. So switch of grade you can give and you can have multiple cases, say, case A, case B, case C, case D and case E. Case A can be excellent. Case B can be very good. Case C can be good.
Case D can be just pass and case F can be fail or any other value that is given can be given as please enter the correct grade or something. So when your grade value is specified as a user input, if it is A means you have to print the output as excellent, if it is B means you can print it as very good and likewise it works. So this is how in detail the switch statement basically works where you can choose from one of the multiple choices that is available.
And each choice is separated or distinguished by the use of a case statement. The switch block tests each case until one of the cases is true. A case is true when in case of numbers, it is like case expression, comma switch expression. For strings, it is string compare. Likewise, it can be done. When a case is true, MATLAB executes the corresponding statement and it exits totally from the switch block. If none of the cases are correct, the otherwise statement is basically used. So this is about the operators and the decision statements in MATLAB.
Part 4
In this session we will be having a look at the strings and cell arrays. So what is a string? Creating a string character is quite simple in MATLAB and a string can be represented in MATLAB something like A is equal to Skill-Lync when it is written inside a single quote, the MATLAB recognizes it as a string. So quickly we'll move to MATLAB and see how it works. So what we can do is we can close CLC, clear all, yeah, and I'm giving X is equal to Skill-Lync. Now this value is recognized as a 1 into 10 character and it is stored inside a variable name X in the MATLAB workspace.
So the Skill-Lync is basically it's a string with 10 different characters which is stored in the MATLAB workspace and it must be represented like this in this form. So this is basically about how you define a string. Now MATLAB. Now MATLAB provides the access to multiple string related functions like combining them, parsing them, comparing them and manipulating them. So these are some of the string functions that are there with MATLAB that is blanks, cell str, character, is cell of str, is character, string join, string concatenate, string split, string replace, regex, regex pi, regular expression, replace, regex prep, etc., etc.
So, and also we have string compare, string compare, index, lower, upper, etc., etc. So one needs to have an idea of what they are and one needs to appropriately use them. Though we would not be able to show a demo of all right now, but we can see how the string manipulation happens now basically. So what I can do is like we have x and I'm defining something as string ASCII is equal to u int 8 of x. So each letter of this skill link S-K-I-L-L-L-Y-N-C has a numerical ASCII value to it basically.
So when you are using this u int 8 of x, it returns a 1 cross 10 row vector value like this as shown. Now when you want to take it back as a character, what you do is str back character is equal to, we will be using a character command. So like u int 8, u int 60, when you have an ASCII value, when you wish to convert it back to a string value, you will be using character of str underscore ASCII. So it returns back skill link.
Now let's go back. We'll see if we can make use of some of the commands. So one of them is strcat, which is a string concatenation, where string concatenation is basically, it's combining your strings. So we'll see how the string concatenation part here works and then we'll see how to join strings. So we have like one string that is defined. Now we need to define multiple strings and we need to see how to concatenate or to join them here. So that we can make use of it.
Probably not one, but two or three commands we will see. So I'm starting off with CLC and clear. So we are giving something as name is skill link. Type is, say we can give it as online courses and location, Chennai. All right, so now what we can do is let's create a cell array of the name profile and I'm giving something like name, comma, sorry, it is something like this. There's a small correction, I guess it is needed here. So it is, it's not quotes for the name since they are already there.
So it is basically at the first and the last we have to remove it and it is given exclusively for this values. So that is for the comma we have given it. So what we can do here is to make it a lot easier for people to understand. We'll separate them by a space. And now I give this. It throws me a return string as skill link, comma, online courses, comma, Chennai. So this is one way of, you have three different strings. We have joined them together in the form of, say, a cell array.
So we can see here we have combined the characters in them. So this is one. And the other way you can catenate the strings is like profile one is equal to string concatenation of, similarly we are doing it. So name, type, location. So, okay, there is an error message coming. We just need to see what it is, string cat of, all right. So we have here missed a comma operator, which basically is the difference. So if you can see here, there is no delimiters is what the error message that is saying.
So when you are trying to give string cat, it tells for name. Now the comma must start. That is how the string cat works is, let's give help str cat. So once we give help str cat, this is how it is defined. It concatenates the arrays of s1, s2, et cetera, et cetera, until s n. So what we have to do here is like we have to concatenate it as a comma operator needs to be given. So what we ideally do here is we have to give it as profile one is equal to str cat of s name, comma, type, comma, location.
So this is how the concatenation happens. Now it combines them into a single array and it has skill link, comma, online courses, comma, Chennai. So this was the exact error message that was there. Invalid expression, check for the missing multiplication operator or unbalanced delimiters. The delimiters here is basically these commas, which I am trying to highlight now. There was no these commas that was present. So hence it threw an error message there. So this is something that we need to take care of while writing a code for string concatenation, et cetera.
And the other one is we are trying to profile two is equal to character of name, type, location. Okay. Previously, when we did a string concatenation or when we defined it in this way for profile, it was all one dimensional. That is one row and multiple characters were there. A one cross 33 or something depending on the number of characters that were used. Here we are having it as separate columns. So here when you are giving it as character of name, type. Location, it all puts them under different, different column here. And now when we give another, say, we are trying to use now cell str function, it converts this character array into a character array of strings. See, as you can see here, when you're using the character command, it converts them into a character array.
We had previously seen in the older example as well about the use of character, how it converts that str ascii values back as character. It all stored them as a character array. Now the job is to convert this character array back into character cell array of strings, basically. So that will be done using the cell to str. So basically, they are now as a character array and they need to be converted as string values, which we need to be reminded of. So l cell is str of profile 2. So now profile, it lists out a 3 cross 1 character array. So previously, each of this, the S, K, I, L, L, everything were individual characters in that complete row.
So hence, the size of it was 3 cross 14. And here, now skill link together is taken as a separate profile. They are all combined together and they are taken as a separate profile. And that's why we use this cell str here. So cell str is basically for converting your character array into cell array or strings. So this is how it works. So this is how it works. We can also have a look at probably a string comparison example, which we can have it at last.
So str3 is equal to this is test. str4 is equal to this is text. So using an if logic, we will try to compare both the strings. So if of say str compare of string 3 comma string 4 s print f should be given as percentage s and percentage s are equal. So str3, which is str4. Okay, I think we have made a small mistake here. So there must be a comma operator here, which again is pointed out, I think the whole thing I'll try to put to a script for better understanding, it's too confusing here.
So what I can do is like we'll open a script. So what I can do is like we'll open a script and we will copy paste these contents. So in the case we have created some notes. So we have a string with a string to the next step and we will be able to the string part and then I'll go about it again. So we have two friends. So if we can do this thing that we can do this for a string comparison or the string comparison, so that we will mention it here, okay, that I'll quickly mention it here. So if string compare of str3 comma str4 s print f should be given as any of the string comparison.
off quickly completing and just copy pasting it else s printf off we have to put it as they are not equal and let's go for enter now when you try to run this just save it with sdrcmp yes this is one of the important things that you need to take care sdrcmp you can't save it with the same name because there is a matlab library and that library is already there in its path so you cannot do it so what we can do is we can rename the expression to sdrcomp so these are some of the things that one needs to take care of you cannot name the program or you cannot give the variable name the same name as your inbuilt library function so this one needs to take care of and then i've saved it and when we try to give a run it tells this is test and this is text are not equal so they are two different ones i'll quickly change it and then i'll try to run this is text and this is text are equal right so this is how it is for the strings and the string manipulation etc etc so we've basically seen about cell str and about the string comparison concatenation etc etc now we'll be moving into the matlab cell arrays so clearly in one of the examples we saw about how the strings are getting stored as an array and how cell str command as a whole was helpful in converting your multi-dimensional character values as an array your cell arrays basically operate like that cell arrays are very similar to negative regular arrays in that they are indexed list of data with a symbolic name if you can see this example here they use these flower brackets or the bucket braces these bucket braces are basically called as cell so they follow a cell like structure and when you are giving a name of one it is like jim joe jane janet for when when you finally type names in your command window it gives you a one row four column array and it stores them in each separate cell so this is like one cross one denotes jim one cross two denotes joe one cross three for jane and one cross four for janet so cell arrays are used when elements of differing data types must be stored in a similar array so this here is only shown for a string likewise for multiples see here if you can see mixed values mixed values of one is a string mixed values of two is a numerical value mixed values of three here corresponds to again a numerical array so uh you give mixed value of one is this mixed value of two is that mixed value of three is five two one if you wish i mean see mixed values of three here is again a numerical array which where this numerical array is being stored in a particular cell of a cell array now if you wish to access the particular variable value here like five two or one you have to give mixed values of three of one here off one again will be given as like in a curly braces and not the flower braces as we had seen with cell arrays so off one here denotes five of two here denotes two and so on so this is how it quickly works for cell arrays we can have a short demonstration of how cell arrays work so what i will do is like i have it here itself so what we can do is a of one comma one is equal to denoted inside the square brackets so uh not only callback we can also do like how in our matrices we do likewise we can also do our definitions array creation and cell array definitions can also be done here like how we play with our matrix and our vectors so a similar drill we need to just follow here we are taking a three cross three which will be put in a cell arrays one cross one so one two three four five six seven eight nine okay so i'm closing this cell array definition for one cross one now for a of one cross two i am going to give it inside flower bracket killing say for example and a a of two comma one we are defining it say we are using uh another uh imaginary value something like three plus two i a real and imaginary value that we are using here and a of two comma two again we let's use a colon operator and see whether uh we are able to define it to that level here uh basically so This minus pi is 2 pi divided by 10 is 2 pi. So basically from minus 3.14 to plus 3.14 where decrements of 10, it must be divided and it must be stored here.
So this is how like we are giving, for example, and let's see how A is here. So if you can go to A, you just type A. It's a 2 cross 2 cell array where the size of it is only mentioned here, which is 3 cross 3 double skill link. And this is a 3 plus 2i. And here it is again 1 cross 21. So this is how it is given. Cell disp option is there. Let's just see what cell display does. Now it lists out each and everything in detail. So when you're going to give A and give enter, it just lists out like this 2 cross 2 cell array, mostly the size.
And it is going to display it in this form. For any numerical expression, it just lists out the dimensions of it or the size of it. For that particular cell, it lists out its dimensions or size. When you're going for cell display of A, for each and every element of that cell array, it is going to list out in detail. The 3 cross 3 matrix is expanded. 3 plus 2i is here. Skill link is expanded and every single column of pi to 2 pi here is expanded and it is given here.
So this is how your cell arrays basically operate. So with this, we come to the end of the session on strings and cell arrays.