Interacting with a Simulink Model from a Matlab Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to another video today I'd like to talk about interacting with a Simulink model from a MATLAB script so in essence we'll talk about how to setup Simulink model parameters and execute the simulation model from a MATLAB script in addition we'll look at how to import an export data between MATLAB and Simulink so the two can be used synergistically in terms of a more formal agenda let's first start by talking about how to define simulate model parameters within the MATLAB workspace related to this is wolf take a gander at how to run a simulant model from a MATLAB script once all that's running we'll take a look at how to send simulation data back to the MATLAB workspace using a variety of methods such as a out one block a - workspace block as well as logging the signal finally we'll look at how to use MATLAB data as input to a simulated model we'll do this using just one method namely using the from workspace block before we jump in a Simulink I'd like to revisit the model we built in our previous video entitled ordinary differential equations and dynamic systems in simulating and use this as a motivating example needless to say if you haven't had a chance to watch this yet please go through this tutorial first before continuing further you can get to this video via the link in the description or via the link that is just shown up on your screen now if you recall the system we considered earlier was a simple pendulum with some viscous friction at the pivot as well as an input torque at this same location the linearized differential equation governing the system was shown to be this now in the past video we assumed these constants which led to a simplified version of the differential equation we then built a simulate model to numerically solve / simulate the simplified differential equation now while we were able to get some good results using this model this approach should have raised some red flags especially to those of you with a software development background namely we hard-coded these constants into the model so if we want to go back and change parameters of the model like the length or mass of the pendulum it would involve hunting through the Simulink model and replacing all the appropriate values in the various blocks scattered throughout the model so if you think about it the generalized version of the differential equation I'm circling up here in blue is vastly superior so why don't we try to implement this version of the equation in simulated where we use variables to represent the constants rather than the version with the constants already hard-coded into the blocks all right so we've got MATLAB up and running and in the interest of time I've gone ahead and just recreated the model that we built in the previous tutorial so this is the one that we were discussing here but we notice here that the constants are hard-coded into here um let's just double check to make sure that this runs here so I'll go ahead and get run and look that ran and here's our first hint of where this might be able to help us namely if we go back to Malibu you notice I just ran the simulate model here and notice that it actually created a variable in mymathlab workspace so it seems evident that Simulink is talking to MATLAB maybe it goes the same way maybe it goes MATLAB can talk to simulate so to do that why don't I come back here to this pendulum model and let's try to go ahead and implement the improved version or the generalized version of the differential equation so if you remember I think this gained right here instead of being 1/5 this should have been C over the quantity M times L squared and let's hit OK and maybe let's make this gain a little bit bigger so we can actually see that value great and similarly this game instead of being 10 it was actually G over L here right and then finally I think one more thing I think there was supposed to be a gain up here which actually ended up being just a 1 with those constants that's why we don't have a block here but in this case we do need a gain so I'm just gonna go ahead and insert a gain here of the appropriate value so the appropriate value that should go in here I believe is let's go ahead and open this block up this should be 1 over the quantity M times L square all right so there we are again let's open this up a little bit so we can see that it's in there and we see right now Simulink has variables to represent all of these constants here so let's go ahead and save this model here and if we get run as you can probably imagine we get a whole bunch of errors here because it has no idea right what C G M L any of these constants are here in my model so obviously it's not gonna work now the way we're gonna solve this here or the interesting behavior that we should observe here is that when you hit run on a Simulink model what it's going to do here is it's going to come back and look for those variables anything any variables in the model here in the MATLAB workspace so since it didn't find it here through that error so in order to fix that all we need to do yours let's come over to MATLAB and start ourselves a new script and in this script let's go ahead and define all of those constants that we needed our simulate model alright so let's come here to my script here and all maybe you say this is a script to illustrate how to interact with a Simulink model from a MATLAB script let's make this a little bit bigger just so to get the line break correct okay so again let's go ahead and start with our usual clear CLC close all and what we saw right now is what I would like to first do with this script here is define model constants so what I mean by that is we should define it here all of the constants that are needed in that model so I think gravity we were using a value of 10 maths for using a value of 1 C was a value of 0.2 and the length of this was a thinking value of 1 so let's go ahead and save this file here and what we should do here is we should save this in the same location as our Simulink model just out of good practice and I'll go ahead and maybe call this um how about let's just call this MATLAB script to interact with Simulink model I know that's a bit of a long verbose name but this is really what we're trying to do we're trying to make a MATLAB script that's gonna interact with that Simulink model so if I go ahead and save that again let's just double check yep here it is I've saved the script in the same location as the actual model that I'd like to interact with here the reason I like to do that is so that when I go ahead and hit and-run and I change my folder to that directory it's gonna be in the same directory as the model here so if I go ahead and change the folder we see now that what MATLAB has though with our script here is it's defined those variables in the workspace so now if we come back to our Simulink model maybe let's just resize this a little bit so we can see everything together earlier we saw that there was an error when we tried to execute it but now what we do this what we're gonna do is we're gonna hit play Simulink is gonna realize that there are a lot of variables here in this model it's gonna look in the workspace hopefully find those values here and our model should run so let's give this a shot so I'm gonna go ahead and play it look at that it ran so if I come here to my scope and yep this looks totally reasonable and now we see the power of defining these variables here in a script if I want to change something like I want to increase the damping like how about here I just need to come back to my model change that number run it again and come back to my Simulink model hit play and I should have some new results with a new damping and yep look at that though the system seems to damp out a lot quicker here so this is a variable very powerful technique and you know what you can change more than just simple gains and things like that so for example in the simulation parameters button we can do all sorts of fun things here so for example instead of stopping at 30 seconds we could go ahead and say how about T final and you know and if we come back to our scope here we noticed this was fairly jagged here if we wanted to smooth this out we could try to tell simulate you know what don't take such a big step here if I come here to my simulation parameters again I come in to additional options in the max step size maybe I'll call this how about tea mat tea and step max right hit apply hit okay and again all I need to do are doing it is to find more parameters here so I think we had something like called T final let's maybe make this how about 45 seconds here and how about T step max will say don't take anything bigger than a 0.05 second step so now again if I run this script all those should be initialized so if I come here to my Simulink model hit save and hit run we should have this change so if I open up the scope you look everything looks and a little bit smoother seemingly it's taking smaller time stuffs and it's running all the way out to 45 seconds we can also do things like change the initial condition so why don't we come in here to the integrator so in this integrator if I double-click on the integrator block instead of having an initial condition of zero let's just how about put theta zero and similarly for this integrator instead of zero for theta dot we'll say this is theta dot zero hit OK save and finally we just need to come back to our script here and start defining these variables so maybe let's have the pendulum started how about negative 30 degrees negative 30 times PI over 180 and how about theta dot 0 la sure we can leave this at zero for now ok run this script again and come back to our Simulink model hit run and again here we are starting at negative 30 and things are going well until the step function comes in at 10 seconds and pushes the block a little bit further so this is a great way to define model parameters here so you have a Simulink model what you should be doing is using variables everywhere for block parameters for simulation parameters for initial conditions and then before you run the simulant model you should just have a MATLAB script whose sole job in life it is is to initialize all of these variables so that they appear in the workspace before you start the simulation now the one problem with this workflow is that we had to run the MATLAB script go over and open up the Simulink model press play on the button here and then take a look at the scope here in order to see any results now in this day and age of automating everything wouldn't it be great if we could execute the Simulink model directly from the MATLAB script well the answer to that is yes it would be great and luckily there's a command to do that so what we can do here is if we go ahead and use the sim si M command and we pass it the name of the model so let's go grab the name of the model here I think oh we called it pendulum model dot SLX pass it to that as a string here what this is going to do is this line will execute and the model here so to illustrate this why don't we go ahead and you know you know let's change the initial conditions maybe let's go back to to zero everything here and maybe let's um let's buy an undock so that we can see both this the script on one side and then maybe let's put the scope here over here now let's go ahead and I'll put a breakpoint like such let's go ahead and run this and now we're sitting here waiting on line 20 so when line 20 is executed the Sim command what that's gonna do is basically the equivalent of us going over opening the model pressing the play button here so let's go ahead and continue here and hopefully to keep an eye on that scope here and there you go right you see that it was executed and the simulation was run in fact the simulate model doesn't even have to be open let's go ahead and close the entire Simulink model here and I'll take away the breakpoint and you can run this again here and it will execute the model of course we're sort of taking that for granted right now because the Simulink model is not really sending data back to matlab at this point so there's nothing for us to really see being generated by the Simulink model so why don't we go ahead and fix that now all right so to fix this let's first go ahead and redock the editor here into matlab here and let's go ahead and reopen that Simulink model here so maybe I'll just come here to current folder and I should be able to just double-click on the Simulink model to open it now what we would like to have happen here is I would like it that when the simulate model runs I would like to send some of these signals back to MATLAB here the easiest way to do this well actually no I take that back one way to do that here is let's just come ahead and open up our library about browser come here to syncs here and let's look at using this block here it's called an out one here whose just called out one because this happens to be the first output so if I for example want to send theta back to the model let's just go or back to the MATLAB workspace let's just branch off the theta connect it like such and save the model here now tell you what let's close some of this here and actually you know what we can close the entire Simulink model if we'd like close the Simulink library browser let's again come back to the workspace I'll maybe make this a little bit bigger here take a look at all the variables that are created here right now now what I'm gonna do here is let's run this one more time we're gonna see one more variable is going to be created is called y out and that is what's being generated by that little out one block that we created and added to our model so again if I run this it will run the script and when we hit line 20 it will execute the Simulink model and hopefully the simulate model will send data back here to the MATLAB workspace in the form of that Y out variable so let's go ahead and hit this button right now to run the script and there it is this thing called Y out was created by simulating so let's take a look at what this is and we'll see that Y out it's actually a somewhat complicated data structure here so what we need to do is actually just extract data unpackage this data here so let's go ahead and maybe I guess I probably should have commented it here what we were doing here is run the Simulink model via our using the sim command and now that we've run this we want to extract the data generated by the Simulink model right so it was sent back to the workspace so all we have to do is extract that here so the the syntax for this is a little bit esoteric here um you'll notice here let's go back and take a look here at the Simulink model here maybe I can close this you'll notice that this Y out signal was hooked up to the theta signal and the theta that signal was named theta here so when we come back here we noticed that you know what there's one element and it's called theta so in order to do this what we do here is we're gonna first go ahead and extract the the signal itself maybe let's call this theta signal and what I'm gonna do is I'm gonna say Y out dot get element and what element do I want I want the theta element here okay and then once I have that I can extract the time by just saying something like they signal dot values dot time and now what I can do is I can get out the actual theta value that corresponds or line up with those time stances timestamp T so I'm gonna say theta is theta signal dot values dot data okay and I'll put a semicolon here and let's go ahead and run this and again I should come back here to the workspace and now we see we have theta and time there are nine hundred and three elements long here and what I can maybe do now is why don't we plot those to see what's going on so if I say figure plot T theta X label should be t in seconds and the y label the y axis should be theta in radians maybe we'll turn the grid on to make this thing look pretty and maybe as a title we'll say data generated by the Simulink model right let's save this and now if I run this this is actually very exciting because look at that we are now getting data back from the Simulink model right this trace was generated by stimuli but now it's here in MATLAB right we can now use this to do analysis or other interesting data processing so for example you know what if we wanted to find the maximum value of this this signal here back in Simulink that was very hard to do here right because that one involved you just you know just eyeballing it off of a scope and now that we have the data in MATLAB we could actually go ahead and do that so for example if I want to find the peak here let's go ahead and do that now now that I've got the signal I could do something like just say you know theta Max and the index at where this happens I'm just gonna use the max function here I'll look for max of theta here and then I'll maybe the time that this occurred here is time at the index Max and now what I can do here is let's come here and I'll turn hold on so I'll plot both the signal and I'll go ahead and plot the when the theta max occurs let's again plot this as a red X marker size 14 again if these are not familiar to you all this plotting commands were covered in our 2d MATLAB or our 2d plotting with MATLAB tutorials so I'll leave a link to that in the description and you should also have a link on your screen right now to watch that if you're interested but again let's um oh you know what one more thing let's add a legend here how about so how about let's say the first one is theta and then the other one next one is the maximum value of theta so now this should be aha look at this this is really really exciting right we're able to now identify maximum minimums we can do all sorts of other sort of analysis as well um all right so let's come back to the Simulink model and we saw that the way we achieved this year was using this out one block I would like to show you a little bit more syntax while we're at this um because in a lot of cases you may want to log more than one signal or you may want to log actually vector based signals so what I mean by that here is tell let's delete this signal right here and let's go ahead and grab a MUX here so I'll come here and oh my Simulink library browser here and I'll get my signal routing and find a MUX block drag this in and again a MUX here should be able to concatenate multiple signals together so what I want to do here is I want theta as the first input to this and maybe how about theta dot so I want the position and the velocity of the block here so now let me hook this up here and now this whoops let me line this up here what we're now logging is a vector maybe more aptly called let's call it X for the entire state vector of my system theta and theta dot here and one thing that might be helpful to realize that this is now a vector here is let's go ahead and turn on the display here to show the signal dimension so I'll come here to display signals and ports and I'll click on signal dimensions and we see a to show up so we see right now what we're actually logging here or sending back to the MATLAB workspace is a two element vector here right it's theta and theta dot here so if I do that and come back - my MATLAB script I see I'm gonna have to make one small change the small change in syntax now is that data is actually gonna be a matrix it's not gonna be a vector any longer in fact it's gonna have two columns here the first column is gonna be the first element of that vector the second column is me the second element of the vector so I just need to modify this to say okay theta is actually the data here but it's all rows column 1 here and now theta dot is this same thing let me maybe get the white space a little bit cleaner right except it's all rows column 2 okay so now what we can do here is um again let's go ahead and convince ourselves of that maybe what we should do here is let's start a subplot maybe 2 by 1 by 1 here and maybe the next one here 2 by 1 by 2 let's go ahead and plot T and theta dot here and again we can go ahead and pretty up the plot if we like so X label is again T but now this the Y label is actually it's D theta DT right it's theta dot in terms of rads per second so now if we run this whoops oh sorry I um yep I need to modify this so if you remember right we changed the out one block to be hooked up to not the theta signal anymore but I'll stable to a signal that we labeled X right so if you come back here and look at the the model here right this is now hooked up to the signal called X which was our vector here so let's go ahead and change that here to get the X signal so if we run this again aha here we are now this is all working so now we've got the the position here as well as the velocity as well successfully being exported from the model so that's great so that shows how to use the out one block to save either a scalar or a vector signal from Simulink and send it back to MATLAB let's look at a second technique to do a very similar operation here all right so let's go ahead and again open up the library browser here and if we come here to sinks here you'll see there's a block here called - workspace let's grab and drag and drop that into our Simulink model here and what we're gonna see here is I can go ahead and double-click on the block and let's change the variable name to maybe be called something like sind X here now while we've got the parameters open let's go ahead and take a look down here in the save format notice that the default one is time-series if you click on this you'll notice there's actually multiple different ways you can save this but let's start with just the default time series so again let's go ahead and just hit OK we've now got this - workspace block let's just go ahead and hook up the signal X to that save our model and now come back to MATLAB and when we go ahead and run this and excuse me I should come back here to the workspace here we notice here now that there's this variable called SIMEX here and we see that again SIMEX is similar to the Y out signal in the sense that it is a complicated data structure here so all we need to do here is just unpack that data to get at the signals that we want so let's go ahead and maybe um put in a couple of notes here oh and actually gosh I just noticed here um maybe I want to change this code I want to make sure this is all appropriate you you remember this is our previous version here where we were unpacking the data from Y out I better call this now let's call this X signal all right and I'll hit shift enter to replace all the instances of that just to make everything consistent here okay now I know again that was just a little bit of me being a little anal retentive to make sure the code is clean here because what we're trying to do right now is actually I don't want to be using the Y out data here so I'm actually going to comment this entire block out instead what I want to do is I want to get theta and theta dot from this sim X variable that we obtained using the - workspace block so maybe what I'll do is I'll write a little note here and I'll say extract the data generated by the tube workspace blog and what we can do here is we can contrast that here with extract data generated by the out one block is maybe a better description of that comment block above okay so in order to do this all we got to do here is go ahead and extract the data here so the scent again it takes a little while to just figure it out but it's not that bad here so I'm just gonna say T is sim ex dot time and theta here is gonna be sim X dot signals or sorry excuse me dot data all rows call them one here and theta dot is gonna be sim X dot down up all rows column two again because we saw that X was a vector right it's a 2 by 1 vector and let's just go get the white space all lined up here so we can see everything cleanly great so now if we run this we see that we get the exact same results right the data is now being sent back to the MATLAB workspace using this 2 workspace block ok so before we leave the 2 workspace block maybe what we should do here is let's come back to the model and I just want to show you here that you know there's multiple ways that you can save the data we saw here that earlier it was of at the time series there's other options on the one I actually used like quite a bit as this structure with time I use this for a lot of my models just cuz I'm a little bit more familiar with it here so if you change the save format to say structure with time all we're gonna have to do here is just change the way that that sim out variable we extract the data from that sim out variable so what I mean is if we run this this is all be not gonna work because the data structure of sim out has changed here so if you take a gander at this thing you see it's just got a little bit of different formatting here so all we got to do is extract that so what we should maybe do is let's first expand this comment here so this code right here is how to extract data generated by the to workspace block using a time series write structure and now what we want to do here is let's get code here that is going to do this exact same thing where we're gonna extract data from the to work space block but instead this is now using a structure with time format right so in this case all you do here is the time is now sim x dot time here sorry excuse me lower case time and then theta is gonna be sim X dot signals Dodd values all rows column one here and theta dot is gonna be sim X dot signals dot values all rows column two okay and again let's line up the whitespace so that all of our code looks nice and clean and we see here this is just a slightly different save format so I'll go ahead and let's comment out the previous block here cuz we don't want that to execute I want this one to execute now so if I run this again we see we're back to the exact same spot here where we've got the data being sent back to the MATLAB workspace now from the two workspace block but using two different structures right you can either use the structure with time format or you can use the default time series why don't we go ahead and switch this back to the default just cuz that's probably what most people are going to be using with their models now it's gonna be this default time series so I'll go ahead and hit save save and then again comment out this block and uncomment this and run again and again we're yep we're back to where we were okay finally let's look at one more way that we can go about sending data back from the Simulink model to the MATLAB workspace the third and final method I'd like to talk about today here is called signal logging so to do that let's come back to our Simulink model here and to log a signal what you want to do here it's actually sort of a a multi-step process what I would first recommend you do is take the signal that you want to log and make sure that you've named it so in this case we have name but you see we've given it a name X that's perfect now once we have a name what we can do is right click on the signal sorry I try that again right click on the signal come down to properties and select here you see where it says logging and accessibility just click it and put the check mark here on log signal now the reason that we wanted to make sure we named the signal first here is because you see here when you're logging the signal the default option is to use the name of the signal when logging the data back to the MATLAB workspace of course you could use a custom name if you want here but I think that gets a little bit confusing very quickly here so I would like to keep the name the same as what I've named it in the Simulink model so I'll go ahead and keep this as a default I'll go ahead and hit apply and hit OK and notice here that you get this little icon it looks like this little wireless streaming icon here on the signal that's simulate telling you that that signal is being logged or saved here the other place that you can tell that that's happening here is now that I've been enabled this signal to be logged let's come up here to my model configuration parameters and when I come to the data import export side right here you'll see down here in signal logging here this now has a checkmark and you see what its gonna do it's gonna create a variable called logs out in the MATLAB workspace here and if you go ahead and move a little bit further to the right you say configure signal so long if you click on this what we should see now is check it out here it is here's that signal X that we set up earlier and it's now set up to be logged and sent back to the MATLAB workspace so great let's go ahead and close this and we can close this as well here oh maybe one more thing while we've got this thing open here you might have remembered earlier when we were using the out 1 block and it was creating this variable called Y out well here you see exactly where it came up with that name here right and we also saw that here's that T out that's automatically being created when you run the model here so in this data import/export pane of the model configuration parameters there's a lot of stuff you can configure here to get data back from the semantic model back to the MATLAB workspace but again let's leave everything default here for this tutorial so I'll go ahead and cancel this because we actually didn't change anything we just looked at things here and now then we go ahead and save and run the model here and we come back to the workspace we see here that we've got this variable called logs out which again is a complicated data structure here now we just need to extract the data from so let's go ahead and do that right now so again let's comment this section out we're not using the data from the two workspace block anymore I want to use the data from the logs out object what's actually really nice about that is the safe format is very similar to using that out one block so I can just go ahead and copy this entire block of code let's come down here and say I want to now extract the data generated by the signal logging technique alright so I'll paste that here let's go ahead and uncomment this section here all I got to do is basically change the name of this instead of using Y out I'm gonna use logs out okay so if I run this we should basically be here again there's the third way to skin this cat right is you can now use the logs out meth in here alright so that was covering how to get MATLAB from the Simulink model into the MATLAB workspace what if we want to go the other direction namely what have we have data that we've loaded into the MATLAB workspace and we would like to use that to drive the simulation or be part of the input of the model so in order to do that let's come back to our Simulink model and again open up our Simulink library browser and instead of the sinks let's come here to sources and if you scroll through this you'll see that there's a block here called a from workspace if we grab and drag and drop this into our model what we're able to do here is we can double click on the block and what we're gonna do is create a variable in the man that workspace called in this case how about Sim T 4 this is the torque so again I apologize for the nomenclature lower case T is gonna be time upper case Clea it's gonna be the input torque to this system right so if I get ok here what I would like to be able to do is let's make this model a little bit bigger so we can get a little bit more real estate here is instead of using this boring step function what if we had this coming from the MATLAB workspace the torque was something more interesting like say a ramp followed by a sine wave so you obviously could cobble that together with a whole bunch of simulate blocks but it would be really nice if we could load that in from the MATLAB workspace so in order to do that let's first go ahead and maybe let's just put a switch here first we can choose which one we would like to use so I'll grab a manual switch block here and let's just disconnect this so hopefully this will make a little bit more sense once we get everything can necked it up here okay and then I'll connect this block like such sorry click here click there there we go so now I can either use the boring step function or if I turn the manual switch down here I can use the input coming from the MATLAB workspace as the input torque to this system okay so let's go and save the model and let's come over to the MATLAB workspace here and create this variable called sim T all right so I'll come over here and maybe let's scroll up to the top here and before I run the model here let's just go ahead and start a new sections here and call this create an input called CIMMYT Eve which will be used as input to the Simulink model okay so let's go ahead and do that now so in order to do this I need to make this variable called sim T that has a very particular structure and that structure is it just needs to be a matrix with two columns the first column needs to be the time stamps and the second column needs to be the Associated value of the signal at that given time stamp so let's go ahead and do this now so I'll maybe make a variable called T and this will be the time so let's make this linspace maybe go from zero to a t final and maybe let's use 30 steps here and the one slight nuance is we're gonna need a transpose here because linspace is gonna give me a row vector here and i actually want a column vector here so T in is a just a column of all the time stamps and now all I need to do is create another variable maybe let's call it capital T in for the actual torque at each one of those times here so let's go ahead and first make this just a whole bunch of zeros the same size as T in and what I want to do is now I want to fill in all of this with whatever custom values I want so you might be able to load this in from a data file but in this case to keep everything self-contained why don't we just procedurally generate what this should look like so let's just do something like how about for K gets 1/2 length of T in let's go ahead and make this again we'll make it a ramp for the first ten seconds and then after that we'll maybe switch it over to a sine wave so we could do something like if T in sub K is less than 10 then the actual value of the signal or the input torque here is just gonna be the time here right so it's just gonna be a ramp with slope 1 right and then else if the time is greater than 10 seconds let's just make it a sine wave so how about T in sub K is just gonna be sine of T in sub K something like that so that should be pretty simple let's just go ahead and check this input for for completeness so maybe let's just say figure plot T and versus T in here and we'll just go ahead and put a breakpoint here we won't run the whole thing I just want to double check to make sure we created that signal properly and yeah here we go so for the first 10 seconds it's a ramp and then we go into this very coarse sinusoidal input I guess maybe this is a little bit too coarse for our liking maybe we should increase this instead of 30 steps let's make this how about 100 let's quit debugging and will rerun again just to make sure that this seems reasonable there we go okay so here we go we got a ramp and then we got a sine wave input for the rest of the time so this looks beautiful so we've got these two signals put together so all we need to do now is basically just say something like um sim T all right that was that variable that were created again it just needs to be two columns here the first column is the x the second column is the Associated signals there so now we've got you see this block of code here is just generating the input signal that if we come back to the Simulink model will be fed in via the from workspace block and that will be used to drive the entire simulation so again let's take out the breakpoint here and just run this and let's see we should now be seen here we are here's the response theta of the pendulum to that end point and we see yeah it looks like it's ramping up and then it goes into this interesting behavior as due to the sine wave so this looks awesome I think we've got most of the inputs here or we've got most of the necessary tools to get data from Simulink and also sent to simulate so this looking great alright so maybe the last thing I'd like to show you that might be helpful when you're interacting with the Simulink model via MATLAB script is the open command tell you what why don't we put this in right before we actually execute the simulation via the sim command so it might be helpful sometimes to simply open the Simulink model so you can see what you're about to simulate so in order to do that's actually really simple it's just call it open and then you pass it the name of the model so we've got the name of the model down here so if I pass this in here what line 36 is gonna do is gonna open the model before we actually run the simulation here again this is completely optional maybe we should put a little note here this is completely optional we saw that the sim command has absolutely no problem running the Simulink model if it's completely if it's closed here this is purely for aesthetic purposes just so you can see the model that you're about to run so let's go ahead and run this again and here we are everything is working I think we've done everything we'd like to achieve or accomplish here so let's just take a real quick recap I think what might be helpful is let's just walk down this script that we've gone ahead and generated because like we said the point of this video was to show how you could interact with a Simulink model and control it via a MATLAB script here so obviously our first block here this is our normal clear CLC close all here this is where it starts getting interesting right the very first thing that we usually do that I always recommend in terms of a pattern here when you're interacting with the simulated model is the first thing you do after you do your clear CLC close all is define some of the model constants any of these parameters like um gravity mass damping ratio link any parts of the model here they should just be defined as well as any simulation parameters things like the final the simulation time the time step initial conditions all of these are needed to generate uh to be defined in order to get the model to run similarly what we next did is we created some data that could be used using the from workspace block here so again this could be something where you would load it in from a down file here or you procedurally generate the complicated signal that you want as an input right here whatever you like to do so at the end of line 33 we have to find pretty much every single input that the Simulink model is going to need to run then what we do here on line 36 is we use open to just simply again just open the Simulink model is this saving us the the work of going file open and browsing through the model here this is completely optional and is purely for aesthetic purposes the real magic happens here on line 39 when we are gonna use the sim command and pass it the name of the Simulink model we want to run what that's gonna do again is it's just gonna go ahead and press the play button in that Simulink model and have it generate the necessary data for our analysis here after we run the model here right we saw that if we had a properly instrumented the model namely with either out blocks to work space blocks or log appropriate signals that would then get sent back to the MATLAB workspace here so again line 39 is doing an incredible amount of work right all of the heavy lifting is done with line 39 right but it's the Simulink model that's doing all the heavy lifting here in terms of generating the data here and running the simulation after we do that the next thing you're gonna have to do is you're gonna have to extract all that data that was created so let me just uncomment all of these blocks of code so we can see what's going on here so again we looked at multiple different ways to extract data so we looked at using it out one block here right and if you wanted to use an out one block to get data here is the appropriate syntax to extract the relevant data from that from that block again this is not my favorite method I actually really like the to work space block here so we saw that there were multiple ways that you could use a to work space block you can either use a default times a default data structure of a time series and if you wanted to do that here here's the syntax to go about extracting that data or you can use this structure with time format all you have to do is just change the syntax of how you're going to unwrap that data structure that was passed back to you and finally we looked at sort of the newest way of going about doing this is logging the data the using signal logging to log that data to this logs out variable and again if you wanted to do that here was the syntax in order to do that now once you were able to do that maybe we should put a few more comments here after that you're free to do analysis using MATLAB on the data here right so again you can use all a MATLAB min/max FFT any functions you want you can call it on the data you can analyze the data you can do whatever you like here for in this case we're finding a max and then we're just visualizing it here all right so there you have it this I think is our entire workflow here for using MATLAB to control a Simulink model and to interact with it so let's just jump over to a quick set of summary slides so we can recap what we talked about today all right so to summarize what we covered today we started out talking about how you can define variables in the MATLAB workspace which are then going to be used by a Simulink model here this allows you to generate a much more robust and dynamic Simulink model that's able to adapt to changing parameters we then looked at the sim command which allowed us to basically run a simulant model from a MATLAB script after running the model here one of the things you probably want to do is you want to send that data back to the MATLAB workspace so we looked at three different ways to do that first using the out one block which we saw is actually not a very good way to do it here and it's more presented here out of academic interest than anything else the more popular and actually my preferred way to do this is to use the two workspace block and finally we looked at also how to log the signal if that's how you would like to get data back to the MATLAB workspace lastly we then looked at sort of the opposite direction operation in the sense that now what if you had MATLAB data defined in your workspace that you want to use as input to a Simulink model we looked at how to do that using the from workspace block so I hope you enjoyed the video and it was useful for your interactions with MATLAB and simulate if you did like it please subscribe to the channel here you can do that by clicking on the icon that should have just showed up on your screen we will be having weekly videos on MATLAB and other engineering software tools and other and topics of interest as well so with that being said I hope to catch you at a future video I'll talk to you later thanks bye
Info
Channel: Christopher Lum
Views: 85,813
Rating: 4.9734941 out of 5
Keywords: Matlab, Simulink, To Workspace block, Simulink signal logging, From Workspace block, control Simulink from Matlab, Matlab/Simulink interaction
Id: sF_sjFqNFUk
Channel Id: undefined
Length: 44min 35sec (2675 seconds)
Published: Sun Oct 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.