While you will usually receive your data in the type you expect, meaning a string or a number, learn how there might be times you need to convert strings to numbers, or vice versa.
- [Instructor] MATLAB programming can be an interesting undertaking. While you will usually receive your data in the type you expect, meaning a string or a number, there might be times you need to convert strings to numbers or vice versa. I'll show you how to do that in this movie. I have a blank command window. And so the first thing I'll need to do is to define some values. I'll start with a decimal number that I'll call val one equals and I'll just say 6.94. And I'll type a semicolon so I don't get the value echoed back.
So enter, and I can see in my workspace at the bottom-left corner that I have a value of 6.9400. So it looks like MATLAB will extend it to four decimal places. Now let's say that I want to assign that value to a string, and convert it to a string in the process so I can use it in text handling. For that, I would do s-t-r one equals and then the function num, n-u-m, two s-t-r. So number to string.
And this is used for decimal values or for numbers that could contain decimal values. Type a left parentheses and val one and I need to close the parentheses and enter and there I get the string 6.94. And if you look at the workspace in the bottom left-hand corner, you'll see that the string is exactly what I put in, 6.94. But MATLAB represents the number internally as the number as 6.9400. You can also convert integers, that is numbers without decimal components, to strings.
So let's say that I have val two equals five. And again, semicolon to suppress the echo so more commands can stay on the screen. If I type in a new string variable, s-t-r two equals I can use int to string, so i-n-t to s-t-r. It follows the same pattern as num to string. Left parentheses, val two, right parentheses and enter and I get the string of five. And you might wonder what would happen if I were to use int to string on a decimal number.
Well, let's see what happens. I'll do s-t-r three equals int two string val one, which remember is 6.94, enter, and it rounds it up to seven. So rather than truncating it, or giving me an error, instead it says, you want an integer out of this? Okay. Closest one is seven, here you go. And of course, if I did a string four equals num to string of the number five which is in val two. Enter, then I would just get five.
It works as expected. You can specify the number of characters that you want when you are converting a number to a string. So let's say, for example, you want to convert Pi to a string for use in text handling. Pi is a non-repeating, non-terminating decimal. So you need to tell the program how many digits you want. So in this case, we'll say string five equals num to string, 'cause this is a decimal, and we don't wanna round to three.
That will be Pi, which is a keyword within MATLAB, 10. And enter. And I get three plus the first nine digits of Pi for a total of 10. I can also convert strings to doubles, double position numbers. So, for example, if I did val three equals string two double, left parentheses, I have the first 10 decimals of Pi in string five. So I'll do string five, right parentheses, and enter.
And I get my string, which has been rounded to four decimal points, 3.1416. And also, string to number can work on multiple values. So let's say that I have a vector of four strings that could be numbers. So I'll call it v-e-c one equals. And I'll do string to number, string to num of the string three, space 14, space 15, space nine.
Close it out with a right parentheses and the string is in single quotes. Enter and I get the numbers of three, 14, 15, and nine.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions