Intro to MATLAB - Week 4 - Data import and plotting in loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is one more example of playing with data in MATLAB and I will do this demo on an excel file I have so this data file contains some information from Nordic electricity market and so it has the timestamps or the dates and then electricity spot price from Finland then spot price from two swedish areas and then it has regulation price up and down so this one means that electricity price will be in a given area can be on hourly basis regulated up or down based on demand and supply and then we have some background information so some temperature data and transmission capacities between the areas so I'd like to get that data in let's try import data my file was named 2016 and of course I need to use the signs and I have to give the extension and that was an excellence X what all right let's run it and see how do we get it in okay so we have data itself 13 columns and 1699 rolls let's count yes so all the data is in in that matrix and we also have text data and column headers so these both are the same so they basically carry the headers we had in the acceleration early and they should be they they are selling celery because the lens of death texts may be different but at least we have the variable list for instance if we want to do some plotting and we can we can then use those column headers from here as ready names of the variables ok and now as I was telling you some data files are big this one wasn't so big so the import from Excel didn't take that long but some data files may be really large and this import from Excel or from CSV takes quite a bit of time so for that reason once you've imported the data once it's good to save it so now when I say stayed let's say electricity data so now when I press enter what MATLAB will do it will save into that file so it will create final electricity data and it will save into it the content of my workspace now workspace has only one variable and it's called X if I had more variables here and I didn't want all of them I could then type here specifically that I want X to be actually saved now it doesn't really matter whether I type this X or not it will give the same results so I save the data and now I can remove this import from here and all I need to do is load electricity data so now I will first first run the clearing so you see the workspace gets empty and then I run the load so you see this variable here called X which I had before is still I mean appears again in the workspace because it has been saved in the data part and then in your current folder somewhere you can actually see this electricity data as a mod file ok so now let's say I want to start from just plotting the data and let's say I load this data first time so I don't know what it contains so once again let's see it has 13 columns of the data and it has 13 column headers so I will plot them like in two columns in subplots I'll make a figure I'm also so that I don't have to type every time X dot data I'll call that just D so I get out my numerical data from the structure and then since I will need 13 subplots I don't want to do them manually so I will make a loop and that will be from 1 to length of length of column headers we can also make that actually I'll call it n bar so number of variables so that I don't have to type that long name every time so I'll use n bar here ok and what I want to do is for every variable in here I want to make a subplot for it and now how how do I write my subplots as I said I would like to arrange them into columns and so that I don't have to worry and count that yeah I would need actually seven rows of plots if I have 13 variables and so on I can make much love do it for me so I know that remember that in subplot you kind of need to put in three things first one is how many rows second how many columns last one is which one you are plotting at at the time the last one here is simple because we are doing since we are going through the variables one by one that's the i'f one that we want to be plotted and actually yes from those we could actually omit the first one because it the date so it's bit silly to actually plot the dates so in that case I'll be plotting I minus one because in the first subplot I want second variable second subplot third variable and so on okay I said I want a fixed amount of two columns so that's clear as well and now in here I I want MATLAB to calculate for me so I have n variables but since I have two columns so I divide it by two because that's the number of rows we need in case the number is odd we round up words so we use ceiling and since we actually took away the date from plotting so we actually don't want all the variables we want all but one so that's why it's n bar minus one so here I have my subplot prepared if this seems too difficult for you then you can do that math yourself so you would know actually that there were originally 13 variables we take away one so that's 12 left two columns so you could simply type subplot 6 2 and then I minus 1 in this case but I like my codes to be prepared in such a way that they would work for any data that I have so later if I have another data set I would like to do the same kind of plotting all I all I need to do is change the data which is being loaded and the rest of the code will adjust itself okay and now what I want I want to plot that particular variable so I know the variables are columns in my d matrix so I plot the I've column make axis tight and I want to know which one is which so I will put the titles and now I know that the title of my specific variable is in the I've element of this celery X dot call headers so again maybe just to save some typing I'll define variables here oh yeah here I maybe should have done it if I call this n VAR this should be maybe the length already but okay maybe let's do it like this so I don't call it anywhere I just call it var so this actually stores the variables because here then I refer to length for that that's okay so I know that it's variable var and it's I've element but since it's a cell array I have to use those curly braces so I put I in there and then I have to end my loop okay let's run and see if we crash order on anything undefined oh yeah I had n bar here as well yeah I have confused myself and var with with actual list of variables so I will maybe make n bar here after all define it as length Oh and so I can use just n bar here and I have n bar there and then I have four yes so I see even simple codes can have some simple mistakes but it's important that you can trace trace what has happened so you see it's it said here undefined function or variable n bar since for awhile I had this environ removed so matlab didn't know what I actually meant so now we run then we get the plot so we have our prices and we have our temperatures in here and we have transmission capacities so this is basic example of plotting your data when you have set of variables what else could be done in here for instance you would like maybe to see correlations of pairwise variables and which correlations could be the most interesting maybe how much spot price is correlated with temperature so that's our first and six variable remember that sub nodes are numbered in this way so one two three four five six so that's a simple scatter plot figure and the scatter plot can be either done using ready scatter function in MATLAB and you give the two variables you want to be plotted so in this case it's the first column and the sixth one I forgot to combine here sit tight and I'll run this when you have a scatter plot like that normally that means you want to probably verify a level of correlation in your data oh yeah I made a mistake now myself so it wasn't 1st and 6 once again first variable is actually the dates so this should be actually 2nd and 7th how did I know because of the weird look of the plot it couldn't be like that but this if you look at the numbers this is how the dates are understood in MATLAB so once again run yes now it makes a bit more sense so we had the prices here and the temperatures here and when you have scatter plot like this as I was saying most likely you'd like to check level of correlation between the variables and maybe have that information in the plot so first of all our label X label these were both prices and while Abel had temperatures and then so for instance correlation if you don't know already function that check correlation in here correlation coefficient between two antennas in an array let's try again okay for instance we have in here cork wife returns matrix of correlation coefficients for a or the columns of a represent random variables and they're all represent observations also that's if you have a matrix of variables now we just want two specific variables a and B so this is that second and seventh column so let's use that one call it C coracoid of these two now give an individually with a combine between let's see what that brings okay so it actually anyway brings it as a matrix but that means that on the diagonal we have the variables correlated with themselves and then in here is the actual correlation coefficient between the two variables so my coefficient I'll call it like this is actually from matrix C instance first row second or I could use two one as well so either this or this its symmetric so it doesn't really matter and I would like now that cough information to appear at saying title of my plots so the title normally would be just a string but I wanted that number included in there so I will say correlation between and now we are combining an array of pieces of text that's why I have to include the square brackets and I'll be putting those pieces so this is a fixed text then I'll add the names of the variables so I will use bar 2 and bar 7 equals let's call it C and then we put here the numerical value now the these I don't have to worry because they are strings the names of the variables but this number is not a string so I have to convert that string to an um the number to a string so the trick was using new to string for instance and then I put that coil in there so now in the title of my plot I should get that full text wherever toolbar 7 and this numerical value of correlation coefficient should now be pieces of text and you can see it in here correlation between elspeth fee and them health equals that
Info
Channel: Professor eM
Views: 1,437
Rating: 4.3333335 out of 5
Keywords:
Id: dOJPWD_CjQ4
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Mon Sep 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.