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

Unlock all 12 lessons of Introduction to Model-Based Development using MATLAB and SimulinkFull course ₹40,000 · paid

Price & syllabus
Free lesson · 133 min · no payment

MATLAB Tables, Functions, Callbacks and Scripted Simulink Models

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: “2.0 - Tables & Structures, MATLAB Functions & Callbacks”

What will you learn in this free Introduction to Model-Based Development using MATLAB and Simulink lesson?

The instructor covers MATLAB table arrays, creating a table with the table function and reading a variable with the dot operator, for example the mean of a column. The lesson goes on to functions and callbacks, shows a GUI volume calculation example, and ends by building a Simulink model from a script.

  • Creating a table with the table function
  • Matching row counts across table variables
  • Push button callbacks in a MATLAB GUI
  • Using replace_block and set_param on Simulink blocks

What comes after lesson 2?

The next free lesson is lesson 3, “Simulink Basics: Blocks, Signals and the Toolbar” (91 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

Hello. Welcome to today's lecture on tables, structures, and MATLAB functions. This is a part of the continuation of the MATLAB scripting tutorials. And the topics that we are going to see today are about the table arrays, the structure arrays, and the MATLAB functions. So we will quickly go into the topics for today. So the first topic is MATLAB table array. The table arrays store column-oriented or the tabular data such as columns from a text file or a spreadsheet. Tables store each piece of column-oriented data in a variable. Okay. And they are accessed and created using the MATLAB command table of VAR1, VAR2 until N. So you can use N number of variables.

To create a table, you basically use the table function with a size as the first input. And then that would be followed by the variables. So variable 1, variable 2, until variable N. We just need to ensure that whatever size that you give for the first variable until the Nth variable, it is the same. The same size is being maintained throughout here. So the input variables specified as arrays can be with the same number of rows. The input variables can have different sizes and different data types. So the column size can differ, but the row size has to be same. That is what we are talking about the table function size.

So while you are declaring the row size. Okay. So while you are declaring the variable, the row size must be mentioned, whether it is one row, two rows, three rows, or four rows, like whatever it is. The number of columns can change. The data types can change. And the variables can also include elements like the numerical arrays, logical arrays, character arrays, cell arrays, etc., etc. So as I had already told, the command in MATLAB table is what we have to use. Now quickly we will go back to tool and we will see how this example can be recreated in MATLAB.

So we are quickly moving to MATLAB as seen in the last class. I am just currently clearing my command window. My workspace is already clean. So what I am going to do here is like I am going to define a table array. Okay. So how am I going to define? Let's see. Let's give is equal to table of 1 is to 4. Okay. I am giving a transpose version. The reason why we are giving transpose version, some might ask, we can directly give it as a 4 is to 1, but MATLAB does not take it that way.

Okay. So that's why we are giving it as a 1 is to 4 and a transpose of it is being used. And then the same example, we will go for a matrix of 1s. That is 4, 3, 2. Okay. And then I am going to define the third element. Okay. I of 4, 2. I of 4, 2 basically is going to give me a table of 4 rows and 2 columns. And that is going to be defined as my third variable here. Now I am closing the braces and I am going to hit the enter button.

Yes, it is a 4 cross 3 table. The headers are defined basically as variable 1, variable 2 and variable 3. Sometimes we can give a customized name to this. Let's say we can define 1 is to 4 transpose can be given with name A. 1s of 4, 3, 2 can be given with name B. And I of 4, 2 can be given with variable name C. And when you are going to define it that way, the table headers basically change from variable 1, 2, 3 to A, B and C respectively.

So this is the column wise elemental representation. This is a transpose of 1 is to 4. So basically there are 4 rows in a single column named as 1, 2, 3 and 4 respectively. This is a 1 cross 3 cross 2 double array that is being here. And I of 4, 2 is again a combination of 1s and 0s. I am talking about this variable 3 column, this table. And now each is a variable and each variable depending on the initial size that we define here. Here the size is being defined as 4.

The same row size 4 is being maintained throughout for all the variables. It can be dimensionally different, but the rows must be same. Otherwise, it will throw error strings basically. So this is how it is all defined. Now basically apart from defining the variables, we can play with them. How are we going to play with them? Like in the last session, we saw about how we define the arrays and the array manipulation and the matrix manipulation. We were using the dot operator. Similarly, using the dot operator here, we will be accessing the different properties of specific array elements.

Now, I wish to know the average of the variable 1. I have this variable 1. This is 1, 2, 3 and 4. For this specific variable 1 and for this specific column, I wanted to know what is the average of it for this column alone. So, we can check it here. So, we can check it here. So, AVG I am giving is equal to a command mean is basically used to get the average. So, the table is defined with the name K. We can see it here in the workspace.

What I am going to give is like K dot variable 1. Variable 1 is the first element or the first variable of this table. And I hit enter the average. That is 1 plus 2 plus 3 plus 4 is 10. 10 divided by 4 is 2.5. Return value 2.5 is thrown. Right. Now, how are we going to see the different properties? Similarly, using the dot operator, we can access the different properties of this table and the various elements. Now, if I wish to know what is the property of the table and the different elements that is associated with it, we can simply use the dot operator again to access and check for the properties, which is K dot properties we would have to give.

And now, when we hit the enter button, the return string basically throws the table property with different property elements like description, user data, dimension names, the variable names, the variable description, etc., etc. Here, using the add prop and rm prop, that is remove property, we can modify the custom properties here. Or if you wish to access a specific property, you need not list down all the properties. If you wish to access any one specific property, that again is possible here. Using add property, see these, the variable descriptions are empty strings here.

They are empty. So, if you wish to give any information about what kind of variables they are and for what purpose that you are going to use these variables in your applications, we can certainly name them here. And if you wish to access any specific property of these variables, that again is possible. We can have a quick check on that. Similarly, K dot properties dot this variable name. Again, the second time the dot operator must be used and we need to give these variable names. If I would want to know what are the different variable names that is here, I can type it as variable names.

It returns me with a cell array with three different array elements or array values. That is variable one, variable two and variable three. Likewise, this variable name can be replaced with variable description, variable units or row names or description or whatever it is. So, this is how the table arrays are manipulated and how the table arrays are accessed using the MATLAB scripting. Now, let's quickly move on to the next topic, which is the structure. In structure, what we do is like we have, it's again very similar to my table arrays.

The only thing is that here we have elements of different data type. There, we predominantly had elements of numerical data type. Here, we can have a combination of numerical data type. We can have even strings, which is attached to it, where we can put them all together and we can go for manipulation of the data. A structure array is a data type that groups related data using containers called fields. Each field can contain any type of data. This is the key point that we would like to note here.

Here, a single field there in the table array, we saw it was all variables, which was governed predominantly by the size. Here, a single field there in the table array. We specifically have arrays with same number of rows, same numerical or logical inputs are required is what we saw. Here it is predominantly of any data type that you can have. A single row or a single variable can have both numeric and alphanumeric inside its fields. That was not possible with tables. If you're having a table of numerical data, it should only be numerical data.

If you're having a single variable, it can contain only numeric. It cannot contain anything else. So something like that cannot contain an alphanumeric value. Basically that was there. Here it is not like that. We can have a combination of both. So the structure creation basically looks like this. We are using the keyword struct like how table was used. We are using the keyword struct. And again, one more thing we need to note here is that there it was all about sizes and the width. variable names. If you can see here it is not about the sizes. It's about the field. What category it is and then the direct values of it. Okay. If you can see this example here, it shows me C, I mean struct of strings, which is hello and yes. Hello and yes are strings, which are stored as in a specific field.

And their length. Their length is also explicitly mentioned here as 5 and 3 and that is stored. So you can see here this is a string data type. This one is a numerical data type, which is the size or the length of the string. So these are of two different data type. They are explicitly defined and they are used here. So in this way, we can define the MATLAB structure array. Now let's quickly move on to the tool to see how we are going to define. So the drills are similar. If you wish to have, you can have this. Otherwise, we can clear the command window and we can clear the workspace as well.

Which is CLC and clear all. Now we can have a structure array example. Something like what we do with a name, degree and marks like about the student database. So to create a student database using a script, I think this tool is a very good one. The structure is a very, very good tool or a very, very good command to be more precise of handling this. So we will go directly to see how the problem that I am going to take right now is about the student database.

I am going to have a set of two students, their name, their age, their degree and their marks. Basically, we are going to take them and we are going to put them at the point to be remembered here. It is the field and then the values. So the first thing is fields. Again, if we can clearly see here, the flower braces is not there. Flower braces indicate the values are not given within any quotes. They are directly given within the flower braces at the field. So the category of the category of what is it a name or is it a degree or more in the lines of heading, right? How you give your headings more on those lines that come under the field that is field one, field two, etc, etc.

So let's again quickly move into the tool. I'm going to give it as V is equal to struct of say my first field I'm going to give is name. Okay, name. Fine. So you can see in the pop up here it is field comma value. So field one value one until field n value n. So my first field I have defined here. Okay, now I need to give it the first value under my flower braces. Okay, I'm going to give it as say Joe comma Jim. Since it is a structure, it is a character array that we are going to access here inside a structure, we are using the quotes to access that particular particular character array element in a structure. So that's why it is defined within quotes. This is basically for the characters. That's why if it is a numerical, you need not define it in this way. We can directly even instead of using the flower braces, we can use the square bracket elements that we will see how we can do it. So we have defined the first field and the first value.

The next I am going to give something called as age. Okay, so now this comes as a second heading. Okay, 23 comma 22. Again, as we have told for a flower bracket, we for a character array, it's a flower bracket. And for a numerical array elements, it is a square bracket. So this difference we can be seen here. So this difference we can be seen here. Now the next is about the degree. Okay, I'm going to give it as okay, arts square bracket and signs. I'm closing my square brackets. And I'm going for marks. And in marks, it is one second, we can give a numerical representation, which is basically 9 comma 8, for example, and I'm closing the structure element here.

So I give an enter. Yes, the structure fields are name, age, degree and marks. And it is for Joe and Jim, age is 23 and 22. A degree is arts and science and marks is 9 and 8. Now when I try to give it like this, for the table, you can see here again clearly, variable name V for a structure, a variable name K is defined here. You can see in the workspace, depending on their properties, they are defined, which is a 4 x 3 table and a 1 x 1 structure.

The names of these variables are defined as variable 1, 2, and 3. Here the variable names are replaced by the fields. The values are here more defined on the lines of what they are. Here again, you need to follow if there are total number of elements, say if there are 3 students or 4 students, accordingly the same number of values that you have to mention here. But it is not a very compulsive one or it is not very difficult like how define or declare a variable name in table. It is much easier to follow and to define in structure arrays.

We also saw that how we individually access the properties of different variables and different elements of table. Likewise, a similar thing can also be done for structures here. So we have this structure V that is created. Now what we will do is like I would like to access the names alone. How am I going to access? I am just going to simply give V dot name. It creates a cell array of Joe and Jim. Likewise, if I am going to give something as V dot marks. Sorry, it's a mistake from my end. Yeah. Okay. Yes, it creates a cell array of two elements.

Now, if you wish to create a structure array, you know what are your individual elements. Okay. Say this example we are talking about. You have two set of individual elements, which is S dot A is equal to one and S dot B is equal to ABC. Basically, S dot A is a numerical value one, which is a one cross one element. S dot B is a cell array, which contains three different values. You need to put both of them into separate fields. So S dot A and S dot B, A and B become two different fields and their values are one and a cell array of ABC. So how are you going to now create a structure S? Okay.

A similar drill is actually followed here. S dot A is equal to one. Like if you can clearly remember in the earlier classes of matrices, we were talking about how we access particular elements. So you have an array. If you wish to manipulate one particular value of an array, you go and directly define it with a different value. Say it's a two cross two matrix. It is all filled with zeros. If you wish to change the one cross one element of that two cross two matrix as one instead of zero, we simply give it S matrix of one cross one is equal to one.

So the whole value of the matrix now changes from 0, 0, 0, 0, 0 to 1, 0, 0, 0. Something similar is what we are trying to achieve with structures here. Okay. So S dot A is equal to one and S dot B is equal to. A, B, C. All right. Fair. So we have a structure S that is created with two different fields. And we can clearly note here that the size of it is again one cross one for S dot A and S dot B. And there are two different field of elements here.

So that is how we basically define structures. We can go and directly define the field values in itself and then we can create structures as well. So this is about the structures and the tables. Structures are more powerful computing method and computing tool. There are applications where there is a lot of mathematical manipulation in your system. We basically go for structures. So let's move on to the other set of topics. Last topic of this given lecture that we are going to look at is functions and callbacks.

So among functions and callbacks, we are only going to look at functions in this particular lecture. So what is a function? Okay. It has a caller and a callee. If you can see this given diagram, a function has a caller which tries to go and access its callee so that the callee performs a specific set of operation and it gives out the results. So the function is like say, hey, can you execute the results and give me? A caller asks the first thing like, hey, can you execute this problem and give me? The callee gets the respective set of inputs. It executes it. The mathematical or the manipulation part of it is done here. And then the final results is pushed back to the caller where it gets published.

So a function is a function. So a function is a group of statements that together performs a task. In MATLAB, the functions are defined in separate set of files. The name of the file and the function should be the same. We can see how we are going to do it, how we are going to define a function. You have a specific function. You have a specific function name, that specific function name. MATLAB itself prompts you to save it in that way. When you are trying to save it with a junk name, a normal script can be saved with any name. You can save it as untitled, example 1, example 10, etc, etc.

But if you are performing a function specifically for addition or a minimum maximum or squaring of a given number, it should be saved as the name of a function. So here you can clearly see in the green color, my function of it is defined, right? My function of my inputs. So my set of inputs is known here. My set of outputs is known here. And this function command must be the first line of your function script and your file will be saved as myfun.m. When you are saving a m script and when you are saving a function, you can see in your current working folder tab of your MATLAB, a specific difference of the icon shape would be a bit different.

That gives you an indication that, okay, this is not a script and this is a function. And your functions cannot be run directly. There is a separate way to actually run your function. So that also what it is and others we will see here. Okay, functions operate on variables within their own workspace. And that is basically called as their local workspace. And they are separated from the workspace you access with the MATLAB command window. So whenever you are operating on functions, functions create their own workspace and access all the elements related to them locally inside their own workspace. Functions accept more than one arguments on input and may return more than one output arguments as well.

So this is how you define the function which we saw a little earlier. You can save your functions with the name of the function definition. Again, we said that only myfun in this case of this example screenshot that is shown here, it is myfun.m should be the function name that you are saving. In a script which contains commands and function definitions. In a script which contains commands and function definitions, functions must be at the end of the file. Script files cannot have the function name same as the function in the file.

So we will quickly move to an example of how to create a function. And before that what I am trying to do is I will just clear my workspace here. Command window and my workspace is getting cleared. Functions. Functions. It's always safer to play with functions as a separate file. You need not have a command window to execute your functions. It sometimes does not give the desired results. So for all the function related examples, we will create a new script. Either you can go and create a blank script or you just create on click on new. You can click on functions. It opens a template for you where you can go and make your changes.

So here it is. This is what we were talking about. Let's close this untitled. So your current file name is untitled to hence the function name is given as untitled to your function of output arguments and number of output arguments you can have is equal to untitled of input arguments. And it is here where this is. This is the cally part. The functions first line is usually the caller part. This is the cally part. Here is where your actual calculations will happen. And finally, we will see about how to save it and how to run it. Right.

So the first example let's take is about see a simple square operation. That is I'm going to perform a simple square operation. Say if I'm going to give my input value as 2 output will be 4 input value as 3 output will be 9 which is 3 square 5 25 7 49 etc and so on. So how I'm defining is it is like this. Function of y is equal to let's change this to square. Okay. So square of x. So the number of arguments that we are having is only 1. We have 1 output argument and 1 input argument. Probably if information related purposes you can edit these comments here directly.

You can give something like calculate the square of the given number x and your arguments are x and y. Okay. So the logic part is y is equal to x and y. x into x and my end statement. Now this is the whole logic that we are going to have. I'm going to give a save. Automatically what is my name of the function here square. It takes the name. If I wish to change this to sq and I try to save it. It automatically takes the name sq in my save as option. So that's how you have to actually save it. Otherwise you cannot save it. Matlab throws an error string here. So we will save it with the actual name of square.

Here. And previously we had seen. I'll again open a normal. A difference I'll show between a normal script and this script. Okay. I'm here. You cannot use clc and clear all in your functions. The first line will always be the function caller. But here. But here you can give it something like clear all clc. I'm going to have something like a is equal to b into b. Okay. b is equal to 2. I'm hard coding it. I'm saving this. Let me try to save it with the name square value or need not have it as square value. I can even save it in a junk name called example.

Right. Right. Now I open my file example and I try to run it. Okay. Fair. So my b is 2. A becomes 4. Now similar drill we are going to do for the functions. But how it is we can see here we use the clc clear all. I am directly hard coding it. I click on the run button. It runs. As far as functions are concerned. You click on the run button. It throws an error message. It's not enough input arguments. And MATLAB even throws a pop-up like run square of x. So this is how you have to run it. So instead of clicking the run button you have to give it with the name of your function square of x.

2, it gives 4. The answer 4 directly is given here. So again, square of 19, it throws 361. Square of 100, it throws 10,000. But here, every time you have to go and you have to change it and you have to run it, it throws 361, etc., etc. So this is how and you can also clearly see that the value X and Y is not displayed in your MATLAB workspace. Functions operate on a very specific workspace and they get saved there. So this is the basic operation of the functions and the related commands. More about the functions you can learn with the other sessions. So this comes to the end of the given session and thank you.

Part 2

Hello. In this class, we will be looking about the file handling formats. What are the different types of file handling formats? What are the different files that can be accessed and handled in the MATLAB environment? About debugging, what basically debugging is, how to debug MATLAB scripts, about the flow control logics, and then the callbacks. So, let's quickly move to the first topic, which is the file handling formats. So, this, the first column, which is present here, which is the file content, shows what are the different types of files which can be accessed in the MATLAB environment.

Basically, if there is a text file, or a spreadsheet, or an image, or a video, or an XML file, which is extensible markup language file, they all can be accessed by the MATLAB. What are the different values, which are basically present in it, can be accessed in the MATLAB environment, it can be manipulated, and can be presented. So, in the first column, if you can see here, it is the MATLAB formatted diagram. The first row is about the MAT files, which is called as the MATLAB formatted diagram files.

Basically, what we do with the MATLAB files is, like, we can load the MAT files, and then we can save the MAT files. Say, we have some certain set of files in the MATLAB workspace. If we wish to save those files, we can use save the MAT file option. Likewise, whatever variables that are defined in my workspace, which I have saved it as a specific .mat file. If I wish to load back for a different project, or for some other application, or for some other usage, we can use the load MAT file option.

Likewise, text files can be accessed, which is of two formats, basically, CSV and TXT. Spreadsheets can be accessed, XLS, XLSB, CLSX, etc., etc., which is basically read, write, and these set of operations can be done. About the images, BMP, GIF, JPEG, JPEG, PNG, different file formats can be read, and can be written, and different properties related to the images can be seen. Likewise, the same can be done using the video reader and the video writer command, and the same goes for XML as well. That is, using XML read and XML write.

So, this is the file formats extension. This is the type of file. The first column is the type of file. The second column here is the file format extension, and the third column is basically what work they do here. The fourth column that they have is about the read and write operations, what basically these read and write operations are, and what they do is what we see here. So, we will take one of the most commonly used examples in MATLAB is to read an Excel file, basically, and to create an Excel file using this.

So, if you wish to read an Excel file, the different values which are present in an Excel file, we use the XLS read command, and to create your own Excel file, or you have some array which is stored in my MATLAB workspace. When you wish to write them into an Excel file, you use the XLS write command. So, we will see how we are going to go with XLS read and XLS write here. So, this is about for only for XLS write, we will be seeing here.

XLS read is basically a small change of what we do with XLS write. With XLS write, as the name suggests, writes into my Microsoft Excel spreadsheet file. XLS write of file name, comma, M, okay. Writes a matrix M into the Excel file name. If you can see this example here, it is something like Excel is write of test data. Test data is my file name here, and the data which is present within the square braces is my array of the following set of values, which will be written into my Excel sheet.

So, when this command is executed here, the following Excel sheet with the name test data, which we can see in this screenshot, gets created, and the values are entered along the rows. So, this is how it is. Now, we will see how this can be done in the MATLAB tool in itself. So, let's quickly move to the MATLAB tool. Okay, I'm closing whatever files that were open. I'm just cleaning my command window and my workspace. So, what I can do is like I will open a script this time.

Let's create a new script, which is exclusive for this operation. So, the first is Excel is write example. What are we going to do is like a student database kind of thing we are going to create where we will have information like name, age. This is going to be entered into an Excel sheet with the name, something like student database. We want it in this specific format. Excel is write supports multiple format, but the format that we are looking at is .xls. And we are looking to save it into a specific sheet.

Okay. In this slide and in the screenshot, if you can see, by default, it is written into the first spreadsheet. By default, it is saved in .xlsx format. But now, I wanted it to be saved into a specific sheet. The sheet will have a specific name and it must be saved with a very specific extension. Okay. So, that we will see how it is working. So, let's now create a variable with a name like data. I am going to feed in my values into it, which is basically the name and age we can have.

Okay. Name, age. Okay. I am giving the name as Jim, 24. Clearly, you can see here that only for the characters, we are mentioning them within the quotes. For numerical representation, it's simply plainly we have given like what we have seen with the other classes. This is just a reminder of how it operates. Now, I am going to give my second set of inputs, which is Jack, 21, Jones, 22, Andy, 19, and the last value that I am having is Steve, which is 18. I have closed it here.

One thing we would like to note here is that if you are going to give for one set of rows, that is your name is one column, the age is the other column, Jim, 24, here is the first row. The next row is Jack, 21. The third row is Jones, 22. What we are going to give here is every row here must be separated with a semicolon. It should not be a comma operator, but a semicolon operator we have to give. So, this Jim, 24 is one row.

Jack, 21 is the second row. Jones, 22 is the third row, and so on. Now, I move to the next line. Here, we are directly executing the command. XLS write is equal to, let's give the name, student database dot XLS, and what is the variable name? Data. Data, and now the third part, I would like to save it into a specific sheet. So, the sheet name, I am going to give it as student info, and I close it. And, okay, I am saving this as, say, something like file formatting, okay, okay.

Now, when we try to run this application here, invalid expression, okay, I think there is something that we have to correct, we'll just, ah, yes, it should be XLS write, sorry, that's my mistake here. It must be as XLS write off. I am trying to define something into XLS write, basically, which is a wrong thing. So that's why MATLAB is saying that it's an invalid expression and it is not written properly. So that part is now corrected and when I try to now save it and when I try to run it, yes, you can see this left bottom corner, it's busy, which means the script is under execution.

Now you can see in the current folder list, Microsoft Excel worksheet has been created with the name studentdatabase.xls. Now when I try to open the same, okay, I can open it outside MATLAB. Yes, student database is created in the sheet student info and that sheet is like name, age, Jim 24, Jack 21. So this is how basically an Excel sheet can be created. Likewise, if you wish to access the particular set of data from your Excel sheet into the MATLAB environment, if you wish to write them into your workspace or something, you can definitely do the same.

So but here, like how writing it into your Excel sheet was different. With strings, you have to write it in a specific way and numerical data, numerical arrays, you have to write it in a specific way. Likewise, even for Excel is read, you have to read strings separately. You have to read numerical content separately. That we can see here. But before that, for reference purpose, we'll just open this Excel sheet and see this column. The column A here is completely with strings. The column B here is completely numerical data.

So when I wish to read my strings, we give it in a specific way. We can see that. So XLS read example and XLS read off. You can clearly see which is the file name. And so as you can see here for XLS read, we have a file name, file name, comma, sheet, file name, comma, range, different, different strings and properties are associated with this command. So what we give is first we give the file name. We have this file name, we have this file, student database dot XLS.

All right. Now I'm going to go to the specific sheet, student info. All right. And now I'm defining the range. The range of values here is like if we can go back to the sheet quickly, A2 to A6 is what it is defined and B2 to B6 it is what it is defined. So I'm going to mention it as A2 is to A6 and I'm closing it. So this is how you read strings. The next one is we are going to read the numerical data. So for reading numerical data, we can directly give it as age.

You need not give it as in this format. Only for strings we need to define it in this format. Now for age, what we can do is quickly XLS read off. This information is going to be the same. So you can copy it. You can paste it. But it's going to be B2 is to B6, the student age. And now say, okay, someone does not know how many columns or how many rows are there in the Excel. And if they wish to try to access a column or a row which does not have any data in that, how it would be or what would be the return string here, that also we can see.

Okay, I'm going to give something like say department is equal to XLS read off. I'm going to give the same set of information. If you can clearly see in the Excel, we only have the columns. We only have the columns A and B. There is nothing in column C. But again, we are trying to read this Excel. So ideally, it must return empty strings for us. Let us see. And now since the Excel sheet is already created, we are going to comment them. So how to comment them, you just select the respective text and give control R.

The lines get commented. To uncomment them, you can use control T. This is the shortcut. Otherwise, go at the start of the line, go and give the percentage character. The whole line, whatever follows that gets completely commented. All right. So now it is already ready. So I'm commenting for the right part since the Excel sheet is already written. Now I am going to run the script. Yes. It is created in my workspace. And okay. So what I'll quickly do is let's call these values in my command window itself.

Yes. First variable name is created, which is a five cross one array with different values like Jim, Jack, Jones, Andy, and Steve. Their age is a numerical value, which is again clearly represented. Here what we were talking is about an empty array. So the empty array is there. We are trying to fetch the data from C2 to C6. That is actually not possible because there is no data in it. So it returns an empty string for me. It returns something like zero cross zero empty array.

So there is no value in it. So this is one of the ways to access the data from an Excel into your MATLAB environment. And the previous case was an example of going back to create your set of you have some data with you if you wish to put that into an Excel format, we can use this option. So this was about the Excel file formatting. Let's go back to the presentation slides. The next option is like loading into a workspace and saving the data in the workspace environment into a file.

The workspace is not maintained across different sessions of MATLAB. When we quit MATLAB, the workspace gets cleared out. Sometimes when we accidentally give a clear command, the workspace gets completely cleared out. And sometimes not all the calculations we write it in the script. Few set of calculations we write it into the workspace. When we immediately wish to define the parameters of any variable or something, I write it into my command window. And then when I hit an enter button, if that variable is already existing in my workspace, the value gets updated.

This we have seen in the previous classes. So now the updated set of values will be there in my workspace. If I wish to save those updated set of values from my workspace, I use the save workspace command. So whatever content that is present inside my workspace will now be saved in as a .mat file in the current working folder. And likewise, if I wish to load them for any future purposes, I can same use the load of workspace command here. So how it is basically being done?

We'll quickly look at it. Now you have this age department and name in your workspace. If you wish to save your workspace, what you can give is save of workspace, all right. It creates a workspace.mat. Now if you wish to load your workspace, what you do is you go give a load of workspace. But okay, before that, I'll clear my MATLAB. So my workspace is now empty. As you can see here, what I'm giving is I'm going to give load of workspace. Yes, the same set of values are now loaded into workspace.

You can clearly see the name age and department is being loaded into my workspace. So this is all about workspace data logging and data manipulation and about the Excel read, Excel is read and Excel is write we have seen here. So we'll move on to quickly the next topic. So the next topic that we have here is MATLAB debugging. So to give an overview of what MATLAB debugging is like, so far the problems and the scripts that we are trying to analyze and we are trying to understand are all simpler ones.

In real time, in your work environment or in your any project environment, one would have to handle a larger volume of work with MATLAB. You might have to write big lines of code to execute an operation or an application. And at that point of time, if there is any error and if you wish, if you are finding it difficult to actually analyze where this error is coming, how this error is coming and why this error is coming, a step-by-step way of analyzing and understanding what this is, can be done with the debugging part.

So, fastest way to learn scripting again is debugging, how exactly your variables, see, not all operational variables get executed and are shown in your command window, are displayed in your command window or is loaded. Certain values are actually getting manipulated in the run and they will be loaded in the workspace completely. So, to see exactly at which point how the value is loaded into my workspace, whether it is loaded as a row array or a column array or is it loaded as a matrix, which element of the matrix is being pulled out, using which element, which kind of operation is going to be controlled, all can be known in the MATLAB debugging state.

So, for any person who is new to MATLAB, if he wishes to learn MATLAB very quickly and very effectively, I think using MATLAB debugging, he can quickly learn. So, before you begin debugging, make sure your program is saved and that program and any files and the supporting files, whichever it is related to it, is all there in the current working folder or in your current set path. Just ensure about that and so quickly we'll move to the debugging.

Part 3

Say you wish to debug this specific command or this specific script. Write or Excel is read is going to work. Fine. We'll do it here. So you can see here the number of lines 1 to 11 is there. So we have written the script with 11 lines of code and wherever you have a command, whether it is any executable command that is present, you can see some set of dashed lines. So what you can do is like you can place your mouse and just do a left click here on these lines. You can see the change in color as a red dot. So this red dot is a breakpoint that is being fixed to analyze and see how this particular operation is going to work. And likewise, for a big script, this is just a small one with 11 lines of code.

If you wish to execute something with more than 11 lines of code, you can actually go for multiple dots here. In the same line, you can keep multiple dots. If you wish to remove a breakpoint from there, you can remove them. So this is called placing of a breakpoint. If you wish to remove a lot of breakpoints, you can directly click on this breakpoint option and you can give clear on. Now we are placing the breakpoint at the line number five and we are going to see how this whole operation is going to work and what is going to happen here.

In the meantime, we will also be seeing if there is any error in the script, how it is going to throw up in the command window. And once this breakpoint is kept and when we are trying to run the script, what are all the different changes we saw in the presentation slide that it goes into a pause mode? So what is that and how each step-by-step script is going to be executed in the debugging stage can all be seen here. So in the fifth line, we have placed the breakpoint. We have already seen what it is.

So this is a very simpler script. So we'll once again see how the execution is going to happen at this stage. So I have placed it in the fifth line and I am going to click on the run button now, right? You can clearly see that there is a green arrow which is at the fifth line here. It means that at the debug point, it comes and it halts. At this stage, you can click on the step run next line option so that it moves step-by-step across each line.

So this green line denotes the which line to be executed next. In this case, the CLC and clear all were executed and now it is in this stage of execution. So this is going to happen now. Once we click on the step icon, the fifth line gets executed and it moves into the sixth line. So I am going to now press on the step option. You can clearly see that the workspace gets executed. Here an array of six cross two is created with the name data here.

Here the variable name is data. So the same is there. Here one more thing while we are trying to execute in the debug mode, we can see here. Whatever line that we have completed the execution and whatever data that is already been accumulated into my workspace, the same we can double click on the workspace and we can see that is one option. Apart from that, there is also another option. Since the fifth line is now executed, when you try to run your cursor over this and see, yes, it is throwing a pop-up right now, which means that the variable name data has been executed.

So this array has been generated and this is the value that is presented. So this gives a very quick and interactive way for the developer to quickly analyze his problems rather than going every time to workspace and see what's happening. Now, so this fifth line is now executed. I am now going to go and execute it into the sixth line. Click on the step again. Yes. Yes. This gets executed here. Now, again, click on step. Yes. So Excel is the read name has been done. So the next one is age.

It's done. Yes. So in this stage, every operation has been done here. And we can see that once the last line is executed, you can see a drop in arrow over here. So which means that this is the end of execution of the script. And once you click on step again, it goes out of the debug mode and it comes here. But the breakpoints remain as such. You can go and you can click on the breakpoint to remove them. If there are multiple breakpoints, you can go and clear all.

So this is how it is. So in this case, it is a very simple one. Everything was executed at one shot and we were able to see the results. Now, for example, we will see how the same case is going to be there if there is an error. So say this, we are trying to induce an error here and see what's going to happen. So and see how your command window is going to throw a string of error message here. All that we are going to see.

So the same drill we are following here in the fifth line, we are placing the breakpoint. We are going to give a run here. It goes into the debug mode. One more additional thing that we have to note here is that in the command window, in normal cases, it is just this double arrow that comes. When it is in debug mode, it shows K double arrow. This is something of an indication to any newcomer that we are already in debug mode here. So that part is clear.

So what we are doing next is like it's there in the fifth line. We are going to give a step. You can see data statement is run. You have the array appended in the workspace. Now I'm going to give a step. Yes, we have induced an error and it is throwing an error message. And also you can quickly see here that it has come out of the debug mode whenever an error message pops up. It tells me that there is an error using Excel as the right command.

And what is the possible error? It means that this Excel file student database dot Excel is not writable, which means it is locked by another process. Yes. If you could have clearly seen here, we have opened this Excel as right and we have kept it here. So that's why this error message had come and it is telling that error in file formatting line six. So this is how you can actually identify what kind of error message is this and what it is. And we can go ahead to sort it.

So in this case, since it is a very, very simpler problem, the thing here is like we have closed the Excel file and we try to rerun the script again. Okay. It's a very small one and an easy one. We can directly click on the run button. It is going to give us the expected results. So this is how you actually go down to solve the error messages. And this is how the whole debug operations work in Simulink. So now let's move to the next set of topics.

So this is what I was telling you about while running. The run button changes to a pause button that we can clearly see while we are trying to execute program. While running it, we can see it and examine the values while debugging. Yes, we saw every sequence of operation at every stage. Whatever variable results comes, they all get appended to the workspace so that we were also able to clearly see. Now let's move to the next one. So these are some of the frequently used commands in the MATLAB.

Some we have seen already. Some we are going to see. A few of them are for purpose of knowing what they are. You can read through the material and you can know them. But right at the end, we have a couple of commands, which is get param and set param. They are the most important set of commands. So the idea of the complete syllabus and the course is to develop a model based design models, MBD compliant models. And in order to develop MBD compliant models, one needs to have thorough understanding of how to play with different parameters of those specific blocks.

If you are using any specific library blocks, we need to have a thorough understanding of how to play with them on how to set values to them, et cetera, et cetera. So that that would be helpful for people to have a clear cut understanding of it. So for those, we use get param and set param blocks. Ideally, about get param and set param blocks, we will see in the upcoming lectures of what they are, how to play with get param and set param blocks completely in detail.

We'll have a complete lecture on how we handle with these blocks in specific. Now we'll move on to the next section. This is the last section of this particular lecture. So it is about the functions and callbacks. We have already seen what the functions are. Now we'll see what a callback option is. So if you can clearly remember in one of the older slides from the previous lecture, you can see that the function and the callback options are more or less one and the same, except for the fact that we have a UI control command for the callback function.

And it performs a function kind of operation for any particular callback or we would have to say it performs the function for a specific action. Okay, so here this is majorly used in GUI applications, which is graphical user interface. So as the name suggests, in graphical user interface, we would have a radio button. We would have a text box where we manually enter some data. We would have a radio button. Submit button or a push button kind of a thing. So these buttons usually form the callback here that is defined by these ambison at the rate operator. So this is one example statement or a syntax statement for a push button callback. What we see here is say you are having a push button which performs say an OK operation. So whenever you press the push button, it must display something, a message like OK or something.

So when we are using a push button callback and you define what style it is, what kind of an icon it is, and also you say that this is a callback function here and this is the push button callback function's name. So whenever this button is clicked, it must throw the display message like that. There can also be cases where when you're pressing a push button, it must give a calculated output. So something like that can be there. When you're trying to choose one among different options in a loop, we can go ahead using a radio button, etc, etc.

And if you're trying to ask any input from the user, we can go ahead say in a mathematical calculation or something you want to add two numbers. You will have enter the value of A, you have a text box, enter the value of B, you have a text box and the push button performs addition operation, something like that. So the input asked from the user, the callback would be assigned to a text box and the final addition operation callback will be assigned to this push button here.

So we are not going to go and create a GUI here, but we can see an example of how the GUI function works. So these two scripts here, volume. So the example that we are going to see here is about the volume calculation and how it is done here basically. So first I'll open the script and I'll show you what it is. When you can go to MATLAB and click on a new, you can, okay, this function of MATLAB does not seem to have that. So what you can do is like, I think to open a fresh GUI, you can type a GUI in the command window and you can open it.

So that's how we have created this GUI. And again, as we have seen in the older lectures, you can edit the comments for your convenience here. These are all comes as a part of it. So when you are trying to open or when you are trying to create this graphical user interface, when you keep adding your own text boxes here, how many number of text boxes you add for each, you get a function definition here. This one is a function definition for the first callback. This one is a function definition for the second callback, which is the height, the width, and this one is a function definition for the third callback function.

These three callbacks here that we have is all about these text boxes here where each separate function has been created. Likewise, we have a push button related callback here, which performs the operation here. So the handles are created for each of these functional variables here, which is handles.depth, handles.width, handles.num. And the result here is basically multiplication of depth, width, and height. And that will be followed by numerical to string conversion and display of that particular value in the command window. So this is how, so every time when you try to add a text box or a push button, automatically these functions get created.

We need not worry about what these are. So for a given text box, say you are adding, you will have a callback function and a create function here. So in this create function, I think, yes. So in this callback function, we are going to define what it is. So we are going to define a variable called depth. So depth is the variable name and it retrieves the string value, which is entered by the user since it is a text box. So MATLAB considers it as a string.

You have to do a string to double conversion because only then things as a numerical number and then it enters the calculation. So string means whatever value that you enter here, MATLAB thinks that it is within the quotes. Then the string to double conversion happens. It removes the quotes. It considers it as a numerical value then and it assigns handles to it. These handles are the values or the passing variables for another processing function or callbacks. So once these handles are created here, then it can be called back in another operation.

And that's how it works completely. So this is what these callbacks usually do. So what I'm going to do is like I'll do a simple operation five into six into seven. I'm doing answer is two hundred and ten. Now I am going to since this is a function here for this volume, we are not going to run on this function file. So I'm directly going to enter the values here. The same values I'll give five, six and seven. OK, I have entered the values. And when I am going to click on this calculate volume button, the result output is two hundred and ten, which we have.

So it basically multiplies all these three values and this volume callback here function, which when you click on it, the push button is the callback operation here. It calculates and it will give you a result. So this is one of the very interactive ways of using your function user related operations you can try to work with and you can try to play with. So this is about the callbacks here and with this, we come to the end of the given lecture on the file formatting, debugging and the callbacks are the part of flow control logics is already that we have covered in the earlier slides.

We can probably have a revision of it later. So with this, this lecture ends. Thank you.

Part 4

Welcome to today's lecture on MATLAB scripting. The topics that we are going to cover for today is block properties and different workspaces. Now let's quickly move to the topics. So here in this table, we are going to see what are the common block properties. The block properties that what I am talking here is that we have a Simulink model. We have a lot of different blocks that is used in a Simulink model. Some of the basic blocks are a constant block, a display block, input and output, an addition block, something like that, etc.

So the left-hand side column, the first column called parameter that is shown here, has some set of properties which is common to all these blocks. Say like attributes for match string or background color or block description, etc., etc. So these are some of the properties which is common for all these blocks. So this table here gives you a fair bit of idea of what these block properties are. So if we are going to access NC about the attributes, format strings, it tells you that what this particular operation does.

Background color means what it particularly does. Okay, it is self-explanatory here. It gives the block icon a background color and the properties here shows what are the different colors that a block icon can take. Same goes for block description, a block type, close function, compiled port widths, copy function, delete function, description, etc., etc. So these are some of the block properties that we have. And the idea behind this tutorial is to identify what are the common block properties that we have and how are we going to access these block properties, etc., etc.

So this is the idea behind this tutorial. So we can quickly move on to, so these are some of the different set of properties again. So I can quickly read through them about attributes for match string, background color gives the block description, block type. It tells what type of block is this. It's a text message basically. Close function, it is a close callback, which is basically a MATLAB expression. It closes that particular function. Copy function is about copy callback. Delete function is deletes that particular callback.

Description gives a specific description for that property or for that block, etc., etc. Font size, font weight, foreground color, init function, font angle, a drop shadow, whether you are placing a block, do you want a shadow to be present or not, something like that. And then input ports, link status, load function, block name display, object parameters, what are the different attributes of it, open callback, etc., etc. So this is about, we will see through scripts, how are you going to access the block properties here. See, one thing with MATLAB Simulink is that you can open a Simulink window, you can drag and drop blocks, you can double click on the blocks and you can try to access the parameters, you can change or you can manipulate the parameters.

That is one way. There is also an other way that without opening the Simulink block, that is double clicking and opening that particular block, we can access and handle those parameters. Remember, in the previous lecture, we were talking about a couple of important commands called get param and set param. So these are the basic or the fundamental commands one needs to know and one needs to understand. Apart from that, there are a few other commands as well, which we will definitely know about it. So we will see how we are going to go about and play with that.

So first, we will create a script and using that script, we will see what basically can be done. OK, so quickly we move to the MATLAB. So the idea behind this is to understand setting of a few properties to read what are the different object parameters and the block properties in the command window and to set these block properties from the command window into the model. And the final one at the end of the complete Simulink lecture would be a live demo on how we are going to develop a model.

A complete model will be developed hands on using an mscripting. So that is the final and the ultimate thing here. One important point I would like to mention here is like for this given block, you can see here when you're trying to open a constant block, for example, and see you can have one, two and three. That is the constant value interpret vectors sample time and then minimum, maximum object data type and lock output data. So you have only six or seven parameters that is available readily in hand with it.

But in actual, this constant block has basically available in number of properties that is available with it, which can be manipulated and it can be played with. So we will quickly move on to MATLAB Simulink to know more about this. OK, first quickly, the drill we will do CLC and clear all. So what I'm trying to do here is I'm going for a new script. OK, and at the first set of lines, we are giving CLC and clear all. So MATLAB example for accessing Simulink blocks.

OK, so let's define a variable system is equal to, we'll give a basic name as example.mdl. OK, so before that, what I'll do is like I am going to save this with a name access Simulink block and quickly I can go ahead to even the name, it shouldn't give me any problem. So what I'm going to do is that I'm going to new. Since your system is going to now try to access a block called as example.mdl, it is ideal that we go to create a Simulink model with the name example.mdl.

So we are going to go and create example.mdl here. So I have given, gone to new and I have chosen Simulink model. It's opening. So when you open Simulink, the first it tells that it has its own set of libraries and all that. It has some templates, basically, if you want to create a library model or a project model, et cetera, et cetera, it asks for. But let's not go too deep into them. So the first thing that we are doing here is that we are trying to just open a simple blank model here.

So I'll give, open a blank model. Yes, basic blank model has opened. So this is the layout of a Simulink model. You can see the current name of it. It is untitled. So this icon, which is present in this row, the second row, the first one has some set of options. Second one has some set of palettes. So here there is one icon that is present here, which is the Simulink library browser. So we can click on the library browser and we will choose a constant block, a simple constant block to know more about what these are and others.

Once we move from mscripting to Simulink, we will have a detailed understanding of these blocks, what this library stands for, what these model explorer stands for, et cetera, et cetera. For now, our only scope is to understand what scripting or using scripts, how we can access our model. So that is our basic understanding for the day. So we can go that. So here in this simple model, we have actually opened the library browser and we have added a constant block. Now quickly, I will save this model in the same working folder as example.

And save as type. I will give it as MDL. So it asks in two other options. Matlab usually asks to whether I can save it in SLX or MDL. It asks. In this case, we are choosing an MDL. So we have saved it and I'll currently minimize the same. I'm closing the library browser as well. So now going back to the mscript, I have defined s is equal to example. OK, then open underscore system of sys. All right. Load underscore system of sys. All right. So having open underscore system and load underscore system basically opens the model and it loads the model.

So if you can clearly see, we have closed that example.mdl now. Now, When I am trying to run the script, it opens my model and it loads my model. Fine. So this is done here. Now let's go on to identify different properties that is present with the model here. All right. So now quickly we are going to see about what are the blocks that is used in our example model here. So what I'm doing here is BLK is equal to find underscore system. So this is the name of the command.

So the commands in the line 6, 7 and 9, they are all self-explanatory. That is open, load and then find. So find underscore system of sys, S-Y-S-V give. So once you are giving this command and copying and pasting this command here, it will tell you what are the different blocks that is present in your model. So one, it considers the whole model in itself and the other one is the constant block. So here in this case, you have only one block. So it considers only a constant block.

Now say you just want to do another operation here. So let's have another block. So I'm going for a gain block and let's do a quick multiplication kind of an operation here and let's have, say, a display block. OK, so I have a gain. I have a display. So for this, I am giving a value of 5. So for this, I am giving a value of 2. And when you're running this, you get a value of 10. Fine. So I'm quickly saving this. And now I will run the whole script again.

OK, it opens, it loads and it writes .blk. If you can quickly see here, all the other blocks that are used in the model is displayed in this array, BLK. It's the constant, the display and the gain. So here the names of these blocks, when you click on them, it comes as constant, gain and display. If you can wish to change the names, you can change the names and you can have them. So this is how you using the names of the specific blocks. That is the model name slash block name. So that is how it gets saved in this array here.

So now let's quickly identify about what are the different blocks. So here we have a couple of blocks. We will try to access the properties of these specific blocks. So what I'm going to do is like let's define a variable called OBJ1 using the find underscore system itself. We will see how to access one specific block here. So here you can see find underscore system of sys is actually listing me out all the blocks that is used in this model. So now to access one particular block, what it is we are going to see here.

See here as I type, if you can see the pop up that has come here, it shows find underscore system of name, comma value, system name, value, etc, etc. So these are some of the ways like how we can define here. So we'll quickly go on to define it like this. Sys, comma, I'm going to give the block type, comma, and I'm going to give the name constant. All right. Now I'm going to go for OBJ2, OBJ2 find underscore system of let's have a gain here since we are also using a gain block.

And for understanding purpose, since this is how usually in your office environment, you would have to sit and develop programs about different models, accessing the properties of models is always good that you can write comments against them. So here in this case, you can start writing your own set of comments. You can comment something like access constant block. Here you can comment something like access gain block. And here you can say something like access all blocks. All right. Good. So this is how it is done.

And then we have other important commands. One we have already discussed about it, which is get param and set param. So it gets the parameter, object parameters, and set param usually sets a specific object parameter. Initially, the property value could be one. This time it is going to set it to a different value, something like that. But apart from that, there are a few commands before get param and set param. So they are usually GCB, GCB, H, and GCS. So we will see what they are with the help first.

So I'm going to go to the command window and I'm going to type help space GCB. GCB gives the full block path name. Okay. So currently, what is the active block in your model? That to access or to know what block is there in the model, you go for this. Now, simply, if I am going to type GCB in the command window, it tells that a display is my currently active block in the model. Okay. Otherwise, if you can click on this gain block and again go back and then type GCB, it tells that the gain block is my currently chosen active block.

So why this GCB is significant is like once we know what the GCB is, what exactly the model is, using the GCB function, we can assign different properties, we can assign the handles, we can call the different object functions and handles using this GCB command directly. We will see how we go about with that. Like GCB, we have another one called as GCS. Okay. GCS tells you it gives the full path name of the current Simulink system. So here, since it is only one system that we have in hand currently, giving GCS is only going to throw the name of this model.

If you are having multiple subsystems present inside a single model, it will throw like which is the currently active subsystem here. And if there is any block that is there inside a nested subsystem in my model, it will throw the exact model name slash subsystem name slash the block name when you are giving GCB. And there is one more command called as GCB H. This is again something related to the GCB. It will list out the current handles of that particular Simulink block. So we know the GCB for our model is gain block.

When you are going to give GCB H here, it gives the handle value, which is 4044 is the current handle value, which it gives here. So now we'll see how we are going to play with the get param and set param here. So what I'm quickly doing here is like I'll go back to my script. We will define the GCB, GCS. You can directly define GCB and GCS here like this. Need not assign it to any particular variables. We'll give something as get param of GCB, which is the object that it specifies that particular block here.

And comma, what parameter you would like to access. So here it is the object parameters. So we are clicking on GCB, object parameters. And now let me try to save and let me try to run this script. Again, we can see the model opens. And yes, giving get param of GCB, object parameters lists out all these properties. So this particular block has all the properties associated with it. So if you can go back to this and see we are having this gain block, just click on the gain block and it has only some specific.

OK, one second wait. We will see whether it is the property of the gain block or I think this should be for the gain block, I suppose. So anyways, I'll just click GCB for the gain block and I'll try to rerun this script, this part of line again. Yes. So here we can clearly see that it is only loaded with all the gain block related functions, the name, the tag, the description, etc, etc. The name of the properties, basically what kind of property it is and their structure size, say like it's a one cross one structure, etc, etc.

So those informations are provided here. If we can click on a gain block to see it has only limited options. So gain the signal attributes and the parameter attributes, nothing more than that. But when you can come here and see you have a low. Lots of functions and object parameters that is associated with it. And most of these object parameters and functions we would have seen here. If you can go back to this, you can see about, say, copy function, delete function, undo function, load function, etc., etc. So some of these are relevant. A lot of these are irrelevant as far as the operations are concerned. So yes, it again depends on the individual to make use of which of these available parameters here.

So let's quickly move back to our script and we can identify different handles of them. We'll see how we go about with that. So what I'm going to now quickly do is like we'll see what is the parent of this selected block usually. Usually, so we'll give a command called block underscore parent is equal to get underscore param of GCB comma parent. All right. So this we executed at the last and we'll see. Likewise, now we are going to access the selected block handles. Okay. So what we will do here is like we'll give something like block underscore hand is equal to, there is a function called as get, we'll make use of it, get of, get underscore param of GCB comma handle.

So like the object properties, like the object parameters, we can make use of parent, we can make use of handle, we can make use of a block type, etc., etc., etc. here. So again, there is a one more like block underscore name, okay. If you wish to know what type of block it is, you can give this command, get underscore param of GCB comma block type. So basically, if you wish to know what type property, basically, you can give something like block underscore prop is equal to GCB underscore out data type str.

So, okay. So, okay. The first common question that a person would ask is, how do I know this specifically belongs to this? This is why we have been telling you that you need to have a fair amount of idea on the common block properties. And then you can know using this GCB comma object parameters on what are the different block properties that is available for your given block. And then from that, you can make use of your specific block properties. So, you can here make use of properties like parent, handle.

If you wish to know about the port information, you can make use of ports. Then if you wish to know about out data type str, the gain value, the multiplication value, out data type str is their round method, is their saturate on integer overflow. So, again, properties like saturate on integer overflow, out data type str, they are not visible to anyone explicitly when you double click and when you open the block. Maybe out minimum, out maximum values are there, but out data type str, etc. Sorry, round method, log scale related values are not there. Saturate on integer overflow is not there.

So, these properties, you can define it through script such that it gets appended in your block. See, if you can clearly see here, you have only gain value, min max value, out data type str relates to the output data type block here. You can explicitly set what data type you want. You can even double click, go to this option and you can set or even you can set from your script. So, we will see in the next line how you are going to set that particular property.

So, now here using these, we can get to know about different properties. So, let's quickly type this and see. It tells that it is directly in the Simulink model. So, it tells the parent as the model itself. So, let's see about handles. Yes, so previously, it was only giving you information about what are the different object parameters here, but when you are going for get function of get param underscore gcd underscore handle, it will tell you what is the specific value that it is set. So, the parent is example.

The block type is block. So, the name is gain for it. So, the font size and everything is automatically set. So, parameters like mask variables, mask enable string, etc., etc., is not set here. This is not a masked subsystem. So, it is telling us mask off. So, it is asking for the foreground color, background color, etc., etc. They are all one and the same. Nothing has been set. So, none of these predefined functions has been set here. And so, the block description is again part of it.

Block type is again gain that is also known here. So, all these things are taken care here. See, the output min and output max is not explicitly defined. So, out data type str, if you could have seen here, it is inherit via integer rule. So, I will now quickly, you can see here. So, your out min, your out max, it is not defined, but your out data type str. Okay. Let's quickly change it to uint8. We'll change it to 100. And applying here. I'm closing here.

You try to run the same command. You can see the values are different. This is how using scripts, we would be accessing the different properties. So, here, using get param, we got to know how these properties are. Now, using set param, we will see how you are going to set different properties. Okay. So, say for example, I wish to change my background color. You can see here, the background color is in white. Now, if I wish to change the background color to say something like red, how it is going to be done?

We will see how you are going to do it. So, let's go here. Set underscore param of GCB, background color, red. If you can go back to the PPT and see the background color, it accepts any of these colors. It accepts red, green. The default value is white. That's why it is there inside the flowered brackets for identification. It's red, green, blue, any color you can give. Here, in this case, we are mentioning it as red. And we'll close it. Now, we'll just quickly see this and go back to the model.

It is changed. Likewise, we have integer rounding mode property as floor. We will try to set the integer rounding mode property here. The same command and same drills. Round method is given as floor. I'm just replacing this round method with, we can see what are the different values that is there. You can give ceiling, convergent, nearest, round, simplest, and zero. So, in this case, from floor, we are going to give it to zero. So, quickly give enter and you can quickly go into the MATLAB. And you can see it has been changed to zero.

So, this is how we make use of the getparam and the setparam functions. Using setparam, once we get an idea of the different handles and what are their values of the different handles. First, using the handle function, we can get to know what are the different properties and what usually size that they follow, whether it's a one cross one structure or it is an array property, etc., etc. Then, from that, when we get to know about the get and getparam off. So, gcb, object parameters lists out what are the different object parameters that is available with one particular block.

And then, when we go with like get off, getparam gcp, handle, it gives us what are the different values that is assigned currently to that block. And then, using setparam, we can play with them. We can change the properties with them. So, this is how we access the Simulink parameters through the mscripting. Now, let's quickly move back to the presentation slides. So, this is what we have exactly done here. So, these are some of the different block properties. Compilation of the model, again, can be done at this stage.

So, before compilation and after compilation, it gets. This is what it is shown here. When you close MATLAB and when you open and when you just run the script to know about the compiled port widths and what. Properties are, et cetera, et cetera. It gives us zero cross zero structure. But once you just simulate, you run the model. And when you run the specific property command, it shows something like this. So once the model is compiled or not, it shows with the message is compiled. And before compilation, it shows this message called ready, which means that the model is ready to be simulated. Yes.

So now we are moving to another topic from the accessing Simulink function. So let's close this. So the new topic that we are going to see is about different workspaces. If you can see in one of the older videos, when we were handling the lecture on MATLAB functions, we had put a word that functions do not use the base workspace. So whatever workspace we are working on currently for this application, we call it as the base workspace. That is not a functional workspace. When you are trying to use functions instead of mscripts, functions access and they have their own workspace.

So even every function that you create in MATLAB has its own workspace. Each functional workspace is separated from the base workspace to have a unique set of datas with them. Any local variables that you are defining it there and all only will be accessible within that workspace. Now in MATLAB, when you try to pass on the value from one workspace to another, there are some set of commands that we have. So those commands are basically eval-in and assign-in. So one are global variables, like global variables can be used across different functions as the name suggests. This is something common that we all would have known and we would have learned during C programming days.

But the major thing here is about evaluating in another workspace. When you are trying to call values from a base workspace and assign into a different workspace, how are we going to play with it? That basically is about the eval-in and assignment. So now quickly move to my MATLAB. So here in the workspace, we have some set of values here. So what I am going to do is like I am going to give the eval-in command. So before the eval-in command, let's see what the eval-in here does.

It tells, it evaluates the different expressions in a workspace and returns the output arguments from the expression. So let's simply follow what it does here. So eval-in of first is my base, which denotes my base workspace. And then to know what are the different variables that is defined in my workspace and what are the other objects that is available in my workspace, I am going to give a who. So when I quickly give an enter, it tells me the following variables are available in my base workspace. That is answer, block, block, hand, block parameter, object one, object two, and system.

Now, when I wish to assign the value into my base workspace, or into a different workspace, I go with assign in. So I give a help of assign in. And it tells that it assigns a variable name in the workspace. So WS can be one of the caller or the base workspace. So now I have defined something like this. And so here in the assign in, what we will do is like, we'll define it with a name bvar is equal to assign in of, okay, base, comma, okay.

So what I'll do is, I'll quickly define the eval-in with vars. I'm putting it into an array. So now when I'm going to define here, I'm just replacing it with vars. Okay, it is throwing an error message. It is saying like, too many input arguments, we will have to evaluate what exactly this error message is. We have quickly added a function with eval-in and assign-in. I have given eval-in and assign-in. Previously, they were as standalone commands in the workspace. Now I have assigned them to a function and I have given the name as function of base variable is equal to eval-in and assign-in here.

So now quickly when we are trying to play with this and I've saved it with the name base variable, that is the name of my function. And I quickly try to save it and I run it and see, you can clearly see that b underscore var is created. Here, b underscore var has all the set of variables that is previously assigned and vars, when you try to run it, it will list you out and give you what are the different variables that is present. So this is how eval-in and assign-in here basically works.

So with that, we have come to the end of the current session. So in the next lecture, we will see using script, how we are going to create a new Simulink model, add all the library blocks, connect lines between them, assign values to them, simulate and display the output. It is all going to be a live demo and nothing going to be from the PPT here. So with this, we come to the end of the current session on accessing different block properties, what they are and about the eval-in and assign-in function.

Thank you.

Part 5

Hello, welcome to the last lecture on MATLAB scripting on programmically accessing Simulink. The last lecture that we have seen, that is the previous lecture to this what we had seen was also about programmically accessing Simulink. Here it is going to be a quick rush up on the different commands. There were a few commands that are related to handling Simulink model through the scripts. We got introduced to those sets of commands in the previous lecture. Here we are going to see in detail and elaborately about all those commands.

And apart from that, we will try to open a new Simulink model completely through scripts. We will be writing scripts. We will add all the blocks from the library and do a quick simulation and try to save and close the model. So that would be the objective. We'll just show a live demo of how it can be done, how to connect the lines between models all programmatically. So that we'll see. So before going into those topics, we will see about this functionally controlling the system lifestyle.

So here you can see on the leftmost column, it contains the command name, what is its purpose and how you are going to call them in your script is there in the rightmost column. So new underscore system basically creates a new Simulink model or a library. So it can be something like new underscore system of directly model name. Load underscore system loads Simulink model from the working folder. So whatever folder that is already there, it loads that thing. So it can be something like variable name is equal to load underscore system of the model name.

Similarly, there is open underscore system. Open underscore system here stands for opening a Simulink model, a library subsystem or a block. So it can be open underscore system of model name. Likewise, save underscore system and close underscore system as their name suggests. Does the saving of Simulink model and then the closing of Simulink model. Then find underscore system usually helps us to find out all the subsystems, blocks, lines, ports and different annotations which are there. So as we have seen in the previous example, we can use find underscore system of the entire model name can be used to list out all the blocks that is present in the model.

Or we can point out the specific block type and the block name as we can see in here. So find underscore systems of system means it opens a model name of system.slx and lists out all the blocks that is used in it. Otherwise, if you are giving something like system, block type, gain, it lists out only the gain blocks that are there in the model. Right. So the next command is a BD root. So this currently points to the top level model of the current system.

The current system is the currently active Simulink editor window. So in which the block is selected. So that is with respect to BD root find underscore MDL refs. So which means it identifies all the model reference blocks which is being used at the model either at all the levels or at the top levels. So to know more about what model reference blocks in the others, we will be seeing about them in the Simulink. So once we get to know what basically model reference blocks are, we can bring these into the picture and we can try to connect with them.

So next one is the file parts which gets the part of the file name, builds the full file name from the parts. GCB as we have seen in the previous lecture, gets the path name of the current block or the most recently clicked block. GCS is the path name of the current system. GCBH is the gets the handle of the current block that is in usage. M file name. It is file name of the currently running code. Get param and set param. We have already seen about this in the previous examples and the previous lectures.

They return the name or value of the specified parameters for the specified model or block object. Opens or loads the Simulink model. Basically, if you're giving something as get param functions. Set param functions. It sets the block parameters to a particular block or a subsystem, etc., etc. UIGetDIR, it opens the folder selection dialog box. It gets the block handle of the Simulink blocks. ADPATH, it adds specific folders to the MATLAB directory path and then the search path. SIM, it helps us to programmically simulate the model and then model compile option.

It executes the compilation phase of the given model, etc., etc. So, here it is shown with the example of VDP, which is where the VDP is the model name. And using the model name, we can directly compile and we can simulate the model here. So, this is about it now. So, let's quickly jump onto the MATLAB tool. And in the MATLAB tool, hands-on, we will try to create a Simulink model programmatically. Okay. So, the drills remain the same here. It's going to new. We are going to the script.

Okay. As it is the first set of lines, CLC and clear all. And we'll give it as creating a new model. Let's give the name as sys is equal to test model. New underscore system of sys, open underscore system of sys. So, basically, these commands helps us to create a block and to open a block. Okay. So, let's quickly save this. And we'll keep it as example and it's eighth lecture. So, I'll put the name as example eight currently. So, what I'm doing is I will try to now run.

It is running. You can clearly see it's in pause. It is still busy out here. So, it means that to create a new model, that process has been initiated. You can quickly see the prompt changing left-hand side bottom of the screen here. It is showing as starting Simulink. So, which means that it has accepted the command and a Simulink model is now going to be opened and it is going to be saved in the name as test model. You can clearly see here test model has been opened and it has been saved.

This is visible at the top left corner of the model. So, which means that now using the command, we were able to open it. Now, the next stage is here. Like, we will try to add a few blocks. Right. So, the objective here for us would be to add some set of blocks and for us would be to do a basic something like an integration or a sine wave generator. Like, for example, we'll try to build a sine wave generator model. So, we'll try to add those things in the model.

So, for that, what we have to do is we have to go with the add block commands. So, we'll see what the add block commands does in first place. So, we will give help space add underscore block. So, it tells us how we need to add them and all. So, add block is source, destination, parameter, value, etc., etc. So, in this, what it does is like it creates a copy as above in which the named parameters have the specific values. Any additional arguments must occur in the parameter value pairs.

Okay. So, this is something like this. Okay. Let's assume this example that they have given here. The one that is highlighted here. Add underscore block of built-in slash gain, comma, my model slash volume, comma, gain, comma, core, 4. So, here it copies the built-in gain to a block named volume in the simulink model named my model and it assigns with the gain value as 4. Okay. Apart from assigning the values, we can also position the models appropriately. Okay. See, if you are trying to add the blocks in the model, if they are added one after another or one above another, usually while trying to run the script, it does not give a very, very pleasant look.

So, what I have done is like I can quickly show you a demo of how to now linearly add them. Okay. So, for that, let's take the x, y, z coordinates and also some set of offset values that we are taking using which we will position them. So, I have also written a small script related to that, which I can quickly show. So, I'm just adding the comment here as adding blocks and lines. So, let me give the value as x is equal to 30, y is equal to 30, w.

U is equal to 30, H is equal to 30, and offset is equal to 60. All right. So now we will write a small positioning related formula here, which is X, Y plus H by 4, X plus W, Y plus H into 0.75. Okay. So just don't get confused upon how this formula arrives. This was basically written and developed on the basis of trial and error, which helps me to position the blocks in an appropriate fashion and in an appropriate manner. So each time with respect to the positioning of one block. So X and Y here are the coordinate values and W and H also correspond to the coordination part here.

So based on that, only my model. So basically from this top corner, it considers the X position, Y position, and then it places the blocks here. All right. So now let me start adding the different ports. So this is with respect to addition of the first block. So this is the position which the model must understand and must place its first block. So what I'm giving is I'm going for the add block command, add block of built in, can clearly see it here, built in slash subsystem.

So something similar we are doing and giving it as built in slash. We don't want the subsystem here. Let's keep things very simple. So I'm going for an input, comma. It is system slash IN1, which is basically the port name. Okay. And then we are defining the position of it and the value POS, which is calculated in the previous line is what we are using here. So this is with respect to the addition of the import block, which is at the top most layer. All right. And then next, what I'm doing is like we are going for adding, say, an integrator block.

Now you just commented for understanding purpose. Next, I'm going and we are adding an integrator block. So I'm typing it as add integrator. And what I do here is see if you are going to copy and paste the same command, the first block, I can just quickly show you a demo. See, if I'm trying to execute the line number 17 and 18 together, what happens is it goes and it adds the input, say, in this position, it goes and it adds my input in this position that the position is again, as said, it is going to be determined by the top most position, etc, etc.

Now, if I repeat this line number 17 again, before adding another block, MATLAB would take and place the corresponding second block on top of the first block, it will be something like this. So it does not give a very clear picture of whether your blocks are present or not. So it will give a very, very unpleasant look. So for that, we have again calculated and we have written the POS command such that it is placed to the right side of the existing import block. So it is buried. So it is placed to the right side of the second block. So it is placed to the right side of the second block.

So just don't get confused about it. So this is how these calculations are. We have this offset value which gets added with the x-coordinate and the y-coordinate here. So we are writing the command here. All right. So POS is added. next we are adding the integrator block here so what I am doing is we can just use the same line the only thing we have to change is that the input is now replaced by an integrator and this is now going to be renamed IN1 is going to be renamed as like INT1 so now you have two blocks the the third part of the challenge obviously would be to add a display block but before that we'll try to connect these two lines before so for connecting the two lines before there is a command and that command is called add lines so let's quickly see what add lines does here so you can just go up so it is add line sorry so it add a line to the simuling system so the command name is add underscore line so sys comma oport comma iport adds a straight line to the system from the specified block output port oport to the specified block input port iport so and oport and iport a string consisting of the block name and the port identifier in the form of port this is how this command is going to work and this is how we are going to add this right so let's go to add line here so I'm giving it as add underscore line of sys which is the system name then we are choosing IN1 slash 1 okay comma INT1 slash 1 okay so we have found out our output and our input here basically so here if you can see the IN1 has an output connected to it and INT1 has an input or receiving port connected to it so basically in this order we would have to give the lines and then we use this command of auto routing auto routing automatically identifies both the ports which is present in the model and it completes the connect line part so okay auto routing comma on all right so we have given the auto routing here so the next challenge here is to add a display block which is at the final end which is going to display and show me the result so again for this I have already calculated the command so I'm just going to type that command need not get confused by what it is so which is basically x plus offset into 2 y x plus offset into 2 plus w sash space y plus h right okay now we are adding the last block here which is going to be my display block here I'm just replacing I've just copied it so import name I'm changing it cope and all right so built in scope system slash scope position pos done now we have to add the line between the output Of the integrator block to input of the scope block. Okay, so add line of sys, int1 slash 1, okay, comma, scope 1 slash 1, comma, auto routing, comma, on.

So that's how it is named here. And the next one is like, okay, so this is done. So apart from adding your blocks and connecting the lines between them, there is also an option of replacing the blocks. Okay, so here if we wish to replace any block, that also we can do. So a quick demo of that we can also see here. So the command for replacing a block is like replace underscore block. So once we give a help command for replace underscore block, it tells us how the whole thing would be done.

System, block type, and then the new block here. So the system and the block type, it's say if you are going to replace an import block with a sine wave block, it is going to be something like the block type would be IN or input and the renew block would be something like a sine. And then the whole thing is done here. Let's see how the replace block, let's see how the replace block part command is going to work. So it is something like replace underscore block of sys, name, comma, IN1, comma, built in slash sign, comma, prompt.

So there is a command called no prompt that is used in this blocks. So the no prompt command usually does not help us to make any dialog blocks display or not. So once you add this no prompt command, it does not display any dialog blocks and it does not also return any kind of arguments at the output. Otherwise, it is going to keep throwing some strings in it. So to avoid that, we use the replace block. So that explanation about that can also be seen here to suppress the dialog blocks from being displayed.

We add this a no prompt block. Right. So once after this no prompt command is added here, let's go to the final piece of it. So what we quickly do here is that we write a set param function. So inside the set param function, we take the, okay, we are quickly replacing it at the ideal position. So X space Y space X plus W space Y plus H close, comma, name, comma, sine wave. All right. All right. So fine. We have replaced the blocks in terms of position as well.

So at the last thing that we have to do is like, let's save the subsystem. All right. Save underscore system of system. That's the variable name that is being used. And alas, we have this model available. So what I'm doing is like, I'm quickly closing the test model and I'm also deleting this test model. All right. Okay. So let me run this script. It indeed takes some time. That's it. It has opened. It has created the blocks. It has added them and it has been saved in the current location as well.

So this is how this works. So by running this directly, it adds over here. Say, for example, I'm, let me just simply for understanding purpose, I'll just comment all of these things here and let me just copy this and paste it over here. Now, what I will do is I will close this. I will delete and let me run this again. You can clearly see how badly the blocks are placed here. That is one after another. Right. The same positional values are being used and you can see something like this.

It does not even show that an integrator block is actually present in your system. So that is the reason why we have given some, we understood and identified the coordinates and based on the coordinates of X and Y, we tried to add the offset values to them and then we multiplied it by two so that there is an equal distance that is present between the values. So this is quickly about the how using MScripting, we shall develop the models and blocks. So with this, the complete MScripting lecture comes to an end.

So from first, we have seen about the different data types and then what are data types, the basics of how does an MScripting about introduction to different commands and then different data types and then about what is integer, what is a float, what is the strings, tables, character arrays, and then the if loop, while loop, for loops. And then from then on, we started slowly moving into this data manipulations using MScripting, basically about this XLS read, write options. And then the functions and the callbacks, we also had a quick demo of GUI, what, how the callbacks are used with the examples of GUI.

And finally, we showed a complete demo of creating a Simulink model using scripts. So with this, the lecture on MScripting is completed. The next set of classes will be continued with the basics of Simulink. Thank you.

Want the rest of Introduction to Model-Based Development using MATLAB and Simulink?

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 Introduction to Model-Based Development using MATLAB and Simulink course

The remaining 9 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.

Grow by Skill-Lync · free lesson

Start watching

Free, no payment. Leave your details once and every free lesson on Grow plays straight away. A Skill-Lync counsellor may call you about the full Introduction to Model-Based Development using MATLAB and Simulink course.

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.