Object-Oriented Programming in MATLAB | Master Class with Loren Shure

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome today today's master class on  object oriented programming thanks for joining me   um i'm hoping today that you're either in one of  two categories either you're um uh using matlab   but you think you need to start learning about  object-oriented programming or perhaps you come   from another language that is object oriented and  you want to see how to do it in matlab for both   of those p sets of people i'll be trying to show  you what's going on today so thanks for joining me   my name is lauren shore and i've worked at  mathworks for over 30 years and i try to   share my knowledge in classes like today's you  might also find some interesting matlab tips   um on the blog that i maintain on the mathworks  website the art of matlab so let's get started   okay so as i said the uh topic is object-oriented  programming with matlab and um my agenda is   basically why why bother with object-oriented  programming we can get everything done or most   everything done without it i'm going to make sure  that for those who are new to this area that i   talk about the basic concepts and terminology and  i'll show you how to make a class it's pretty easy   and then how to make classes more robust and i'll  show you what i mean when i get that to that and   i'll just talk in general about um more advanced  features that are available that we won't have   time to go through today really so the problem  i want to talk about is um finding and locating   um balloons or blips from radar that's been  deployed up in some balloons basically and i   have a sensor array that's going to do this and  you can see what i have here is that i have two   that are um staying at about minus i don't  know whatever minus uh 30 degrees and minus   or minus 45 degrees and minus and maybe minus  50 degrees and then i have this other one that's   moving and as we go over time you can see  that it's um being detected and identified   different number of targets at different times  and so i would like to show you as an example   how i use that and a bunch of different  programming techniques uh for working on it so   um the first thing i want to do is talk about  the um progression of different techniques so   whenever we're talking about an algorithm  and something like this we have data as well   and we need to combine the two the algorithm  has to actually work on the data to make this   make sense and so we might have values like i  might have ten dollars in my checking account   and at the command line i might say um tell me  the interest on my ten dollars and i multiply by   point zero zero zero zero zero zero zero zero  one and find out of course that i get nothing um   but i don't need a fancy program for that and  that's not something i do every day i don't   need to automate it there's not a big deal with  that um but as things get more um as i'm trying   to solve a deeper problem i might want to take  advantage of increasing sophistication and so it's   not that hard but i might put some data into a  variable and i might use that variable in a script   and i'm you know might have a whole bunch of  lines of code in order to analyze those data   and i might want to say well i don't have  just one variable i actually have a bunch   of variables that are related so i might put them  together in a table or a structure or something   like that and i might operate them on on them with  functions knowing that i might have another set of   data coming along later and i want to reuse  this this this algorithm that i'm creating   um on new data over time and then  finally we get to classes and classes   um are an interesting uh construct because well  you notice up here in the beginning when i was   talking about the programming techniques the data  kind of sat on their own and the programming if   you will the algorithm sat on its own but in a  class we actually join them together and classes   have basically the algorithm piece which is  called which are called methods in object   oriented programming and the class also has data  associated with it and those are called properties   and so the scenario that i'm looking at is a  sensor array and you can see the array of sensors   here that are going to try to locate a weather  balloon that's emitting transmitting a signal and   the sensor array receives these different flips  over time but of course they don't all arrive at   the same time because they're spread apart so this  one might if this was the nearest one it'll come   to their first and maybe their next and so on and  our goal is to process the blip um in such a way   so that we can compute the angle of arrival how  far is away and what angle you know is the blip   here or is it somewhere over here for example in  which case the arrivals would come in a different   order maybe in different a sequence and so that's  the problem that i want to try to solve here um so how can we implement the  scenario and how can we make it robust   um so i'm going to come over to matlab now  and i'm going to show you what i would do   with a regular procedural program only i have  enough code here that um i am not going to   um type a whole lot here i have stuff already  written and what you'll see here is i have my   script here it's a live script um so i've got  um or it's a regular script rather and i've got   my comments here and i can just run a section  if i come to the editor i can run a section   i use run a section in advance so often that  i have a copy of it on my um quick access   tool strip here toolbar and so i'm going to run a  section i'm just going to make sure i have nothing   in my workspace and now i'm going to define the  signal that it's sending so if i come back to this   picture this is sending a signal well what's  what's inside that signal what's inside that   signal is well in order to do what we need to do  we need to know what the speed of light is because   i'm going to take that with the frequency of the  signal that we're using and compute the wavelength   it has a certain wavelength and i'm going to  give it an amplitude so that's what our signal   consists of um i mean that those are the specs  for the signal that's not the signal itself   and then um i need to define right now we want to  eventually find out what is the angle of attack   or the angle of arrival but right now i want to  assume i know it and do the problem in the forward   direction eventually we want to go backwards  and solve what's called the inverse problem   so i'm going to have a balloon at  an angle of arrival of minus 10   i'm going to say its range its distance from  here is really really big it's basically in   infinitely far kind of not really but pretty  big and then i'm going to embed in the balloon   the signal and so now i have something that's  called a structure so if we look in my workspace   i've got two different structures and i can  click on one of them and you can see that this   is a structure the balloon and it has three  fields now in this case it has a single value   the angle of arrival we happen to see it here the  range 1 times 10 to the minus to 10 to the eighth   and then embedded in there another structure so  basically a structure allows me to um combine   data that belong together but don't necessarily  fit together like in a vector or in a matrix   or even if i could it might be inconvenient  because they're different units or something like   that okay and how do i index in well if i go like  this um let's just go like that you'll see that i   index in here by saying balloon dot aoa so dot  is the way i index into a structure and find the   value of a field in the structure okay so that's  what a structure is and many of you probably   know what that is i'm not going to add any extra  balloons right now i'm going to skip that and i'm   going to define my sensor array and you can see  it has a whole bunch of parameters that i need   in order to try to figure out um how to make  it work right it's got a number of detectors   a number of samples and zero padding because it's  actually going to use an fft algorithm underneath   and from all these specs from the balloon and the  sensor i'm going to actually make what the signal   would be coming from that balloon based on that  sensor what would arrive at that sensor and i'm   going to get the array data from that and from  the array data and the sensor information we have   and i can show you i've got this here it's  a whole bunch of ffts and things like that   but you can come here and now i can say  calculate the angle of arrival based on this   we knew what it was to begin with it comes  back and tells us it's at an angle of minus 9.5   well if you recall i made it an angle  of 10 minus 10. so um i might want to   figure out um whether even before i do something  in the field you know if i want to have maybe   instead of having um my number of detectors be  16 maybe i'm willing to spend money and make   you know 3 000 of them but before i do that i  might want to do an experiment um numerically   to figure out if that's going to help me  so um let's just work with the fft the   sensor zero pad first and let me come in here  and make that 256 and i'm going to recreate the   data because i changed the sensor and here's  the angle of arrival and it did get better   and you can see i have a 20 48 there i'm an  impatient person so i'm going to go right to   the value that we found was really good for the  zero padding and now i come here update my data   and i get a much closer value closer to -10 than  i had before and you can see it over here in this   plot which is showing where the or peaks are the  angles at which um there are potentially different   arrivals and we can see one here at around minus  10. well maybe that's not quite close enough for   me and i go well what else can i throw at it  i don't really want to do a longer fft because   maybe that will introduce too much latency into  the calculation and i want to be able to keep   track of these things very fast and so maybe i  can change the number of detectors so i can say   sensor dot num detectors equals um  i had 16 i'm going to make it 32.   since i changed the sensor i  need to recompute the signal   and i need to recompute the angle of arrival and  it didn't change the number of detectors the more   information i have from more different bases on  the ground it's not helping me in a finer way   find that well let's just try this again maybe  32 wasn't enough maybe i should do um 10 24. i mean i'm just this is just a simulation  right so why not it didn't change it at all well let me come back up here and remind  us we also had our signal speed of light   one of the dangerous things about using  structures and procedural programming is   i could say signal dot and by the way if i  just do dot i can hit the tab key and find   out what's in there so i can change the speed  of light equal to 33 that's not really good   i'm going to come up here and actually reset it  because i don't want to mess that up but you can   see that i'm not safe that way so why didn't this  work well i'm going to show you why it didn't work   because i cheated and i'm going to open up sensor  and now you're going to see that my original   field was called num detector and i wrote  num detectors and it just added another   field it didn't warn me it didn't tell me  it didn't give me an error message anything   and so i'm a little bit um unprotected i can  do everything i want it's sort of the wild west   but there's nothing to put checks and balances  on me at that time so it's a little bit scary   it's very flexible but things can go wrong so let  me come back to the slides for a moment so as i   said it's pretty easy to learn we kind of didn't  have to plan we could think about oh well what   if we added this new parameter maybe that would  help and you get to try things really quickly and   um there's no formal relationship between the  data and the functions though i'm not making sure   that things i'm not doing any sanity checking  and you see every single thing in that sensor   and maybe some of those things aren't available  maybe i'm going to switch a sensor someday and not   all of those things are going to be available so  i could have a problem if that were true i'm going   to come here while i'm at it and change this  to 96 and then i'm going to rerun this sensor and i don't get much closer but now what  happens is if let me actually change that   back for a moment let's leave that  at 16 for the moment um whoops 16.   and now what i want to do is i'm going to  add two more targets so our signal is going   to get be a combination of balloon number one  balloon number two and balloon number three   and you can see instead of it minus 10 i  have one at 20 and one at 25 that i'm adding   and now i'm going to just set up my sensor  array compute the data compute the angles   of arrival and and display the and now you  see it doesn't find three it finds two and oh no it only finds one here that was left  over from before it finds this biggest one   here but this this peak even is enough lower  that it decides it's not important enough   um and that's again because at this point i don't  have the right number of detectors so let's see if   we can do better with the more detectors that we  have let me run that create the new um sensor data   and the field data and now you can see i get three  balloons minus not quite ten i'm not quite 20   and and 25 and here you see on my plot i get the  three different targets that we were looking for   pretty closely now again but this was without  doing anything formal to make this all work   well that may be uncomfortable to  a lot of us you know then you could   start putting all kinds of error checking in  the program do that but if you miss anything   um maybe it's so obvious to you that the speed of  light is what it is that you don't even think to   make sure that someone can't change it by accident  um for the purposes of your experiment so um um   what i said before was that there was  this progression of programming techniques   and now i'm ready to think about using a class  to help me out here to help me not make the   kind of mistake if you will when i put numb  detectors instead of numb detector in there   and let me give you um some background on object  oriented terminology and this is general object   oriented technology not math work specific  a class you might also hear it about about   class programming classes are a blueprint for  creating an object it's an idea classes also   contain properties that are data and state related  and behaviors which are algorithms or behaviors or   actions an object is a specific instance of  a class so if i have a class called dog the   properties for that class might include breed age  color and the things that can happen with a dog   are eat sit and bark um now here's two instances  of dogs my pal shawn's uh dogs lexie and luna and   you can see they have data behind them they have  alaska husky she's eight years old she's black and   white and a cho well this isn't this isn't john's  i don't know who luna is but luna is a chihuahua   she's age four and she's brown and they can both  do the same things they can eat sit and bark   but the way they do that of course is different  lexi may bark in a much less high frequency bark   most of the time than a smaller dog a chihuahua  so they don't do it exactly the same way they   don't get the same value the same outcome but  once you put their properties in you can do   modeling and predictions so what i want to do  now is i want to define a class that's a blip   and from that class i want to create a balloon  a balloon object and i want to use that object   in place of the structure that we have so let  me just come back here and what i'm going to do   is i'm going to come to the home tab and i'm going  to say i want a new class and matlab opens up this   file for me it's basically a template i'm going  to double click here and show you the whole thing   right now and i'm going to make it of a class blip  and i'm going to put some help in here flip um simple lip flip class this is why you don't want  me typing because even on something as simple as   that i make a mistake um yada yada you hopefully  will use something more meaningful and now   what properties do i have well what did i have  before in my balloon if we look in my balloon here   my balloon had aoa the angle of arrival the range  and the signal so i'm going to come over here   and i'm going to put aoa range and signal and at  the moment i'm going to delete everything else   because that's all i got and i'm going to  um put an end at the end because the code   analyzer was telling me to and because  it's the right thing to do let me just   okay and i'm going to save this whoops  and i'm going to save it to blip now   what what can a blip do well i can say b  e whoa my keyboard got enthusiastic there my keyboard is being sneaky at the  moment it's not letting me do anything one second sorry about that  we'll be back in a moment well we'll let me do this i can cut that out and now come on keyboard you can do it i don't know all my equipment  has a mind of its own today come on so i'm sorry about that folks for a moment my  keyboard quit working and now it's working so b   equals blip and you'll notice that blip doesn't  have any values but i've got i can then say b dot   um a o a and i could say equals  minus 10 and b dot um range equals um   whatever range i had before i think nope oh yeah   it's uh wherever it is let me come back here we  range let me go like that copy that was because   it called the range function in matlab instead of  what i wanted uh b dot range and um b dot signal   equals the signal we have so now if i look at  b it's got all this stuff in it and if i say um well let me start with something easier  let me make an a a equals 17 str equals   string i can say is a a is it a string  oops and it's not you already know about   classes even if you didn't think you  knew about them is it a um uh double   yeah is it a float yeah it's floating point  because floating point is double or single   is it numeric numeric yes but it's not a string  and i can do the same thing with string and i   say and say is it a string yes is it a character  array nope is it a cell array because we used to   put strings into cells and celeries nope so  now i can say is a b my blip is it a struct no but you saw i sort of i'm addressing  it like it is b dot a o a and so on   but if i print out b you'll see that it's  a blip that has properties and if i go like   this it will bring up the help on blip which  is very sparse it's a very simple class and so   on and it doesn't have very much going on in it  but it becomes self-documenting when i do this   well that's nice what if i just  come along here and i just say balloon equals flip so let me come here let's execute that now and  i'm going to add my two extra balloons i'm going   to add my sensors i'm going to create the array  data and i'm going to compute the angles of   arrival i get the same angles of arrival i get  the same plot we had before but now my balloon   is a 1 by 3 blip array and i  can say show me balloon of one   i can say show me balloon of three dot range  and so on so you'll notice now i'm using regular   indexing with the parens as long as well as the  sort of structure indexing with the dots so the   nice thing is my analyze data everything else  just worked because the notation is the same   even when i changed my balloon to the class blip  now what else might i want to do with this class   well i might you know i have three balloons  it might be really a pain to dig through   the signal and find out what the um the actual  angle of arrival is there so what i can do is   i can come back to blip now and i can add some  methods and one of the methods i would like to add   is and you do this in a methods block i'm  going to put the methods and end in first is   you can basically do whatever you want here  i'm going to make a function you basically   program these almost the way you program um uh  regular matlab code i'm gonna have function it's   gonna take in the object and that's the only thing  it's gonna take in and actually that's gonna need   to be an input the object that we're talking about  is gonna need to be an input for any method that   we create with one exception and i'm not going  to get to the exception yet but i want to write   the identify code so let me say my identifica code  is going to be disp i'm just going to display hi i'm a balloon well let me not do  that i'm friendly and arriving at my angle of arrival degrees don't shoot okay so i've got that right now so now  let me save that and now let me say um   let me show you a couple things blip but button  uplift balloon when i do balloon dot if i hit tab   you'll notice not just the properties but now  identify is in there so i can call identify   well for those of you from other languages  you're used to this probably i can identify   um well i actually have to pick a balloon not just  one so let me do balloon of one of one identify   and then since i'm calling a function i'll go like  that and it says unrecognized variable it doesn't   know what aoa is and in fact if you look here i'm  going to show you the whole thing i have some um uh um messages warning messages from the code  analyzer and it's saying um that the object   isn't being used should it be a static method if  you don't know what that is don't worry right now   and i have another message that's saying confusing  function call did i mean to reference the property   aoa of my object and in fact i did i really want  the aoa associated with the object that we've got   and to do that what i do is i say give me  the object aoa once i do that you see all my   code analyzer messages get better and now i can  come back here and we can identify it and it's   at -10. for those of you who aren't used to  oo programming i can call this like a regular   function though i can say identify balloon  of three and it just works that way okay now   there's something else interesting notice when i  look had to fill in my parameters for my balloons   i had to create my balloon first and then i'd had  to had to say it's aoa is this and it's range and   i had to do all these assignment statements nine  assignment statements here for three balloons   but what if i want what if i know the answer  right in the beginning and i want to set them   up right away well i can do that in blip because  one of the things that the class has by default is   what's called a default constructor which is one  that you don't need to write and that's what let   me say balloon equals blip it let me just create  one even though um i didn't say anything about it   and it just created an empty one well i'm going  to create um whoops i need this in my methods   i want to put another one in here and i'm going  to say function and it's going to have an output   equals lip lip and i'm going to have as possible  inputs an angle of arrival a range and a signal   yes and i'm going to put the end on this  function and then what i'm going to do is   i if if those come in i'm going to say obj.aoa  equals the aoa that's the input to my function obj   dot range equals range and obj  dot signal equals equal signal save so now i could say oops i could say  um yeah balloon of three equals lip of 25 and um 1 e to the 8 was that yeah and oops signal and i get to do that all in one call notice i'm  trading the um the three calls for one call here   now if i might wanna sometimes have that default  constructor then this won't work all the time   it's only gonna work when i have three inputs  so i can simply go if nr again is equal to   is equal to three and and that if statement  end it and then i control a control i i like   to make things indented nicely so i can save  that so i've got code now that will create   the class let me put preset values in when i  create it and do the identification um so um   what we did is we defined a class from the class  we were able to create the balloon object what i   didn't show you though is something really cool i  forgot to show you this balloon of one dot a aoa   equals 13. it can't it doesn't recognize it  because that's not the way i spelled it because   matlab is case sensitive for these sorts of  things so i present i prevented myself remember   in the beginning i changed the numb detector  i used num detectors instead i can't make that   mistake anymore it's going to catch me and yet i  don't have any code there specifically doing the   argument checking it's what i get um that's part  of the value of the class and so um so i created   a class with properties and methods everything  still works so far but the bad news is if we   take a look um in my class um i've still got aoa  range and signal and if i think about the sensor   the sensor whoops the sensor here has everything  else exposed the signal and sensor both but   the sensor here where'd my sensor go here have  everything exposed now these might be parameters   that a particular manufacturer uses and they may  um have several different ones with different   sample rates or something like that or maybe they  even let you change the sample rate of your own   equipment that you bought from them but another  vendor might make a sensor array that doesn't   even have some of the notions of this maybe it  doesn't worry about spacing or sensitivity and   we've exposed all that information to the users  and so we've maybe exposed more than we want to   one of the things that you'll find  if you notice and you use any classes   that um come from math works toolboxes um we  don't we deliberately hide some of that stuff   we don't want to hide anything that will  impede your using that particular class or   creating objects that are useful but what we  will do is make sure that you have access to   everything you need but we try to hide the rest  so if we change our mind later about something   about how it's implemented behind the scenes  it won't break your code because you can't be   depending on something that you can't get access  to at that point and that gives me a segue to what   i want to talk about next which is we have all  this information we have the number of sensors and   sensor spacing and all those different things and  plotting the results and maybe that's not stuff   that you know in the middle of all this i need  to synthesize the measurements i'm going to be   computing an fft you don't need to the end user  doesn't need to know all of this stuff um uh they   might need to compute aoa they might not need  to know specifically how we do that with the   determining peaks maybe there's some other method  of doing it instead and so what we'd like to do is   basically get change the interface to be the  smaller number of things that we need this is just   the parameters here and these are which is the  properties and these are the methods that we might   need for the sensor and all the other things that  were in there parameters or methods are internal   to there and it should simplify the use of the  object for you because this will be the only   part that's documented not all all of the other  pieces and so now when i make something like this   it becomes a really nice building block for me  while i'm doing that um i also might want to um uh control um how do i control that while i control  it with these attributes that i'm going to use   you can see that i can make the access public  or are protected i can make the modificat uh   modifications either dependent on one another  if two things depend on one other are constant   um so there's a bunch of things that i can  do so um let me yeah come over to here whoops my mouse are not getting along now i'm gonna come  back to my project here and i'm gonna come to um   um number three   and actually i'm gonna come to number four i think  um number three no i'm gonna yeah i'm gonna whoops there's my blip that's my blip this is it's blip   yeah i'm gonna come over to um project  number part of project number four to show you something about encapsulation so  um i have my blip you can see there's a class   depth that's the name of the file has to be  the same as the um actual class name and i've   got my properties and i've got my methods here  so it's basically what you saw me have before   it's got that uh if nr n equals three constructor  but now i've turned my sensor into a class as well   and what i've done is i've put in some  checking because the number of detectors   actually needs to be a scalar  so it's going to be a one by   one it's going to be double so it must be  in and i want to say it has to be an integer   and must be positive because we don't want  our detect to have minus three detectors   zero padding must be integer and positive and  you see i can set default values for them here   but now some of the other properties of that  sensor we don't actually need to make public   and so what i'm going to do and by default  they are public so what i'm going to do here   is i'm going to keep this part um private  and i'm going to make a new properties block   properties and i'm going to go like whoops like  this and i'm going to simply copy and paste and   i'm going to make the properties gettable so  people can read them but they won't be able to   change them so i'll put an end on that and then  i'm going to make another properties class because   properties prop properties and we'll do the  same thing but because i want these to be   they don't need to know that underneath we're  using some um the speed of light and they we   don't want them to change it anyhow and um and now  i'm good so i've got um you see i'm controlling   things like default values i am protecting things  in this case so that people can read them but   they can't change them these they can read and  change but they're restricted in certain ways   and then these are things that they can't do  anything about and i can do a similar thing   with methods and i can have um for example the  wavelength and the frequency are dependent on one   another so i could actually come here and i could  put in getter methods and what this allows me to   do is say that there's some dependency here and  in order to do that i actually have to come here   and let me come up here and put an end on  this and let me do prop whoops properties and um here we go dependent that doesn't look like right it doesn't want  these values anymore since they're dependent   take that out and now i have a blip class  that is um if if the spacing changes somehow   um we can calculate the wavelength and vice versa  they're related here so we get information and i   have some other methods i can have the compute  aoa method and i want to make that one public   because we're going to need that for the sensor  and um i need to create the sensor signal because   of creating the synthetic data rather than getting  it in the field and then i could make these other   methods protected if i want to i'm not going to  spend the time on that right now but you'll notice   that now i have the speed of light being constant  so let me come back to my analyze data here now   and let's um come and run  this so let's clear everything   let's do signal okay and now let me come back here  and we have our signal which is the speed of light   and i can say signal dot  speed of light equals 34. oh didn't you shouldn't have let me do that  did i save this oh i have the old blip no um i did that i did something silly well here's  my best way out of something silly that i've   done let me come here um because i've got  it in here and if i look here at my sensor now if i use this i've got my analyze class let  me clear everything out here so you can see it   i'm going to run here i've got my signal dot  speed of speed of light equals 34. oh it's   still letting me it must be must not have  i must not have set that properly okay um   oh that's the signal i'm it's the  sensor that i was doing ah sensor answer dot speed of light equals 34. oh it's still  letting me do that too okay um oh because i have   the old one i didn't run the new code sorry i  am confusing everything here um let me come here   um well let me not worry about that right now  i'll show it to you in a minute because i want to   go on to the next topic um which happens to be um  well first of all let's find out about attributes   i'm going to say um property attributes so  what kinds of things can you change here   and you're going to see i'm looking up attribute  property attributes in the documentation   comes up with a nice page here and you'll  see that properties from matlab i can   set whether or not to a different value abstract  classes meta information right here um constant   dependent and a bunch of information a bunch of  different things whether or not you want to allow   partial matching so if someone doesn't  type the whole thing they can move ahead   um so a bunch of information in the documentation  there are many many examples in here you'll see   that there's properties there's  class definition there's programming   and there are plenty of examples when it comes  to this so if you come to the examples here   you might want to look at some of those to help  you out too okay so i want to talk about um   inheritance and then i'll show you a little  bit more about the properties as we go   um so suppose i have right now my blips or my  balloons are stationary in the sky kind of like   a weather balloon or something like that but what  if one of them gets a string cut off and it's   moving along it's still a balloon and i can  probably still find the angle of arrival from it   but it's going to be changing over time so  if i have something that's moving that could   still be a blip but it's just a different  blip than the way i've programmed things for   but it has many things in common with everything  i've done before here too and this is where we   get into inheritance rather than creating a second  complete body of code what i can do is i can say   my moving blip is still a blip so use a lot of the  goodness that we've got in our blip class and then   um take advantage of that and then add the  differences or the extra things that we need   for the fact that it's moving now so here's an  example if we have our main class the base classes   and employees they all we all start off with name  dress address and department and we might have two   different kinds of employees a sales person and  an engineer and there's different information   that we track on them the salesperson might need  to know have associated with her him a region and   their commission structure and an engineer would  need her products and her team and there might be   another subset under there underneath engineer  there might be a specific kind of test engineer   that has assigned to to him or her  the test stage that they're using   and so i don't want to throw out everything i want  to have these also include the name address and   department that they're in so way i do that is  i could say i can make a class deaf engineer   that inherits from employees and a class test  engineer that inherits from engineer now um i want to take a a little side tour for the  moment because um because the way matlab works   and the ways classes work um are important in  terms of understanding when matlab makes a copy   and when matlab uses something even if it seems  like it's a copy so if i type this a equals four b equals three and now i say um no and v equals three let me say b equals a and  now let me say a equals three what value was b   well if you saw before the last time i signed it  it was 4 and yet i just changed but it came from a   and so if i look it actually kept  its value 4. so it keeps its value   um but what if i do something like this h dot um let me make a line in a plot   bring up our plot here's our handy plot  i'll put it over here and i'm going to say here's a capital h equals our lowercase  h and now i'm going to say capital h   dot line width ah line width  equals 15. notice how the   that changed the property over here if i say that  was capital h show you there that was capital h   if i say h dot line width what do you suppose  it is is it what it was originally which was   um 0.5 no because uh it actually is a capital  h and lowercase a h are actually very closely   related they're references for the same thing um  it's because our objects our graphical objects are   part of a handle class in matlab and the handle  is a reference to a specific object that exists   values make separate copies as they go although we  do a lot of things about making sure we don't make   copies really we we make them we make sure  we're very parsimonious about it but you   can think about them each as if if i say  a equals three b equals a a equals four   it's as if we make copies all along um and so  a deep copy whereas the handle just inherits   the whole behavior and the whole aspect of  whatever was a reference so you'll find that   most objects in matlab most classes are our value  ones like anything numeric is likely to be that   but most things you write and things that we  write that are graphics or file system things   they may well be handle classes so why use  them okay well i think i showed you that um let me think about what we want to do  here so remember i've got my sensors here   um let me see if i've got a thought for that  so in in this case i want to make a new class   because i want to make a moving blip from a blip  so you're going to see that i inherit from blip   and the extra property is the amount it moved from  the in one step whatever that means okay and my   moving blip is going to have some information in  it it allows us to re-envision and re-implement   things from the super class the class that we're  inheriting from but it uses a lot of the code   that's already there so we don't have to rewrite  the code and really importantly if we find a bug   in that code or a behavior change we want to  make you get it automatically in the subclass   and you can also as it turns out inherit from the  base classes in matlab also so let me come here   and show you this so i have a class the moving  blip now the moving blip as i said is inherited   from blip i'm not clear to close everything right  now it's got one property or does it um if i make   a moving blip does it have one property well  remember it inherits from blip and blip had   three properties so let's say mb equals moving  flip it has four properties it's got the original   ones from the inherited class and it's got the new  one the delta aoa and you can see that the way i   create a moving blip i have um a constructor  here and my moving blip says basically um   get the object the um the information from the  extra parameters in case someone passed them in   um and put them into our object but we want to  take the last three and call our superclass and   this is what i do to call the superclass i say for  my object my moving blip here call the super class   blip and call just with those final arguments  and if um the number of input arguments is   greater than or equal to one which it hopefully  will be um we want to fill in a delta aoa value   and then it's got a method called move and notice  that to move it what i have to do is i have to   somehow say what happens to the object and  i have to say that the object was at aoa   and its new aoa is its old aoa plus the object's  delta aoa and then we just move it and and negate   it um after a while so that it doesn't move as  we go because i'm going to show you how i want   to use this blip did change the only change  here is we made it a handle class as well   and everything else is the same now let me come to  analyze this so we come here this is the co whoops um i am i'm trying to run something  that i was i was not on the right line   okay so clear everything set up our signal set  up arm blip now notice here that because a blip   is not a moving blip necessarily it might be  but it might not be but because these things   might be different i have to put them into  some other data type that will let them um   join together and so i'm putting the blips each  of my um information about the aircraft into a   cell and a cell array here so i've got three blips  now and if we look at balloon oops see it's a one   by three cell and we can do balloon and  i can get the contents out of number two   and it will show me that it is a blip and all the  information and i can get number three and what   you'll see is that it's a moving blip okay and  now i need to create my sensor array information   and i need to create the angle of arrivals  and now what i'm going to do and the angle   of arrival start off at the original positions  roughly minus 10 20 and 25 and i'm going gonna   um i'm gonna keep i'm gonna park this in here for  the moment and i'm gonna make this a little bit   bigger um what you'll see is now i'm going to  um move my balloon um in a loop here a few times   so let me run this and watch the plot as we go  and you can also watch the output if you want   well no there won't be any output there coming  out but you can see it is keeping the two in here   in place and then it hit to near minus 65 it  get too big and so it went back and then forth   again because we were moving it remember i didn't  let it wanted to get way far away from the other   ones so we moved it so that you could see that one  update as we went and so um i could then call the   move function and that's with a pretty minimal  amount of changing to the code that you have so i want to come back to the slides and just  remind us that the subclass substitutes for   the super class remember i was able to use a  moving blip for a blip in this case i did change   everything in this case to a handle because i knew  i might want to combine blips with moving blips   and by the way because this is a moving  blip i can come into my moving blip um   i don't need to return the object because it's  a handle class so if i take this out right now   and run it again uh over here just this section  it works because being a handle class i don't   have to return it as an output it's changing  itself and you're seeing that happen right there   um if it were a um value class you absolutely  need the output there back here the obj equals   move option because otherwise it doesn't update  the object um because it needs to be in the output   okay so um we really changed a very  small amount of code to get some extra   new functionality to allow these things to  be free-ranging and um now i'm just going to   talk very briefly i told you about inheritance  there's this notion of events and listeners so   suppose a balloon comes untethered um  should the balloon um be responsible for   telling everyone hey my tether let go or should  anyone who cares be listening for it and we think   that what should happen is the balloon should just  issue basically a message and say an event and say   hey i became untethered and and say a name of  what it wants what it wants people to listen for   and anyone who wants to any other classes anyone  else can listen for that event and that way your   balloon your blip does not need to know about  other classes that someone may build in the future   that want to take advantage of it it's  just saying here's events that you can   track on me and then it's up to you whoever  you are to actually put a listener on and say   did something happen and then if it did then you  get to do and make some changes based on that   um and there are some um examples in the  documentation for that so i haven't covered   everything but i just want to go over and over  quickly and show you a little bit more matlab has   been fully object oriented for well over 10 years  now class definitions methods and properties we   showed you events and listeners i just talked  about i didn't show you overloaded operators   but if it made sense we could overloading  operators and function names so for example   if i wanted to i could create a plot  method for balloons that would plot them   and i could use the name plot and it wouldn't  interfere with matlab's regular plot function   because this one would only work on blips so  you can reuse good names if it makes sense   i can i can change the access of properties and  methods you can have handle classes and not just   value classes there are destructors as well as  constructors so when something gets deleted what   else do you need to do well an example why you  might need that is if you've opened um a class   a file as part of your your class action your  destructor might want to close the file gracefully   i've showed you right now classes defined in  single files and my file was getting a little   bit long but there are ways of taking that and  breaking it apart into multiple files if you want   we get also static methods and i showed you a  little bit about property validation you can   use packages to control the namespace but already  even without that the classes they having things   protected or protected you can actually keep the  name space somewhat limited and not let you just   expose what you want and you can have object  arrays and the nice thing about these are they   behave like regular matlab arrays they obey  regular matlab indexing and if you don't want   them to you can program that in as well if you're  interested in doing more with this um i already   said many of these things we also allow you to  have heterogeneous arrays and mix-ins and um what   a heterogeneous array would allow me to do for  example is to put um blip or a mip blip and a blue   moving blip into one just one kind of class like  we have right here and use regular concatenation   rather than having to put them in a cell array and  if i do that um it makes the notation a little bit   simpler but it depends on what you need whether  that makes sense and is really important for you   you can always inquire about uh information about  the class on a object or on a class and i already   mentioned uh custom indexing we also have um some  help for you in terms of in that the testing doing   unit testing on your classes and if you're  interested we have a matlab training course   on object oriented programming that of course  goes into much more detail than i have just now   it's a two-day course and you get to practice a  lot of this and of course these days it's also   available online and depending on what  university you're at if you're at a university   you may well have access to mouths which would  give you access to an online version of this class   there's also additional resources in the  documentation that i urge you to go look at   the documentation is very good and very complete  i think it doesn't mean it couldn't always use   more but it has it's been able to answer my  questions when i bother to look and i now want   to just finish i want to thank you very much  for joining me today um it's been a pleasure   and i hope you learned a lot about oo um i want  to point you to two events in the links below   on the youtube page um the first one is the matlab  expo 2020 on demand it's an online site featuring   technical talks covering a broad range of topics  where mathworks products play a central role   and if you're interested in more about simulink  we currently have a simulink student challenge   um in process and you could be the winner of a  thousand dollars and um please also don't forget   to subscribe to the mathworks channel to get  reminders for events like these thank you so much you
Info
Channel: MATLAB
Views: 33,475
Rating: 4.8864865 out of 5
Keywords: work from home live, matlab, simulink, mathworks, matlab tutorial, using matlab remotely, working from home matlab, time series data in matlab, how to plot time series data in matlab, time-series, time-series data
Id: XOkn-zxhwc4
Channel Id: undefined
Length: 64min 13sec (3853 seconds)
Published: Thu Oct 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.