Programming with MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody welcome to today's master class on programming with matlab thanks for joining me today my name is lauren and i've worked for mathworks for almost 35 years now and i try to share my knowledge in classes like today's you might find some useful matlab tips um as well as looking at a blog that i write on the matrix website called the art of matlab um and another thing is in two weeks we're going to do a more advanced session on object-oriented programming in matlab so let's go i want to talk about programming with matlab today it's a sort of strange topic in some ways and the reason i say that is because um when someone first talks to you about matlab they don't say they usually say something more like matlab it's really great for data analysis or use matlab or matlab and simulink it's really great system for modeling but you don't usually hear people say use matlab it's a great programming language and yet we actually spend a lot of our time actually what i'll call programming in matlab and so i want to talk to you about matlab from that point of view today so my objectives are to introduce you to programming with matlab from the point of view of the programming um the language a little bit uh hopefully many of you have used matlab and already um enjoy some of that and i'm also going to show you some of the development tools that i use that i find very helpful in addition to that i also want to demonstrate a range of programming styles going all the way from the command line where we're just doodling around and making a quick computation to doing something potentially more complex and i'm probably not going to get all the way into um [Music] the object-oriented part today because as i said in two weeks i'll be covering that but i certainly will talk about it a little bit okay and i also want to show you how to program effectively in matlab well what does that mean i'll tell you as i go um and you will notice hopefully um that we have some maverick staff on the chat in the um channel that you're looking at please feel free to put your questions in there they'll be able to help you almost certainly okay so i want to show you how i use matlab to do some programming by actually doing an actual running an actual example and creating some some code for that so what i want to do is i want to assess where is the best location to put some wind turbines and so what i want to do is i want to estimate if i put a turbine here how much power will it produce and if i put it over here how much power will it produce there and so i want to try that in a bunch of different locations and then figure out where to put the permanent one the one that's going to have the best return so in order to do that at first i need to interactively explore the data let me tell you a little bit about the data that we have and let me tell you a little bit about wind turbines because if you're like me at least me before i um knew about this example i did not know that much about wind turbines so for our example we're going to assume all of the test turbines are the same um brand and the same um the same exact kind they're all the same thing okay and what they are are these towers and at about 80 meters height you'll find that there's three speed sensors now what you don't know about me yet perhaps is that i'm a physicist by training and so when i say speed i mean speed i don't mean velocity okay so it's going to be measuring just the total speed and not um a directional uh uh component there and the reason we have three there is there's mechanical parts in these sensors and so if you think about it if like a hurricane rips through one or more of them could get damaged and we don't want to throw out um an experiment because one sensor got ruined and so what we try to do is have some duplicates there so that we might be able to get good information moreover we need that information for at least a whole year and the reason i say that is is the wind is not constant and it's it's it's different throughout the year so it where i live it's windier in the summers and in the uh uh hurricane seasons and it tends to be less windy some other times of the year and also during the day it's not a constant there tends to be more wind a lot of times um uh right as this when after the sun's rising you get a lot of thermal action and um you end up getting the uh the wind dying down like it's sunset some of the time so the wind is not going to be constant throughout um throughout all this so i've got my three speed sensors they're at a height of 80 meters i also have a temperature sensor at a height of um three meters which um is about double my height okay so we only have one there first of all it's not um it's not usually mechanical so it's less likely to fail um and number two if i had to i could probably use the temperature from a weather station that's relatively nearby for example the all the data that we're going to get are relatively near logan international airport in boston and so i could just use the temperature from there and that would be probably a good enough proxy for what i want so what i want to do is i want to look at the data and pre-process it from these different observation towers and then what i want to do is find out how much time we spend with the wind between zero and point five meters a second and point five and one meter a second and so on all the way up to whatever top speed it is um because i need that information uh as well as how the turbine respond turbine responds to power at different wind speeds so if i come to the next um slide here what you'll see is if i want to estimate the average turbine power i need to somehow estimate this integral the uh integral of the wind speed distribution so that's what we're trying to capture with our um wind turbine uh the data that we're getting and then we also need to know what the response curve is for the wind turbine from the manufacturer and i'll talk about both of these a little bit later but for right now i want to go back so once i estimate the power i want to do it again and again and again and again so i can compare all the different turbines that are out there well i kind of want to do that but i don't want to do it by hand and so i'm going to want to see if i can automate anything so i'm going to come over to matlab now and in matlab what i'm going to also do is i'm going to come to my current folder and i am going to come into the i always have this problem in the demos and inside the demos you'll see i also have another folder and it's let me make this just a little bit bigger for you here it's called when data files and you see i have a bunch of files there tower locations one two three four five six i can have that's that's um just the few samples that i have right now so let's let's actually open one of these outside matlab and see what's in them okay and i know that's really hard to read so i'm going to um try to zoom in just let well maybe i don't want to open with notepad let me open it um you know what i'm going to do i'm going to go like this we're going to open the current folder i'm going to come into the demos the wind data files and let me open this instead with um uh well i don't it's fine i'll open it with notepad or notepad edit with notepad plus plus maybe that'll help me okay so here now i can zoom in and now what you see is it looks like i have five columns of information here i've got the date and time and actually i forgot to tell you but the data are recorded hourly so we have that information and then i have speed one speed two and speed three those are these three columns and then i also have the temperature um here and that's going to be in degrees celsius here okay so it looks like we have some negative temperatures in winter which means it's below zero or below 32 fahrenheit so it sometimes freezes so now i get to think about this and i go well shoot one of the things i never thought about when i told you before that i was thinking about the data is what happens if the sensor's not broken but we had a lot of wet snow and it got inside one of my mechanical sensors and it froze but then later on it may get warm up and it might work again so i might have to think about that when i look at my data because as you know data is not usually completely perfect so if i wanted to i'm going to just come to the command window first if i wanted to what i would do is i'd like to figure out how to read in the data and i'm going to come over here to the home tool strip in matlab and what you might notice here and we as we go from left to right is i've got i can create new scripts oh look import data that sounds like something i could use so let me import the data and i'm going to come into that folder i actually have copies of files in the current folder so let me use windata.txt here and now these are the same data but they're being shown the way matlab would see them by default and let me make this all a little bit um easier to see i'm going to just make these um columns a little wider and what you'll see is the first column matlab detected that it was actually um a time and so it offers to put it in a date time format i could put it in a string instead but date times are really really awesome so if you've been using a matlab a long time and you're using date stir date num please please please read the documentation and move to date date time it it will save you a huge amount of hassle now it also says it thinks the um file is tab delimited if i don't agree i can change that it's saying use the first row um as the variable name i don't have to have that but i'm going to and i'm going to have the range be well now i've got the range i'm going to reset this so what i actually want to read here is let me get all of it and now let me i don't really want to maybe read in that first row there but it's offering to put this in a table for me and i don't actually want it in a table i'd like to look at each of the variables separately so i'm going to actually put them in column vectors here and you see i have numbers for the three speeds and number two number for the temperature now what if i actually have some data that's missing here i get to decide what i want to do i can either exclude a column with some missing data well if i exclude a column let me zoom in again for you if i exclude a column with missing data i'm going to actually wipe out that sensor for the whole year i don't want to do that yet what if i exclude a row that has missing data well suppose the missing data is actually one of the speeds well i have two more speeds so maybe i could get a good enough answer with two speeds instead of just instead of three so i don't want to do that yet either um and i could also offer to replace it and so what you'll see is i'm going to replace unimportable cells with nan not a number and now what i could do is i can import it into matlab and now we can come over here and we can see all of the data that i've brought in i brought in my my five different variables now i just told you that um i know that the speed of the wind changes um over time but i don't actually have a very good feel for how it really changes so if i were going to look at that it wouldn't tell me whether i imported the data correctly or not but i still want to make sure i did so i'm going to actually look at time and temperature and i'm going to come over to the plots tab here and this shows me of all the mathworks products i've installed which is a lot of all the kinds of plots that are suitable for the kind of data i've selected well i'm going to just do a plain old plot and if you're already a matlab user you won't be surprised at what we see okay one of the things i get is my plot one of the things you'll notice is that in my command window and my command history it put the command i needed so if you're using a new visualization you've never seen before it's a great way to learn okay now if i were to show this plot before we decide if the plot is telling us that i loaded the data correctly if i showed this plot to my manager mike he'd go don't show me that and he'd say that because i haven't told him enough information so what i'd like to do for example is i might like to insert something so let me put an x label on and we will put time whoops time and you'll notice that we have about a year's worth of data there and i'm going to insert a y label and this is temperature temperature and i can click and of course then i can insert um a title time versus temp uh when data that maybe that's the station name okay all right so that's a little bit better now if i think about wanting to do this again and again and again what i might want to do is actually take everything that i've done here and save it in a way so that i could reuse this plot of course except for the wind data but i'm not going to worry about that right now and what you'll see here is if i look under the file menu of a figure i can generate the code for that that's really cool so if i generate the code what you'll see is i now have wow that's big i i now have i'm going to make it a little smaller here and now it turns out that where i am it is um october 7th at 11 15. and matlab just generated this and it allows me to um see the commands it used in it usually y label x label title things that you maybe already knew and if i want to i could save that so i could use it again i actually kept a version that i automated another time so i'm not going to worry about that right now but i want to come back here and now i'd like to see what's going on and do i believe this this is remember time versus temperature well winter where i am near logan airport is in december january february and so on so if i come here and i say let me zoom i can come and zoom in this region and sure enough these are the lower temperatures this is in degrees centigrade here so here i have freezing and i have colder temperatures there and if i want to i can come along and pan and we can go towards the summer and you'll see in the summer i don't have temperatures that are that low if i come back and zoom again a little bit more you'll also see that as i zoom my labels on the x-axis update and so it's telling me it's keeping me grounded about where i am in the world so so that's really nice and now i can do that on regularly well i had that data from the import tool and i can imagine loading in you know tower location one tower location two three four and so on what if i have a hundred i don't wanna come back here and change all those little things that i changed and for example i could change this if i can come and change any variable name as well so any of those things i change it would be nice if i could commit that to something so that i could automate what i'm doing later as well and if we look under the import selection you'll notice i don't know if it's going to let me do oh good i can either import the data as i showed you already or i could generate a live script a script or a function well i want to in generate a function because i imagine giving this a whole bunch of different file names and so let's just take a look in the workspace right now we have 8776 values for each of our things okay so what i can do is let me just save this now we'll save this as import file one and if i do help whoops import file one we see that it tells me when it got that i got i didn't ought to generate it but it also has an example based on what i just did so i can come here and i'm gonna p oh i didn't want to run it again but i did i just loaded it again so if i look in my workspace guess what i have the same number of numbers and in fact i can come back there and i can do that plot again here i'm just using the command line recall and typing a little bit and it's finding me the last time it plotted that and this is the same thing we saw except for this isn't the version that of course is uh got the annotations on it okay so i've begun to make this so that i can put together a story and that's what i want to do and i'm going to now go to something that um i've created already and this is now a live script so let me just take a little digression for a minute before covet i used to travel around the world a lot and give lots of seminars and workshops and and things like that for various customers and um i also visited many countries and in many of these countries i was not fluent in the other language only in a couple could i get by very well but if i wanted to relax in my hotel room afterwards um what could i do well i could i could always turn on the television and i could watch sports i could watch nature i could watch children's shows and i could watch cooking shows because with all of those even if i don't understand the language very well i kind of have an idea what's going on so you'll know if you've ever watched or even heard about cooking shows what often happens someone will go today we're gonna make an omelet and they break some eggs and they mix it up and they add some cheese and do whatever they're going to do and they put it in the oven and as soon as they put it in the oven voila what do they do they take out one that's ready and that's what i'm doing here here i have some final code that i'm going to use and we're going to go through so what i want to do is i want to clean up my workspace and close all my figure windows and for that i'm using a favorite on the um uh quick access tool strip which often lives up here but i have so many tabs open it's hard to see so i put it down below and what i can do is i can show you one of my favorite shortcuts that i created it's clear clears my workspace clears the command window and forces all figure windows to be closed so if i run that i just go like this and now you'll notice that i have nothing in my workspace so now i have this live script now for those of you who have not used live script i want to introduce it to you and give you some some information about it you'll notice first that it doesn't end in dot m the usual way that you're used to seeing matlab code and yet i'm telling you this is matlab code but it's matlab code that allows me to format it well so i'm going to make it a little bit bigger here so you can see and you'll see that i can put in titles and label headings and things like that i can also put in bulleted lists and numbered lists and i can center data wherever i want and i'm going to show you a few things as we go but one of the things i can also insert is i can put in an image if i had an image of a a wind turbine maybe i would put one in if i had a link to where the data is coming from maybe i put that in and if i wanted to and i don't have anything to put here realistically right now but i might want to show people the equations that i'm using and so for that i can either use the latex equation editor guess what i'm not very good at latex because it didn't exist when i was in grad school so i'm going to use the equation editor here and i'm going to go backwards here matlab the name stands for matrix laboratory so it turns out i can put a matrix of equations in let me put one in here and now actually if i do know some latex and i know a little bit i can put a little bit in so there's my pi but if i want to i might want to put in something for example like an nth root now if i said let me put two of them in two you say why two because i want to show you something if i said i want to take the cube root of x minus y to the 17th did i mean this let's put our cube roots in and let me go x minus y 17 or did i mean x minus y to the 17th okay and those are completely different right but if i just say it or if i write it in words and i'm not very very careful about how i say it you might think i meant one thing and i might have meant the other so it's really nice to embed these equations so that you can show uh someone your actual intent there okay so um i'm back to the point where we want to read in the data and i had showed you the import file one i'm actually going to use a version that i'd already saved import wind function data it's basically the same thing now if i want to take this and run i do this like i can run regular matlab code i can run a section i'll run that section there's only comments so nothing happens and i could go to the next section um there was an advance but maybe not i'll just move my cursor over here and now i can actually run the section in advance and i do that so often that you'll notice it's grayed out i could add it to my quick access toolbar because it's sitting right here and so that lets me minimize the tool strip for right now and go and execute some code so i'm going to load in the data and so we've loaded in the data and here you'll see that this was if i come back to the live editor this was in a a code section which is why it's offering i can switch it to text but i don't want to so i can have code or text okay and now i'm going to come to my next section actually what i want to do is clear all my output which i forgot to do so the next thing i want to do is i want to come here and i want to make a figure with all my speeds and the figure with time versus temperature kind of what you saw me do earlier so what i'm going to do is i'm going to run this section and when i do you can see matlab had an affordance it was twirling it was calculating it and then instead of popping up a new figure window in matlab it just simply puts my figure right in here here's wind data from tower location one now you know why i didn't know what the wind does because it changes a lot it's very variable even through a day or through a month and then we also have this is a different data file than i showed you before this is tower location one and in this case it has the summer in the middle so it's a higher temperatures uh you'll also notice that i was able to put some a little bit of latex in my label here so we have the degrees centigrade there okay now if i want to i could look at the outputs side by side and i can also shrink this if i want so i can actually come and i can and i can see by clicking on anything which section it came from i'll leave it like that for right now now i only have about a little more than an hour with you or about an hour and so i can't do the statistics that i really should do which is i should examine speed sensor one separately from speed sensor two and speed sensor three but instead what i'm gonna do is i'm gonna assume that if one of them had a problem they all had a problem at the same time again not a real valid assumption and not we would normally do but it's all i'm going to have time for right now so now i'm going to come here and i'm going to create an average speed profile by averaging the three speed profiles and um i'm going to come here i don't want to clear it i want to run this and you see i ran run in advance and now um i've got i can see whether or not that makes sense because i can look in my workspace and my average speed is the same length as the other ones so i did average the three speeds from each time step that we have okay so um remember what i said before though i actually want to think about what if there's some data missing well right now i said if it's missing if it's nan then um i i i don't know what to do with the data maybe i want to interpolate maybe you want to delete it i need to think about it but where else might i also get data that might not make sense and i told you it might be when the sensor is frozen well the sensor's frozen when it's when the the if it gets water in it when the temperature is probably zero or less but if i think about it being frozen and maybe it went down to minus 10 and it was really frozen solid right when it comes back up to zero it doesn't unfreeze completely so i might say you know let's give it a little room let me um let me give it um a speed to warm up of um one meter per second i'm not sure if that's right but we can try that and the other thing is um so i mean that i'm mixing things up the temperature let's say it's less than two degrees centigrade but if the speed is still very high then it hasn't frozen and i don't want to get rid of it so i might want to think well what if the speed is also slow so when the temperature's slow and the speed temperature is low and the speed is slow those are times when i might want to do something with the data there because maybe i don't believe it and what i really would do in the real world if i had more time is i might look at the temperatures just before that time step and just after and i might look at the speeds just before and just after and i might look to see if one speed is vastly different than the other two speeds at a given time so there's a whole bunch of more intricate things i might do i'm not going to do that right now though in the interest of time i'm going to um i'm going to choose a temperature and a speed and then i'm going to find all the temperatures that were less than that specific icing temperature and i'm going to find all the speeds that are less than my average wind speed that i said was going to be the ice speed and then when those two things are true those are data i want to look at okay well this is really bizarre looking code don't you think what is that well if i come back to the live editor what you will see here is that i have something called controls up here it was right next to when i could switch to code and i can put in numeric sliders which is what i have or drop-downs checkboxes a whole bunch of things so i can make this much more interactive because i don't know what the right number is so i can come here and i could just compute this right now okay or i can come here and i can change it and now we're going to get a new set of things and in fact i can i can click here and we can see we're saying it's ice temperature we're having we're going to let you change it between 0 and 5 degrees centigrade in steps of 0.2 and i did a similar thing for the i speed but let's put it back at 2 right now because that's where i think maybe is where we want it for the actual analysis and now what i want to do is i want to see if i use those which points are those and um you know what i'm going to come back here i'm going to actually do something kind of sneaky i'm going to clear all the output and instead of using tower location 1 right now i'm going to use wind data dot text and now what i'm going to do is i'm going to run a section and i'm going to come here and we're going to run in advance run in advance and now run in advance and we'll get our plot okay and now i'm going to put these um back in line and i'm going to minimize my tool strip and now you can see i've got a plot here well if i want to see it a little bit better i can pop it out but it's not going to change the window inside there but you'll notice that i have a few um we can zoom in i've got a few points in red that are candidate points where it's uh going to be i'm noting that there could be a problem with these points so i could just look at those points or i could do what i'm going to do later on which is i could just choose to remove those points okay but as i'm doing this i can come here and let's um we can do the same zoom in thing no i don't want to do that go away go away for a second what i want to do is i want to zoom in and let me zoom in and when i zoom in now let me show you what it's showing me that i might want to um if i want to always see that span of time which let's see is december through april it just created the code for me so i don't have to guess what it is and if i want to i could update my code right there and so i get the best of interacting with the with the plot and then committing to code when i'm done i'm going to come here and i'm being not very uh good here i'm going to reset things okay so there i have the plot that we've got and i had i think i'd already run this i had maybe not let's see everything has the same number of points as before so i actually haven't come here and taken these particular points where the icing points are ones where i think that the temperature and wind speed are both low enough that i'm suspicious about whether we're collecting good data and so what i'm going to do is i'm going to run and do that now okay and now i'm ready to come back to the um slides for a second because um what i've done now is i've got to the point where i have cleaned up data and now we can start binning it and if i want to start to bin it well what do you call that well um oh by the way i can close this now i'm all done with that i can come back here and let me open the help in matlab and what i can do is i can look for how to do we can see if it has anything for binning now that's not really what i want okay what if i do it's actually called a histogram um and you'll see when i start typing here there's a bunch of different possible things that i can find and if we look here we see histogram and i'm going to come here and choose histogram and the help is going to help me here hopefully what you'll see is by the way i'm running the latest release it came out last week and you'll see we give a description of what's going on how to create it these are the syntaxes you'd use and maybe i don't know which syntax i want one of the best things i can do is come look at the examples and if there's an example that does what i want whichever one it is i can sit there and i can look at the example if i want to i can grab the code and then edit it and put my own variables in another thing i really like is see also because it shows me first of all when the function got introduced into matlab it gives me some places in the documentation that i can read more about it beyond that the actual reference page and it also shows me some other functions that might be interesting like when i make a histogram by default maybe it doesn't have enough bins so maybe i want to have more bins or fewer bins or maybe i want to do this on a two-dimensional data set instead of a one-dimensional data set got histogram two that i could get so real nice way of finding out more information okay so what i want to do is i wanna get a histogram of the data as a function of the wind speed and i'm gonna say i want my wind speed bins to be 0.5 and so that's what i'm doing here and then what i'm going to do is i'm going to also get the counts because let me just run this for you here's my wind speed from wind data notice how we the title actually gets the file name and puts it in there so we know which where the file the data came from and we also get these uh histogram counts and that's because when i make this calculation or when i make an estimate of it i need the counts and i need to also now tell you about turbines so there's a very brief digression about when turbine power curves they all are essentially um parametrized around the same way they start off with you not turning the power on at all and then they have a speed at which they cut in and that's because at lower speeds it's simply the wind speeds don't generate enough power to be worth it but at the cut in speed we start generating power and you'll see we generate power in usually what's either a quadratic or sometimes a cubic polynomial sort of way until we get to what's called the um rated speed this is the speed at which the turbine actually gets its rated power in this case it's going to be a megawatt and it's going to stay at that rated power until we reach what's called the cut out speed well if we're getting that much power why do we stop it well the reason we stop it is when we get to a certain high speed it becomes very dangerous and what might happen is we might have hurricane winds and what could happen then is we could damage the wind turbine which isn't a very expensive piece of equipment we don't want to do that and worse if like a blade comes off and it goes flying around the countryside it could do damage to other buildings or people or livestock or whatever so we really want to cut uh turn it off safely at that point and then once the wind comes wind speed comes back down we power it back on and we um collect more data then so i need to figure out how to do that uh calculation and so what i'm going to do is i'm going to just tell you um how i do first i'm going to tell you the coefficients that we're going to use for the test turbine we're assuming they're all exactly the same kind so the rated power is a megawatt the cut in speed is 2 meters a second rated speed 14 head out speed 25 and now i want to calculate the um power curve notice i clicked here and that actually ran the section without me advancing now and because i want to show you this code so what i'm doing is i'm i could do this in a for loop so i i loop over all the different speeds and you'd be completely it would be completely valid code and it may be a preferred way for you to do it i'm so used to matlab at this point that i prefer to work on all the data once if i can or in big chunks at least and so what i'm doing is i'm going to take uh combine calculate the turbine power bins by saying it's the rated power um and it's going to be um dependent on i'm going to do that power curve that's the quadratic here so i'm going to take the current wind speed bin square it subtract it from the cut in speed squared and then divide by i'm normalizing this basically and then multiplying by p rated so that if i happen to be in the rated range i'm going to get um the top speed here okay but that's not true for everything and so what i need to do is i need to patch this curve so i need to patch it where the wind speed bins are less than or equal to the cut in speed i want the speed to be zero i also want the wind speed bins that are greater than the cutout speed to be zero and then in that sweet spot between the rated speed and the cutout speed i want it to be the rated power and so in four lines of code instead of a more complex uh if else if sort of statement with a for loop i've got my power curve calculated well let's make sure that's true i am a um i like to verify things and i often do that by plotting and that's the curve i expect so that's good okay and now to calculate the average power if i want to estimate that integral i can just basically do it by the finite um sum of the products and so that's what i'm going to do here we're going to calculate the average power and when i do you'll see that my um average power is 270 kilowatts which is about a quarter of a megawatt um and i can also compute the capacity factor that gives me a percentage so 27 of the power is um is there now which is really good um maybe um but i have all these other things that i need to think about like what are the other turbines doing now one thing that's nice here as i could clear everything out let me do that with my my magic there and now i don't have anything in my workspace again and now if i want to i can actually i took the run button and i can run the whole thing which is really nice so you'll see it's going to go through and if we start going through this you'll start seeing that um it may be done already it's already done that's how fast it was okay so now if i want to i could come here and i could say tower location one whoops one and we could clear everything else out and now we could run this one and when we run this one we want to come here you know if if we think about it um there's the data that i was doing this just to make sure that it was i was doing something sane so i might not need that later on and i'm pretty sure later there's the data that's pretty interesting it's actually got a peak at a lower wind speed than our other wind turbine had but when i look at the wind turbine you know this is the same power curve every time i probably don't need to plot it anymore i was sort of using that as a visual debugger as well so i'm not sure that this this file is running as efficiently as i would like because i'm producing plots that maybe i don't need but right now i have to come in here and edit it by hand and by the way the answer here was 19 so um um what i need to do is think about well how do i want to do this and so what i'm going to do is i'm going to show you what i did and i've got a project that i'm running here and projects have a bunch of different possibilities and they let you make shortcuts and so i've got shortcuts for some of the functions that i did so i created a function that i took basically stripped most of the code from the live script here and i turned it into a function and here's my wind analysis function one and you're going to see it looks very familiar here it's got the time blah blah blah import the data it's got my figures in here and in fact if we come and clear everything out let's see what this looks like so let me come here and so it's expecting a file name let's get the capacity factor equals wind analysis and i'm going to hit tab i'm going to do function one and we want it with the file name and we want when data dot t t x t let me keep that um open okay so what you'll see here is i just got if we look over at my matlab we we see that we got um a whole bunch of plots here lots and lots of plots i told you i like this one because it's a summary of my data but i kind of don't really need that one that was helping me debug before that was making sure i imported the data correctly so i don't need a lot of these and so i might want to think about how to make that a little bit more interesting and also you'll notice that when i um got the information out it printed all kinds of stuff and i'm not sure i want all that stuff printed so what i can do as i can come over to my shortcuts and we can look at the second function and the second function um what i've done is i've made it so that the graphics are optional here so let me clear my workspace out and now let's come like this and now let's say we want to run function2 and when i do i get a different output but i don't see very let me uh did i clear do i have a figure window open i don't okay so if i want graphics now i might have to say i want graphics and now i get one figure and it's the data that i wanted there the summary of the data the histogram so what did i do between wind analysis function one and wind analysis function two i'm gonna come over here and show you so i am um in the right folder and i'm gonna do wind analysis function i have three so i need to see what they look like okay so i'm gonna come here and select the first two functions and i'm gonna compare selected files and folders and um you may not have known this is in matlab but it's actually been there for a very long time and i've got i'm going to actually um swap the sides this is my new function on the left my old function on the right here's a little key down at the bottom here you can tell where we've inserted things the in the blue in the um tannish color we can see deletions and we can see modifications in purple so we changed it so that there's a possible second input and then you'll notice we put in an extra line of code here now i'm just um uh here i just kept i changed the uh the reading the data in i just kept uh i did something slightly different with the file name here here i pulled it apart instead of keeping the file name as the whole thing and you'll see in the original file we had all these plots and they're gone here now and then you'll see once again we removed some more plots and then you'll notice that um what i did was i if the graphics are still there i make the plot so i'm amending this area and then what i do is um calculate the probability range get the information about the turbine and so on so i have i basically have the same information minus most of the plots and you'll also notice that instead of sending one number out i'm sending one output out here so if i come to the top again you'll see it has one output it's called wind data and if we look at wind data we are now sending a structure out with the capacity factor but also the updated average speed and all those sorts of things so if you're working on some some files and you have a a pristine copy and then a working copy you can compare them from time to time just to make sure they're doing what exactly what you want okay um now i want to also make this a little bit friendlier and i'm going to show you what i mean here let me clear the workspace out again and let me close all my figure windows and let me say if i come back to wind analysis function 1 we know that it worked if i gave it that file but what if i accidentally say windata one i get a lot of reds filled in red ink spilled in my command window and you might not know do i start at the top do i start at the bottom this is really a drag well instead of doing that this is typical me i go well maybe it's actually the problem that uh i don't need to give it the dot text and so i get a slightly different set of error messages here or maybe maybe it's the same one or i can say oh you know what i'm doing one through twelve maybe i just need to give it a number let's give it three well no it can't find that either and now i'm so frustrated and i'm just banging on my keyboard and i get different error messages all the time okay so let me clear out the workspace again and now let me show you the same things but with my window analysis function three so first of all i'm going to come here and actually run it with the data i have to take the one out here and let's make sure it's still running oops i'm in the wrong folder maybe that's probably what's happening i'm in the wrong folder okay so it ran it got me information that makes sense i got my one plot and it gives me a structure of information back now what happens if i make mistake number one where i put an invalid name it says it can't find that file okay what if i'm a mistake make mistake type 2 where i give it a number and it says oh we were expecting a character but you gave me a double aha that's a little more helpful and what if i say i just don't know what to do it asks me how polite of it and i i can come here and say open and i get my i get my answer so how did i do that well what i want to do is come over here and come to my project shortcuts and i want to open the third function and it's right here and what i want to do now is i actually want to show you how to figure this out so what i want to do is okay so i'm going to set a breakpoint here i hope and now i want to run that again so let me um because i want to know how many arguments it's calling that's what nrgin lets me do so let's clear this again and now let's up arrow and um let's short let's follow this one no inputs it stops here and then i'm at the matlab prompt the debugger prompt so i can say nargin it's zero so if it's zero what does it do well let me bring the editor part of the tool strip down now it says let me make a step um it's why didn't it step oh it did step okay it's going to get the names let's get the it's going to ask for the file i'm going to come here in demos and get that file name and then we step and now i'm just going to continue because it's going to go to the end so i stopped in the debugger and was able to validate something as we went well let me clear this out again and now let me try one of the other ones let's call this with the number or let's call it with the wrong name when data1.txt okay so if nr1 well what is nr again now it's one so if i step we're going to come to the else part of this and then what it's going to do is it's going to say is the first input a character row vector and if it's not um if it is it says fine but now look for the file and i can't find the file so it tells me that so i've written this to be much friendlier so that if you go to use it you're going to get a file an error message that helps guide you and tell you what to do and if we look again with that file diff the differences i can take the um function three and now i'm going to compare it to function one and we will do the same thing that we did a moment ago and now you'll see that there are some differences first of all we still have the plotting things over in in first file on the right but you'll see the only main edition here is on the left is this big block of code at the front and maybe some white space and then finishing how we return the output and the structure so the main thing is i'm putting in handling different inputs and i'm giving i'm opening it if i'm opening the file if uh i'm getting the file name if someone selects a file name otherwise i'm checking to make sure that the file name is the right data type it's actually a character array and i'm looking to see if it's a file and if not i get different error messages depending on what it is and then once i have a valid file name i just go into the rest of the code and it just works okay so i am um not done yet because we actually said what we wanted to do was um figure out which turbine is at the best location right and i haven't done that yet so now um that you see sort of behind how i'm doing this i want to show you my final script here and i'm going to remove all the outputs that are in it and now i'm going to make it bigger so you can see it it's not the one i want i want this one let's clear this out clear all the output so um if we come back over here um here i have that folder that when data files and you'll see i have six files over it but often when you're doing this you have hundreds of files and you know i kind of really don't want to think about i can count them i can find out the number of files and then loop over that number but i don't need to think about it that way i can be more programmatic about it so what i'm going to do is i want to get all the files i want to basically look at all the files that are in that folder here so i'm going to get the full path to that directory and then instead of loading the data in i'm going to create a reference to it that's what a data store lets me do so i would encourage you to look up data store in the documentation and we're saying it's tab delimited and we're saying that the first input is going to be a date then one of those date time things and then i actually have my parallel pool open here i happen to have a four core machine so let me just run this and the reason i want to do this is if you think about it if i have files from a bunch of different different files i can actually analyze them in parallel if i have enough resources here and so here it's showing me what information there is about this file and i happen to have three files plus three more that's six files and it's telling me how it interprets this and if i don't like the way that is i can go and change that but it's okay right now and so i'm going to open the parallel pool here now and it's already there and what i want to do is up for from the number of files it's going to tell me how many files were in that data store here and what i want to do is i want to compute the capacity factor this is with a slightly different version of a wind analysis function here and what i'm going to do is i'm going to read the capacity i'm going to get the capacity factor for each of the files now what i want to do before i run this is i'm going to bring up my um task manager from windows and i'm going to come over here and make my matlab smaller because i want you to see what happens so right now you can see i'm you know i used some some cpus a little while ago but we're not doing a whole heck of a lot right now but now i'm going to do this and i'm doing it in a for loop but not a regular for loop a par for loop it says use all the resources that i have locally in this case because that's where my pool is and now what you see is all of a sudden um my cpus got used a whole lot but they're done okay and so i took advantage of that to um unload my my local matlab if you will now if you had a lot more files what you would do is you might have your pool not on your local machine but on a cluster or the cloud where you have access to more resources and then you just make sure that it has access to the the analysis function and the data and then you can point to the pool and um run that there so i've i've done that now now i just want to get uh which one was the maximum capacity because that's the one where we want that's the location where we want to place our turbine here and now what i'm going to do is i'm going to create a bar chart of the capacities as a function of which one of the towers and you see what i do at the end is i add one more bar for the location that had the best one which happened to be turbine four now once i have this it might be important that i share this information with someone else and so you'll see here i can export this and let's export it to a word document here let's say here's the analysis and i might send this report to my supervisor or something like that here's what you need to send to the people in the field here's the analysis we did and by the way if we want to we could actually create the report instead by hiding the code maybe they don't need to see the code but they want to see some of the details or they just really want to see this i can make a report that doesn't have the code in it or has the code in it either way okay and with that i'm going to come here and whizz along um i talked to you about matlab i showed you by using what i called what i'm going to call logical indexing how i could combine and look at all of the wind speeds at once rather than calculating them one at a time in a loop um so that's because matlab is a matrix based language and you can do those sorts of things really well there's a lot of functionality whether it's histograms or regular plots xy plots that let you do all kinds of engineering and scientific sorts of calculations and i started off if you remember at the command line and then i started working in a live script and then a script and then we made functions and so i'm working along that whole continuum of uh uh programming techniques and the very last one that we might do would be i'm not going to go into the the details here if you um uh look in the channel there will be a location where you can download the code and demos from today and you'll be able to see this virus demo and the idea in this one is using different kinds of functions in there so i can make the code as i did here by putting that if nrg in and making my function nicer so that when people got to it they understood how to use it because they made the error messages more legible if you will and i even made a case that used to give an error with no inputs do something useful so um i have a lot of the development tools and i showed you uh the debugger what i didn't show you is the code analyzer but i've showed you a lot of different techniques now so what is a program well a program actually combines um data and algorithmic information and we can actually take that to a further extreme by letting the program um contain the let by building a class this is typical object oriented um notation when we make a class what we're doing is we're going to combine code and data methods algorithms and data or properties and so if i if i think about it i have a class that's a blueprint for the kind of entity i want to talk about if my class is dog you can see that i might have different kinds of breeds the ages the colors of the dog and the dogs can do different things if we then talk about specific dogs those are objects we have lexi here and luna there okay and those are objects of a class so the class is a as as a sort of an abstraction and instances are what are the objects and matlab is fully object oriented and again i'm going to make a little plug for two weeks from now when i will be doing a session on object-oriented programming there but i showed you we could share results from matlab i also could have showed you that we can make apps with using app designer but i can share documents i can share apps i can share reports like we generated either from like a live script or using something called the matlab report generator and i can also take my algorithms and if it's just the algorithm i can use one of the coder products and make it available so it can be used in a different system where matlab doesn't exist at all and it can be even an embedded system or i can choose to take my entire application including any graphics and any um applications that i make and using the matlab compiler or the matlab compiler sdk i can have something that actually makes an app that's a web based app i can make it possible to link your app in a situation where you're going to use resources that may be in other languages so there's a lot of ways i can take the information from matlab out and use it repurpose it with other tools and i'm not going to go through here i'm just going to zoom to the end yeah so um hopefully today you got the idea that matlab is a high-level language it allows you to have the best of all the worlds with both programming and a lot of interaction and interaction that generates code for you um i showed you a lot of interactive development tools the debugger the editor the live editor and things like that and i showed you that supports a range of programming styles and thank you um and then i'm just listing some of the tools we talked about we did data visualization we we didn't use the profiler today um but we did a lot of different um way we showed a lot of different ways of looking at the tools one last thing i want to leave you with and i'm going to come back to my matlab to show you this in the help if i come back to matlab here um they i can look at the release notes and when i look at the release notes you'll notice that this scroll bar is teeny tiny and it's a little bit intimidating well i can come over here and i can say well you know what right now whoops let me come here right now we're looking about the changes from 18b to the current release suppose i actually was most recently using 19b well that may make it a little bit better it's still pretty long but now i can also come here and i can say you know what i really care about the math and the data analysis and when i do you'll notice that i have a much smaller scroll bar here but maybe still a little bit long but i can start seeing boy there's a lot of new stuff in there that's kind of cool that's one way to learn new things if you don't yet want to learn some new things though i can come here and i can say i just want to check for incompatibilities anything where it may cause a problem for me so now i come here and we look and we see and i go i never used a timetable before so that doesn't matter so those are good so you can go through very quickly and assess what's new what's interesting what you want to delve into more and what might have changed that might be worth looking at and with that um there are a lot more resources online there will be um this video will stay up on youtube so you'll be able to listen to it again and with that um you might also want to take um the matlab uh training course map fundamentals and if you've never used matlab before and you don't have access to matlab yet you can go to our website you need to create an account and you can use the matlab on-ramp and it will give you two hours online uh free tutorial for you to begin to learn um how matlab works and that would be a great way to get started and that's that's it so hopefully um you have learned a bunch um uh i think we're starting soon again with the uh simulink student challenge i'm not sure about that um i think it's coming up we anyhow we have a bunch of competitions go to our website and see what's happening because you might want to join that i know it's also the 20th uh anniversary of our community pages so you might look there as well and also um subscribe subscribe to the mathworks channel to get reminders for next events and i look forward to seeing you in a couple weeks thank you you
Info
Channel: MATLAB
Views: 3,046
Rating: 4.9793816 out of 5
Keywords: MATLAB, Simulink, MathWorks
Id: jogGxRr_sXY
Channel Id: undefined
Length: 65min 10sec (3910 seconds)
Published: Thu Oct 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.