← Previous lessonFree lesson 2 of 3 · lesson 2 of 55Next lesson →

Unlock all 55 lessons of EV FundamentalsFull course ₹40,000 · paid

Price & syllabus
Free lesson · 44 min · no sign-up

MATLAB Programming: Conditions, Loops and Functions

A free lesson from Skill-Lync's EV Fundamentals course, on Grow by Skill-Lync · Last updated 15 Nov 2026 · Session: “Matlab programming practices”

What will you learn in this free MATLAB and Simulink lesson?

Uses a self-driving car example to explain why programs need decisions and repetition, then writes MATLAB if and switch-case statements, for and while loops (tracing a loop step by step), user-defined functions and plots of the results.

  • if, elseif and switch-case statements in MATLAB
  • for and while loops, and tracing when a loop exits
  • Writing and calling your own MATLAB functions
  • Plotting results in MATLAB

What comes after lesson 2?

The next free lesson is lesson 3, “Simulink Basics: Blocks, Signals and the Library Browser” (15 min). Later in Skill-Lync's EV Fundamentals course (paid): lesson 4, Simscape; lesson 5, Simulink Demo; lesson 6, Stateflow. The full course has 55 lessons (12 h 45 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.

In the last video, we have discussed about the MATLAB and what are the different windows which are there in the MATLAB and what is the purpose of each window we have learned. And we learn about the different syntax as well as the commands which are available in the MATLAB. Using those commands, how it is possible for us to create the programming. These things we have learned in the last video. In this video, we are going to learn about the condition statements and looping statement at first.

Let's see what is the purpose of this condition statement and the looping statements. At first, let us have a practical example. Now, you peoples might be knowing the automatically driving cars. So, automatically driving cars can be able to drive without the driver, right? So, without the driver, it is possible for the vehicle to move. Now, in such a kind of vehicle, what is going to happen means, it has to understand what is the speed at which it needs to travel and what are the different obstacles which are there.

What is the route it has to travel. So, by understanding all these things, what it has to do? It has to move forward. So, when it is going to understand and analyze the different values, it has to check the values of the pre-existing values. So, right now, what is the value of the speed? And now, after one or two seconds, what is the value which is being changed? So, that change can be understand by comparing. So, if you want to compare two different values, means you can use the conditional statements.

And if you want to compare all the values and if you want to check all the conditions like speed, GPS, and the different obstacles which is there on the road in a continuous manner, then you have to go for a continuous observation. So, for doing that continuous observation only, we are going for the loops. So, using the loops and conditional statements only, we can be able to create the program. And with the help of those programs only, it is possible for us to make the vehicle to run autonomously.

Now, let's see a simple demo. Now, let's understand the conditional statements with an example. Now, for understanding that conditional statement, we can't use the command window that easily because condition statement, we may need to check randomly. So, what I'm going to do, I'm going to click the new script and I'm opening the editor window. So, editor window have came. Okay. So, here, what I'm going to do is, I'm going to create the conditions variable. So, for the condition, I have to give a variable. So, I'm just creating a variable, grade.

Okay. So, grade is a thing which I'm going to give as a condition. And I'm going to use the switch. Okay. So, switch inside that, what is the variable which is need to be analyzed. Okay. So, followed by that, what I have to do is, I have to write the cases. So, switch is a thing which will get the value from the grade variable. Okay. Now, if the case is going to be a, if the case is going to be a, then I can able to print.

So, for printing, which is nothing but showing some commands or some values over the command windows. Okay. So, if you want to show a set of string or as a character over the command window, I have to use the fprintup. So, I'm just using that fprintup option. So, the fprintup option, you have to use like this. So, we can be able to give excellent. Okay. So, you can give the value excellent and put a semicolon plus enter and then you can type the next case. If the case is going to be B, then similarly, you can give fprintup in that it is good.

Okay. Then, I'm taking one more case, C. Okay. And, here I'm going to use the same thing, fprintup I'm going to use for showing what is the grade C is going to have. Okay. It is going to be average. Okay. And, if the case is going to be D, then I can be able to say it is going to be bad. Okay. Suppose, if the user is going to enter some values which is not there in this case, in that case, what I have to do, I have to use otherwise.

Okay. If the cases are not going to have the value which is there in the grade, I have to use the otherwise. So, I'm just using the otherwise and in the otherwise, I'm just using the fprintup. So, in the fprintup, value is incorrect. So, I can give the value which is being entered by the user is incorrect. Okay. So, after doing everything, what I have to do, I have to tell the program with this, the condition statement is getting over. So, for that, I'm giving end.

Okay. So, I'll repeat it again. So, I have created the variable grade and I have given the value of A. So, if the grade is going to be A, what is the value has to be printed. So, this is the place where we are going to give the input. Okay. So, let's run this program. What is going to happen? Let's observe. So, if you want to run the program, we have to save it. So, I'm just saving it with the switch. Okay. So, I'm just saving this program.

And while saving the program, please keep it in your mind. Don't leave any space for the program name. Okay. So, if you are going to leave space in the program name, it will show error. Okay. So, I'm just creating the program with the name of temp1. Okay. Without any space, I have created. And now, I'm going to run this program. So, if I'm running this program, just see the temp1 have run and it is showing the value excellent. So, suppose if I'm going to change the value of create to be B and I'm going to run it again, now the temp1 is showing us good instead of excellent, the second case, for the case B, the value it has to print is good, right?

So, it is showing the good. If I print, if I give value of A, it is showing the excellent. Suppose if I'm going to give value which is not available here, that is A, B, C, D. So, there are four cases which we have created. Suppose if I'm going to give E, E is not a grade which is there. So, in that case, let's see what is going to happen. See, the temp1, it is showing the value is incorrect. That is the reason why we have given the otherwise option.

So, if we give the otherwise option, what it will do? If the grades which is being given, which we have already programmed is not there, it will show this one only. So, even if I'm going to give any other alphabet other than A, B, C, D, it will show the value is incorrect. If I'm going to give any values which is already there, it will show the corresponding grade. See, for D, it is showing that, right? So, similarly, we can able to change the grades and we can able to check the values.

So, where we can able to use this thing means, suppose if I'm having N number of datas and I want to specifically pick the equivalent value of one data means, in that case, I can enter that data. And by entering that data, I can pick the corresponding value of that specific data using this switch case experiment. So, here I have used only four cases, but the cases can be N numbers. It can be 100 or it can be 1000, but creating the case will be difficult.

But once the cases are being created, then accessing it and running it, it will be easier one. Okay. This is how the condition statements used to work. So, this is the example which we have seen in the demonstration. Using the syntax switch, we can able to select any option or any input which is being given. Okay. So, whatever input which you are giving, based on that input, it will select the case. So, different cases will be there. If no cases is going to be matching, then it will go to the otherwise and it will get N.

So, this is one of the examples for the condition statement. Let's see what are the different condition statement and looping statement which are available in the MATLAB. Now, in the case of the condition statement, we are having one more condition statement called if-el statement. Using if-el statement, it is possible for us to check whether the condition is correct or not. If it is not correct, then it will go to the other. So, that is the reason why it is named as if-el statement. And we are having two more looping statement called for-loop and while-loop.

So, for-loop, what we used to do is that if we know how many times we are going to run the specific set of statements, means then we can go for the for-loop. So, if we know the number of iterations which the program has to perform, then we can use the for-loop. If we don't know how many iterations are going to be there, then we have to go for the while-loop. Okay, just in case, if I want to break the state. Hence, in between, if the condition is going to be exceeding a certain value means, then I can use the break.

And for all like condition statement as well as the looping statement, I have to compulsorily use the end in order to make the MATLAB to understand this is the end of that condition verification and it is the end of the looping statement. So this end is the operator which will terminate the loop as well as the condition checking. Both can be done using the end operator. Okay. Now let's see where we can use the for loop. I have given you one example earlier, right? So if an autonomous vehicle is going to be driven over the road, it need to monitor the parameters continuously.

So the parameters like external parameters, we can't predict. But the parameters like internal parameters like battery level. So the battery level is how much battery level is there at present. So if the battery level is going to be 100% right now, then the vehicle can be driven this much distance. So if I want to create a program using that one means, in that case, I know how many times the battery can be able to give the supply or how much time the battery is capable of giving the supply.

So in that case, I can use the for loop. But if we want to compare the unknown parameters like external parameters in the autonomous vehicle, like the road condition may vary or the weather condition may vary or the different obstacles which is passing over us may vary. So those things cannot be predicted. So in that case, I can use the while loop. So using the for loop and while loop, it is easy for us to create the program. So at first, let us understand what is the syntax which can be used for the for loop and while loop.

So in the case of the for loop, what we have to do is you have to use the for. So for is the syntax which is being used and followed by the for, we have to use the variable name. So using that variable name, we have to provide the start value and the end value of the variable. So which is the starting point of the variable and which is the ending point of the variable. And that can be given using a colon in between. So the colon indicates the two.

So from which value to which value. So using the colon, we can able to intimate the program to understand which is the starting point, which is the ending point. And in unfollowed by the for loop, we have to give the statement which is need to be executed. So followed by the statement, we have to make sure that we are providing the end. Because based on this end, program will understand this is the end of the loop. So that it can be able to go for the next iteration.

So it will check and it will run until the end value is going to reach from the start value. So it will start from the start value and it will run until the end value is going to be executed. Finished. And whereas in the case of the while, what we have to do is while the expression, the expression, when the expression, until the expression is getting failed, it will execute the statement. And here in the case of while loop also, we have to provide one in in order to make the program to understand this is the end of the while loop.

OK, let's see how it will work. Let's see an example for i is equal to 1 to n. So i is equal to 1 in the sense i here i is going to be a variable. And 1 is going to be the starting position and n, it may be 10 or it may be 100 or it may be 200, any values. So until then, this statement has to run in the continuous manner. Now, let's see the flowchart. Here, start is there and followed by the start, we are having the i equal to 1.

So i is being initialized as 1. Then later on, it is verifying whether the i is greater than n. OK, why it is verifying means every time whenever the loop is being executed, it will do automatically self-increement. OK, so it will verify whether i is greater than n. If i is not greater than n, then it will go and execute the statement. Then followed by that, it will do increment of the i value. So it will do the increment of the i value and it will go back again because here the n will come.

So it will go back. So suppose if the statement is going to reach the value, that is, the i value is going to reach the value of the n. In that case, n will not be greater than n. So if the condition is going to be true, then it will go to the n and the loop will terminate. Whereas in the case of the y loop, what we used to do is, we will go with the expression and we will simply execute. Until the expression is going to be wrong, the loop will continue again and again.

So see, it will start until we enter into the condition verification. So if the condition is going to be true, then it will execute the statement and it will continue the loop again and again. If the condition is going to be false, immediately it will exit the loop. So this is the, this is how the for loop and while loop will work. Let us have an example how this for loop and while loop will work. Now let's understand how we can able to use the for loop and while loop.

Okay. With a simple example, let us understand what we are going to do. So for the for loop, what I'm going to do, I'm going to use the for loop syntax, this general syntax. And in that, I'm going to print the value of i, how it is going to vary. Okay. Let's start with the for loop. So for loop, we have to use the for and followed by the for, we have to declare the variable. And that variable, I have to declare the starting point and ending point.

And the starting point and ending point has to be separated with the colon. So here I'm using the colon. So it indicates for i equal to 1 to 10. It may be 100 or 50, doesn't be a matter. But here I have used i is equal to 1 to 10. Okay. So for understanding simply, what I'm going to do, I'm going to display the value of i. Okay. So every time, whenever the for loop is going to execute, it will display the value of i over here.

Okay. And whenever I'm using the loop, what I have to do, I have to use the end statement. So I have given the end statement. And if I want to run the program, which is there in the editor, obviously what I need to do, I have to save it. Even if you're going to click the run button, automatically it will redirect you to save. Okay. So I'm just give the name for loop. So here I'm using a underscore instead of giving a space. If you're going to give underscore for the name, the MATLAB will agree.

Suppose if you're going to leave a space between the for and loop, it will not agree and it will not run the program. Okay. So I have saved it. See what is going to happen over here. The for loop is being run and it is displaying the value of the i. So whenever it is displaying the value of the i, at first initial time, the i value is going to be 1. So it is displaying that value of 1 over here. Then it is coming to the n.

But when it is going to come to the n, it will increase the i value by 1. So already it is 1. So 1 plus 1. Now it will become 2. So now it will go over here. It will check whether the i value reached the nth position. Nth position is going to be 10, right? So it will check whether 2 is equal to 10. It is not equal to 10. So it will go and it will display the 2. So it has, it will display the 2.

Similarly, it will increase the value of the 2 to 3. Then it will continue the loop again and again. Similarly, until when it will go means it will go up to the 10. i value will become 10 and when 10 is going to be equal to 10 to 10. If it is going to be 10 means it is going to be the nth value. So it will display the value of the 10. Then it will finish. Now, from this, what you can able to understand in the sense, this nth value, it is going to be the number of iterations.

Okay. Number of iterations in the sense, whatever commands which I am going to type inside this var loop will be executed with respect to the number of times which I want it to does. So if I know the number of iterations, then I can use the for loop. Suppose if I don't know the number of iteration, what I have to do is I have to go for while loop. So using while loop, I can be able to run the looping statements without knowing the number of conditions.

Okay. Number of times which I am going to run. But in the case of the while loop, what I have to do is I have to declare the values initialization at first. So here I am going to use the value of a as 0 in the while loop. So I am just declaring it as a is equal to 0. And I am going to give while a less than 10. Okay. So while a value is going to be less than 10, what it is going to do?

A value, let it be a plus 2. Okay. Every time it have to add the value of the a by 2. And I want to know how it is going to make the changes in the a. So I am just giving the value. That is, I am just giving the display command. So I can give the display command prior to this in order to show what is the value of the a which is entering into the loop. So I am just giving this a command over here.

And followed by that. Whenever I am giving the loop, what I have to do? I have to say the program that it is. It's the end of the things. So, I need to save it. So, I'm just going to save while loop. Now, see, when the while loop is getting run, initially, a value I have given as 0. So, 0, it is coming as 0. It is verifying like 0 is less than 10 or not. So, obviously, 0 is less than 10. So, what is doing? It is displaying the value of the a. So, it is showing 0. Now, it is coming to the next statement.

This is going to be the first statement and this is going to be the second statement. In the next statement, I have given the increment of a value as 2. So, 0 plus 2, it is going to be 2. Now, it is coming to the end after running the statement. When it is coming to the end, it will go back to the while. Now, the a value is going to be 2. It will check 2 is less than 10 or not. Yes, obviously, 2 is less than 10. So, what it will do? It will come to the next command, display of a. So, it will display the a.

That is the thing which you are seeing over here in the command window as 2. Now, it is showing the 2 value. Then, it is adding the 2 again with the 2. Okay, because of the next statement. Now, the a value becomes 4. Again, it will go over here and it will check whether it is 4 is less than 10 or not. Yeah, the 4 is less than 10. So, still, the condition is going to be true. Since the condition is going to be true, it will show the 4.

And again, it will increase the 4 value to 2. It will become 6. And later on, it will become 8. When it is going to be 8 plus 2, it is going to be 10. Right? So, when the value of the a is going to be 10 and it is returning back to the while condition, 10 or less than 10. Whether that condition is true? No, right? That is the reason why it is getting exited. So, up to 8, it is displaying the value of the a. Then, when the a value becomes 10, it is exiting the loop.

That is the reason why we are having the while loop value as 0, 2, 4, 6 and 8. Okay. Similarly, for better understanding, I will do one thing. I will change the value of the condition over here and I will run it again. Okay. So, I am just changing the value to 20 over here instead of 10. Okay. Now, let's observe whether the value of the a is being displayed up to 20 or not. Okay. Let's see 0, 2, 4, 10, 8. Earlier it was 8 because the maximum limit is going to be 10. Right?

But now, the limit is going to be 20. So, it is going up to 18. Okay. So, this is how the while loop used to work. In the practical case where you can able to use this one in the sense, suppose, if you want to check the battery, battery level, if it is going below this value, you have to stop giving the supply to the motor. In an electric vehicle, if you are going to do a while loop like this means, if the battery level is going below a certain value, it can disconnect the battery and it can able to stop the vehicle's operation.

So, in such kind of places, we can able to use this type of while loop for verifying what is the value which is there in the battery. That is, what is the charge which is stored in the battery. Okay. Now, we have understood how we can able to create the for loop and while loop. Let us see about the functions. What is the purpose of the function and what we can able to do with the function. So, see, in a program, I want to execute a certain set of statements whenever I want.

Means, what I can able to do, I can create that set of statements into a function and I can store it in somewhere. Then, with the function name, I can call that function whenever I require from the command window. So, using command window, I can type the function name and I can pass the arguments so that the function can be called at any time. Okay. So, that is the purpose of function. Let us see an example with the functions. So, let us see about the functions.

Let us understand why we have to use the functions. Okay. Suppose, if I want to find out the volume of a rectangle. In that case, what I have to do, I have to declare the length of it and I have to declare the breadth of it and I have to declare the height of it. So, here, I have to declare the value of L as 2, V as 3, H as 6. Okay. And, if I want to find out the volume, what I have to do, V is equal to L multiplied with B multiplied with H.

Right. So, if I press enter, I can able to see the volume of that thing. So, if I want to find out the volume of another object, this is going to have a different dimension. What I have to do, again, I have to type the value of the L. I have to type the value of the B. I have to type the value of the H. And, I have to type the value of the formula. Right. Instead of that, what I can able to do, I can type these things in a function and I can call the functions and pass the values, whatever I want.

Let us understand how we can able to do that. Okay. So, for that, what we need to do is, we have to create the function with the function itself. So, function space F. So, this is going to be the variable name. So, this is the variable which I am going to assume. So, I am going to give volume. So, this volume is going to be the function name. Okay. So, for this volume, what is the thing which I am going to pass? L, B and H.

So, these are the three values which I am going to pass. So, the values which I am going to pass inside the functions, we used to call this one as arguments. Okay. The values which I am going to pass, the values which I am going to pass to the functions. So, inside this function, with this value, what I am able to do, I am able to write the formula for the volume. Okay. So, volume V equal to L, B, H. Such, I am able to display the value of V.

Okay. And in the case of function also, what we need to do is, we have to give the end. Okay. Then only, the program can be able to understand that is the end of the function. Okay. And one more specific thing, which you have to keep it in your mind is, when you are saving the function, you have to save with the function name itself. Then only, we can be able to call the function properly. So, here, I have given the function name as volume. Right.

So, when I am saving this program, I have to save with the name of volume only. Then only, I can be able to call this function and pass the arguments as per my requirement. Okay. So, I am just giving save. Since it is there in the correct name. And for understanding, I am just clearing my screen. What I am going to do, I am going to type volume of the data. This, that of is nothing but the C bracket, 2, 5, 9. And I am just closing the bracket.

And I am just pressing enter. Immediately, can you be able to observe the value which is being calculated by this V is being displayed. Because, I have used the formula inside this function. And moreover, I have given the command like display the V. So, since I have given the display of V, the value of volume is being displayed immediately. So, for a better understanding, let's give some other value. Okay. 1, 3, and 5. So, if I pass these arguments, immediately it will show that value. So, if I create a function with my necessary statements, I can be able to access those necessary statements whenever I want to access in the program.

So, I have one new thing which we have to keep in your mind is when you are saving this file, you have to save with the same name of the function. Then only, we can be able to call the function easily. So, see, if I am going to type the volume and followed by that, if I am going to type three values, the first value will be assigned to the L, second value will be assigned to the B, and third value will be assigned to the H.

It doesn't mean that the function should have only two arguments or three arguments. We can be able to pass N number of arguments. Suppose, if I want to send 10 datas or 100 datas, means I can send that 100 or 10 datas from here in the command window, and I can process all the 10 or 100 datas within the function, its output can be reflected over here. So, it will display the output over here in the command window. This is the purpose of function. So, whenever we want to repeatedly execute a certain actions, we can be able to use the functions.

So, now we understood what the function used to do. Let's see what the plot can be able to do. At first, the plot. Plot is something like graphical representation. Okay. So, whenever we used to execute a program, the program will run set of statements. When it is executing that set of statements, it will process the data, and it will convert the data into certain forms. And if we want to know whether the datas are being converted in the exact manner, which we require, what we can able to do, we can bring it to a UI format, that is user interface based things.

So, with the help of plot command, it is easy for us to do that. So, what we can able to do is, using... The datas which are available in the workspace, we can able to do the plot. Let's see how we can able to do the plot. If you want to do the plot, minimum, you need to have one set of values. So if you are having one set of values, what we can able to do is, see we can able to plot. Here, you can able to see X, which is going to be a vector, which is having a range of minus 5 to 10.

So it is executing with the range of minus 5 to 10, which is going to be a vector. And followed by that, we are creating one more vector, Y, which is going to be X squared minus 20. So now we have created two vectors. So using these two vectors, we can able to execute the plot command in different manners. If we want to bring the plot, what we can able to do, we can call the figure. So using the figure command, if you are going to type means it will pop up the figure window, which we have seen earlier, right?

Now, using the plot command like plot of Y, we can able to simply plot the Y. OK, if we wish to plot X and Y both together, we can use the command like plot X comma Y. And if we want to mention in a specific way, so in a specific color only, it need to be plotted means that is what we can able to do. We can use the plot command along with the characteristics. So the characteristics has to be given within a single quotes. So under single quotes, we can able to mention its color, its pattern, which pattern, whether it had to follow a dot pattern or else it had to follow a solid line pattern, whichever pattern, which is required, we can able to create over here.

OK, let's see what are the different commands which we can able to use in the plots. So in the case of the plots, as I have told you, figure is a command which is used for opening the figure window. And plot is a command which we have seen earlier. So using the plot command, we can able to plot different plots. So legend is a command which we can able to use to mention what are the plots are. So we can able to mention the name of the plots, that is name of each curves which are available in the figure window.

And using the title command, we can able to give the title to the plots. Using X label and Y label, we can able to give X axis and Y axis label. And using axis, we can able to define what is the axis which is going to be there. And if you want to hold a plot, we can able to use the hold command. And using plot YY, we can able to have two Y axis in a single figure. And if you want to add a text in the figure, we can use the text.

And if you want to add the text after plotting the graph, we can use the G text. And if you want to get the input from the graph, we can use G input. And if you want to plot multiple figures in a single figure window, you can use subplot. And GCF will give you the properties of the plots. And if you want to print the plot, you can use the print command. And if you want to save it in a different format like JPG or PNG or some other format, you can use the savers command.

And if you want to turn on the grid in the figure, you can use the grid command. So let us see an example based on these different commands, how we can able to do the plottings. Let's see how we can able to do plots. OK, so for doing the plots, we can't simply plot with the scalar. We need to have the vectors. So I'm just going to assume two vectors. So first vector, what I'm going to do, I'm going to take X. So as we have already seen how we can able to create the vector, we need to open a square bracket.

Then followed by that, I'm just giving the initial value K and its deviation. I'm going to give like 0.01, then followed by that and is giving its N value. OK, so this is the vector which I have created. And now the X is a vector which I have created now. I'm going to create one more vector because plot can be done with the single command or else we can single vector or else we can able to use two vectors to do the plot. OK, so sine is a function with which we can able to create the trigonometry sine function, sine value.

So here I'm going to use sine of X. OK, so Y, which will be equal to sine of X, which will create the sine wave for the Y. OK, so now there are two vectors. One is going to be the X and another is going to be the Y. OK, now let's see how we can able to do the plot. So if I'm going to give plot and I'm going to give simply plot of Y. OK, I'm not using the X with single command if I'm going to give, see a figure window have open and in that figure window, it is showing the sine wave.

Right. So you can able to see, right. So in this figure window, by default, it is showing us figure one. But if I want to use a specific figure number, what I can able to do, I can use figure two. OK, so I can declare like this. So figure two immediately, if I'm going to type means it is open. Right. Now, let's observe how we can able to do the plot in a different manner. So plot of X, Y. Earlier, I have given only the Y.

OK, plot of Y only I have given. Now I have given plot of X, Y. Let's observe how it is going to be coming. OK, now this is going to be the figure one and this is going to be the figure two. So just observe its difference. This is going to be the plot of Y and this is going to be the plot of X, Y. So what is the difference you can able to see over here is here the function is being executed. That is, the plot is being plotted until the maximum value.

Whereas here it is comparing the X and Y vectors and both together until when it is going to be there. Up to that value only it will plot. That is a speciality of this plot X and Y. So this is plot Y and this is plot X and Y. OK, now I'll minimize this. I'll go for one more thing like let me take figure three. OK, I'm just taking figure three. And for this figure three, what I'm going to do is I'm going to give some more additional things.

Like I'm going to add some colors to the graph. So plot X, Y, what I'm going to do? I'm going to use yellow color with asterisk. I want to create my plot with the asterisk symbol in a yellow color. So in that case, I can use this. OK, and I'm just closing this. And while you are mentioning like this, what you need to keep it in your mind is followed by the axis. You have to put a comma. Then within the single quotes only, you have to mention the details.

So if you are going to give a B, it will consider as a blue. And similarly, depending upon the alphabet which you are going to provide over here, it will consider the different colors. Let's observe how it is going to come. So see, the figure three, it is going to be in yellow color. It is there in the yellow color, right? So since I have mentioned the Y, it is plotted in the yellow color and with asterisk, not with the dotted line. So I can be able to observe it by zooming in.

So I click zoom. Yeah, see, if I zoom further, you can be able to see instead of dots, it is having the asterisk. Since I have given the symbol as asterisk, it has been plotted with the asterisk symbol itself. So likewise, I can be able to plot in different modes. Okay, suppose if I want to add some more annotations to this, annotations is nothing but making the user to understand what other things are being done over here. So I can mention the legend. So it will be like a scale.

Okay, so I am just giving a low graph. Okay. So legend, when you are declaring, you have to use the quotes, single quotes within the C brackets. Okay. So see, if you are using, immediately it will be shown over here. See, that asterisk is being shown and it is showing the yellow graph is there. Okay, but it is not sufficient. I just want to add something more to this means what I can able to give. I can give title to this. Okay, by giving, by using the command title, I can give like it is going to be the sine wave.

Okay, so title and followed by that title, what I have to do, I have to use a single quotes inside the bracket and press enter means it will be updated to the last figure. See, sine wave, whatever title which I have given, it will have been reflected over here in the figure window, right? Sine wave have came. Now what I can able to do, I can able to add labeling. So X label, X axis, let it be time, okay. And Y axis, I can able to use Y label, okay.

Y label, I can use this magnitude. Okay, so magnitude. So I have given these two parameters. Let's see whether it have been reflected. Yeah, see. There. X-Label and Y-Label have came, right? So, moreover, what I can able to do is, I can add grid to this. Grids are nothing but the lines. So, how I can able to add means, by using the command grid, G-R-I-D, if I press enter, it will be added. See, just like our graph sheets, the grids are being added to this graph too.

Okay. And, if I want to plot one more graph to this, what I can able to do is, I can hold this and I can plot that. Okay. So, what I have to do, I have to use hold. So, see, if I type the hold option, immediately showing current plot L. So, the current plot is going to be figure 3, not the figure 1 or figure 2, it is going to be figure 3. Okay. Now, what I am going to do, I am going to take one more variable, Z. I am going to give cos of X. Okay. And, Z value I have declared. Now, what I am going to do, I am going to plot the X, Z. Okay.

So, if you plot X, Z, where it will be plotted means, it will be plotted in the same graph. See, the figure 3 itself, it is plotted. So, we can be able to plot multiple graphs in the same figures using the command hold tape. So, I will repeat the commands which we have learned for the annotations purpose. First one is the legend. So, if I am going to use a legend, we can be able to declare which curve is what. See, since I did not give the second value of the legend, it has taken the data 1. Okay.

By default, it is taking. Otherwise, if I am going to specify the data, it will take that data and it will show what is the second curve is going to be. And, next one is the title. We can be able to use the title command. Using the title command, we can give the title to the graph. And, using the X label and Y label command, we can be able to label the X axis and Y axis separately. And, using the grid command, we can be able to formulate the grid lines. So, we can be able to get the grid lines in the figure.

And, using the hold, we can be able to plot multiple graphs on the same figure. So, in order to plot multiple graphs on the same figure, we can use the hold. So, once you are using the hold, the last graph, which is nothing but the current graph which we are using, it will be hold. That is the current figure. Okay. So, here, the last figure is going to be the figure 3. That is the thing which got held. Okay. See, then, when we are going to use the next plot, it is getting plotted in the same.

Okay. So, this is what we can be able to do in the plot. I hope you have understood the plot commands in the MATLAB. Now, we understood the different commands how we can be able to use in the plots. Let's see what are the things which we have learned from this video. So, we learned about the different conditional statements which are there in the MATLAB. And, we learned about the looping statement where we can be able to use the for loop and where we can be able to use the while loop.

And, we learned about the plots and functions. How we can be able to do the plottings and what is the purpose of the functions. These are the things which we have learned from this video. So, in the next video, let us see about the Simulink.

Want the rest of EV Fundamentals?

Get the full course syllabus and fees.

The full course is paid (₹40,000). We'll send the syllabus and call you back within one working day.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Full course · paid

Unlock the full EV Fundamentals course

The remaining 52 lessons, mentor-reviewed projects and a certificate come with the paid course (₹40,000). Leave your details: we'll send the syllabus and fees and call you back.

Get the syllabus and fees

A counsellor calls you back within one working day.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.