When you import text values from a file, you can never be sure what form they’ll arrive in. There could be extra blank characters, or perhaps text that needs to be in all uppercase or lowercase. In this video, learn how to process your strings so they are in the form you need.
- [Instructor] When you import text values from a file, you can never be sure what form they'll arrive in. It could contain extra blank characters, or perhaps you'll need to transform the text so that the letters are all in uppercase or lowercase. In this movie, I will show you how to process your strings so they are in the form that you need. I'm in a blank MATLAB command window, so I need to start by defining some variables. I'll start with str1, so just string number one, equal, then a single quote to indicate I'm creating a string, and for this one, I will add a number of blank spaces at the start and then type degrees and close it in single quotes, and enter.
Next I'll do string two, str2, equals, single quote, and for this, I will have blank spaces at the beginning and end. I'll just type a number of spaces, it doesn't matter how many, just need to have some there, spaces after, then a single quote and enter. Just to see what happens I concatenate these two strings together using the square bracket method, so I type a left square bracket, and then str1, a space, then str2, followed by a right square bracket, and enter.
You see that I get degrees 95 and it's awful. If I do str2, followed by str1, and again that's in square brackets and enter, I get 95 degrees, and it's still pretty bad, but at least degrees doesn't have any extra spaces after it. The first thing I want to do is to get rid of the blank spaces from the end of my strings. For that, I can use the deblank function. I'll say str2 for string number two, equals, then deblank, followed by a left parenthesis and str2.
Basically what I'm doing is assigning the output of deblank of string two back to the same variable. I've closed the argument list, just a single variable, with a right parenthesis and enter, and there I get it. Before, I had several spaces after the characters 95, but now the blanks at the end are gone. If I want to remove blank spaces from the beginning and end, I can use the string trim function, and that's strtrim.
First we'll do it to string one, so we'll do str1, equals, strtrim, string trim, of str1, end parentheses, and enter, there we go. Then we can do str2, equals, string trim, parentheses, str2. So again, we're just operating on a variable and feeding the value back into itself, and enter. And there we go. If you want, you can also change the case of the characters. Right now I have degrees which is the value in string one, which is all lowercase.
If I want to change the case of the characters in a string to all uppercase or lowercase, I can do that. Let's say that string one will equal upper, which is the transformation to uppercase. After upper I will type a left parenthesis, string one, just feeding the same variable value back, add some parentheses, and enter. Degrees is all uppercase. I can change it to lowercase, so string one, equals, lower, left parenthesis, str1, right parenthesis, and enter.
All the characters are back at lowercase. As you can see, you have a lot of control over the appearance of characters within your strings in MATLAB.
- 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