Posted on

Learn Matlab Episode #7: Data Types

Click here to subscribe for more videos like this!

Data types in MATLAB

In this matlab tutorial we’re going to be talking about different kinds of data types, so things that are not what we’ve seen so far such as numbers, scalars, vectors, and matrices. So one thing that we’ve been implicitly looking at are strings. So whenever I display something like this and I put it into single quotes the thing that is inside it is a string. Later on when we look at opening files we will specify the file names as strings. So a string is basically any sequence of characters and so we’ve seen the workspace but there’s also a function called whos that lets us look at all the variables in the workspace and see some stuff about it. So you’ll notice a column called class. So far everything I’ve created is a double so we have no actual strings yet. If I use the class function and I look at an actual string, let’s just use the I found it string again, you’ll see that it returns char. So if I set a variable called my_string = ‘I found it!’ and I type whos you’re going to see now that there’s a variable with the name my string size 1×11. So strings are considered row vectors and it’s of type char. So the next topic we’re going to talk about is structs. We’ve seen so far in matlab numbers, matrices, vectors, arrays, strings, but that doesn’t leave a lot of flexibility compared to other programming languages. So if you’re familiar with Python maybe you’ve seen the dictionary data type, or if you use Java it would be the hashmap, or if you’ve coded in javascript you may have used json. If you’ve ever used any of those before you know that those data types are very flexible because a json can store other jsons, it can store arrays, whereas in matlab we have so far been constricted to only numbers. So in matlab when you want to declare struct it’s very easy so call our struct my_struct.name = ‘My new struct’ Ok, so this creates a struct with one field which is referred to as name and the value is my new struct. If I do class on my struct it tells me that it’s a struct. So the cool thing is you can add new values to the struct dynamically. So I can say my_struct.age = 25, so now my _struct contains two fields name and age and notice that they are different types. So if I do class(my_struct.name) it’s a char, but if i do class(my_struct.age) it’s a double. So there are also some useful functions that act on structs. So is field will tell you if a field exists within a struct, so I just checked if the field name was part of my struct and it returned one because it is. Let’s try gender. So that returns false because gender is not a field in my struct. There’s a method called rmfield. So if I want to remove a field from a struct, I spelled it wrong, so now my struct only contains the field name because I removed the aid field. There’s a function called set field so you might want to use this if for example the name where the value is set dynamically while the program is running, so you can’t do my_struct.something you have to set the field some other way and I spelled field wrong again, so now you can see I’ve added a field called gender with the value F. So the other thing I talked about is that a struct can actually contain a struct. So suppose I want to add a new field to my struct called contact and I want to give a phone number to my contact. So I’ve set the phone number on this struct and so you can see when the struct prints out now the contact field is just a struct but it doesn’t print what’s inside, and so now since contact is a struct I can set other fields as well. Ok, so my struct now contains the contact field but as a struct and the contact is also instruct. So the last thing about structs that I want to mention is an alternative way to initialize a struct. So far we’ve been manually entering each struct value one at a time but you could also do something like this, S = struct(‘name’, ‘Bob’, ’email’, ‘[email protected]’) right and so what that does is it takes in an arbitrarily long list of parameters where the first parameter is a field, the second parameter is a value, third parameter is a field, the fourth parameter is a value, and so on. So the last data type that I want to talk about today is the cell. So the cell is very similar to a struct in that it contains arbitrary fields, the difference is you you might see a cell as more of like an array that can hold indices other than just numbers versus you might see a struct as more of an object that contains information about some some type of thing, and then a cell might be you know like an array of those things. So the cell syntax is very similar to the matlab matrix and array syntax. Ok, so to initialize our cell we’re going to use curly braces we want to set the field to 1, doesn’t have to be an integer, going to set it to the value hello world, ok. So now if I print out my cell it simply contains hello world. Ok, so now i’m going to set another field, I’m just going to call it A and it’s going to be a matrix. Ok, so now I have two keys and two values for my cell. I have the key one in the key A, and then the values hello world and then this two-by-two matrix. So what I can usually do with matlab variables just type it into the command window and see what it prints out. You can see that it has printed out a lot of stuff. So you generally won’t want to do that but notice how now I can access key 1 and it returns hello world, and I can access the key A and it returns the two-by-two matrix that I previously stored. So this is good for when you want to store things by name for example instead of trying to put it into an array, and you can also store heterogeneous information so different types.