Intro to Structures in Matlab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody so this week I thought would try something different I'm gonna put up a little video and so you guys can follow along at your own pace and I'm just gonna run through some stuff that will be I think will be very beneficial for you before you get into lab so try to watch this before you get into lab and follow along alright so let's get started this week we're gonna be working with things called cell arrays and also things called structures so to understand both of those we have to kind of understand what arrays are so we've actually been working with them the entire time but it might not have occurred to you that that's actually happening so let's just take a let's make a let's make an array in this case let's make one that's zeros with two rows and two columns okay there we go and X has two rows two columns and it's all zeros let's see what else I can reference a specific spot in this array like the first spot and I can manipulate the data in there so let's make this a value pick a number any number seven all right and so there you go in the first row first column I have added I have manipulated the array and put it in the number seven so a good question to ask though is what happens if I try to do something different like maybe I put in a different kind of data for instance how about I want to make the second row second column equal to the letter A what do you think is gonna happen let's find out ah so I didn't get the letter A in the second row second column I got the number 97 and to understand what's going on here we have to use Hoos and we see the variables that we have so we have X a 2 by 2 matrix this is how much memory is taking up and then the class this is the kind of data that it holds and as you can see it's double which is a way of storing a number so it turns out that arrays are kind of like egg cartons they have rows they have columns and you keep one thing in them you keep eggs so if I try to put in a letter instead of a number I MATLAB sort of converts it into a number for you we saw sometimes there's not just rows and columns right sometimes there's a third dimension to it or you could have actually as many as you want but but there's a third dimension to it we saw this last week when we were manipulating an image because we had a every row every column represented a pixel in the image right and then that there was a third dimension to it there were three of those and each one stood for the red the green and the blue value at that particular pixel so so anyway what we got here then is a matrix of double so if I try to put in anything other than a double it will convert and in some cases it won't fit so let's try making a different kind of array let's make an array called C and I'm gonna make it equal to the word Toyota okay all right and I can also access so C is also an array and I can also access say the let's see the third spot in C is why okay so in this case let's check whose I made C and C is a 1 by 6 because guess what there's six letters in here T oh why Oh TA right and it's type char that's the class right okay so now I wonder what happens if I put in if I try to reset this when I make it 97 let's try that aha Toyota so you can see here that what's really happening is that the data right is all being stored in binary ones and zeros right and what those ones and zeros mean sort of depends on your perspective so from matlab's perspective it looks at the data and see and it sees everything there is a type char type character so when it sees 97 it's going to write a when it sees a 97 in a double matrix it writes the number 97 so it's all depends on your perspective and so we've been dealing with this for a couple weeks and we've only been making matrices that sort of or an arrays that have one kind of data in it so the question is what do you do if you want to store more than one kind of data well for that we're gonna need a new kind of container maybe something not like an egg carton so much as maybe something like Lunchables container right Lunchables containers you can put different kinds of food in here so this might be your you know tomato flavored Chara characters and here's some string some strings you know cheesy string type characters and then and then what you have this might be like a matrix that has different types of double values in it right so this sort of is representative of a cell array so let's make a cell array so I do that by making let's say let's call it D is equal to cell and I'll make this a 2 by 2 and here we go ok so I've got a 2 by 2 array and it's got these boxes in it so I can manipulate the cell array and let's just check who's okay so here we go D is a 2 by 2 it's taken up this much memory and it is class cell so class the the cell means that in each of these is a cell and I can have whatever kind of data I want to name so let's say let's try this so when you're referencing a cell array it's not an array right it's it's a it's a different kind of container so I need to use curly brackets to let MATLAB know that I'm you're not dealing with your average array here this is a cell array and so let's go manipulate second row second column oops okay and I'll make it equal to the letter A and voila I have stored a character in here now just to double-check this I can now put in whatever kind of data I want for instance the number 11 okay so let's fill up the rest of these cells what other kinds of data could we do I could make it a string right which is just a bunch of characters Oh and so using the curly brackets every time second row first column so I can literally put like just about any type of data type you could think of like for instance a list of numbers right so I can actually have an array inside of this array it says one by four because there's four numbers in here if I look for it I just have to do D of two comma one what is that that's that array of numbers okay so this is a very useful structure sorry not the word structure this is a very useful way of storing different kinds of data in a celery okay now what's the difference between the cell array and structure well if a cell array is like a Lunchables box with different holes for different types of food a structure is more like a garbage bin that has labels for each so it's basically the same thing as a cell array except that each of these has got each of these different compartments now also has its own kind it has its own label so for instance here's a here's a trash bin it's got three different garbage bags in it this one's for paper only this one is for general waste this one is for glass and plastic so well let's manipulate one of these so to make a structure I have to use this truck STR UCT struct alright and then here the first thing is going to be well so it goes like this field and then your values so the field would be am I looking at glass or plastic let's try this let's make my first field I'll call it paper the paper all right and then in here I put I put the values for paper for the the paper values so I'm gonna leave that empty for right now we'll see in a minute let's see another one I'm making a structure it's also got one called glass and also leave that empty then I have a general sort of waste okay even those blank let's see what happens F is a structure array with the fields paper glass and waste whoo I misspelled waste uh-oh never forgive myself okay so now let's put some stuff in here so to do this right now I it's a zero by zero which means I have zero waste paper bin so let's talk about let's talk about the first waste paper bin so I need F oops F waste paper bin one dot paper and I cannot spell let's make that equal to two okay so now we're looking at the first wastepaper bin has got one piece of paper in it no glass and no waste so that's how you manipulate a an array a structure array okay and actually I can do again I can do there's no rules on what kind of data goes in here so so if I just print an F says I have a 1 by 2 structure array with these fields right now let's consider F of 2 I've got a lot of paper no glass and a waste all right so this is gonna be really useful you'll see for manipulating data and accessing it I sort of use a Oh so to be clear if I want to adjust the amount of glass I would do F waste bin number 2 right F 2 dot glass is equal to 15 glass bottles okay and then if I look at it F of 2 now has a lot of paper in it and 15 glass bottles okay so I'm gonna put up another video in a minute and we'll do an example using structures to manipulate data and story all right and don't forget to leave comments I really need comments to figure out how to make these videos better ok thanks
Info
Channel: Jason Sharkey
Views: 20,989
Rating: 4.8333335 out of 5
Keywords: Screencast-O-Matic.com, Matlab, nmt, structures
Id: Hz5m-hLL_bg
Channel Id: undefined
Length: 14min 4sec (844 seconds)
Published: Mon Mar 04 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.