MATLAB treats most numbers as doubles but you can force different classes like unsigned integers or longs.
- [Instructor] Let's take a quick look at how MATLAB works with the value formats. We can see for all the variables and vectors we've created so far, MATLAB saves them as a double. A double is just a 64-bit floating point number, meaning it can have a whole number and decimal part. But we don't have to keep values in that format. Let's create a new variable here. I'll call it myPie, I do enjoy some pie, and I'll set it equal to pi, p-i. MATLAB has that constant built in. I'll hit enter without a semicolon here so we can the return, 3.1416.
You can see that MATLAB's rounding off the value here to four decimal points. Just a quick note, MATLAB isn't actually rounding this off, it's just showing it to us in a short format. We can change how MATLAB shows things to us on screen by using the format command. For instance, if I say format long, and then ask it to show myPie, we'll see it shows us much more digits. If we're looking at lots of values on screen though, that can sometimes get overwhelming, so we're going to keep the format short for now.
You can always check out the MATLAB documentation to see all the different formats available. I'm going to ahead and clear the screen. So we see that MATLAB set the default value of myPie to a double. We can also change that to single precision which is a 32-bit value, if we want. Going to say myPie single. And we just use the function single and then in there we pass it myPie. We see the MATLAB's telling us explicitly now that it's a single precision value.
Please note if you don't see the class column in the workspace, you can always right click near name and value, and choose class from the drop down menu. And we see in the workspace, indeed that's true, myPie single is a class of single. You might do this if 64-bit values are overkill and you want to save some space when you're creating, maybe, millions of values. MATLAB can also deal with booleans. You can easily create a boolean by creating a variable and setting it equal to either true or false.
If I say true, we'll see that MATLAB says, "Oh yep, this is a logical value," and it's one. Again we can see in the workspace, it has myBool and notice that it's icon is a checkmark indicating that it's a logical value and not a matrix, like the other ones. We can also convert a numerical value to a boolean, if we want. Say myNum here for my number, and I'm going to set it equal to zero. And now I'll say myNumBool, and we can use the logical keyword, and pass it myNum, and since I was zero, myNumBool should be false.
And we can see that it's equal to zero. Most of the time we can just get by with letting MATLAB assign everything as a double. But if you ever need to change it, now you know how.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models