In today’s lesson, we will cover the following topics:
1. Scripts (aka Script Files, M Scripts, M-File Scripts)
2. Strings
VIDEO COMING SOON!
Scripts
So far, all the commands I have taught have usually been typed directly into the Command Window where each line of code runs one by one when you hit ‘enter’, and when MATLAB is closed, the code is not saved (recall you can use the ‘diary’ command to export the code in your Command Window to a text file … but that’s still not the same as saving a MATLAB file directly).
We can solve these problems with something called scripts!
Also known as script files, M-File Scripts or M Scripts, these are the simplest type of program files. They contain all your code in one file and execute all the lines of code together, in the order you type them. This is the same as the code we’ve been practicing by typing into the Command Window, but now you can type it into a script instead! There are three problems that using scripts will solve:
(1) First, we will be able to actually save our code in a MATLAB file. This extension for a script is .m, which is why they are sometimes called M-File Scripts or M Scripts, as I mentioned above. MATLAB will directly open this file up in its program, as compared to code saved in the text file with the diary command discussed in an earlier lesson.
Note: .mat is another MATLAB file extension. These are files that save the variables in the Workspace, but not the code itself. Your actual scripts or functions (we will get to functions in another lesson), are saved in .m files.
(2) Second, we will be able to type multiple lines of code into a file and execute it all together, rather than having to hit enter and run each line one by one like we do in the Command Window.
(3) Third, if we want to run the same code again, or if you want to add or remove a line of code, you will need to rewrite each line all over again and run it one by one in the Command Window. With a script, you can edit the code directly in the script, then run it all together.
Note: Script file names cannot start with numbers, such as 123newfile.m. Do not use a space or a period in the name either. If you want to separate words in the file name, use underscores (e.g. new_file.m).
So how do we create a script?
On the top left of the MATLAB screen, there is a button that says “New Script.” This is circled below:
Once you click it, the image below is what the screen looks like. The top half is called the Editor window where you will type out your code (this is the “script“), and the bottom half is the usual Command Window where we’ve been typing code before we learned about scripts. Now, we will only use it to see the output of the script rather than typing code there:
Let’s take a look at this example, where I have typed out some code in the Editor window:
When I run this code, this is what appears in the command window (highlighted):
Strings
Strings allow MATLAB to store, and work with, text (words/letters) as data instead.
To create a string, write out words with single quotes around them, and the words within the quotations will turn purple. Take a look at this example, where I’ve used the input command to show where we can use a string (we haven’t covered input statements yet, but I used it for the example):
We can also replace words in a string using the command strrep(original string, old wording, new wording). Replace each of these inputs into strrep with the string you want to change the words in, the specific words you’d like to change, and the new words you want to change them to. For example, say I have the following string, called str:
If I want to change the word “This” to “It,” I can use the following syntax with strrep:
Now it has changed to “It is a string” instead of “This is a string.”
strfind – A command for strings
strfind is a command that is used with strings to determine whether specific words or letters are in a specified string. The syntax is strfind(original string,’phrase to search for’). This means you will replace the words original string with the string you want to search through, and replace phrase to search for with the letters or words you would like to search for in that original string. Note that there is another command, contains, which is used the exact same way as strfind. This command is only compatible with certain versions of MATLAB, so if strfind doesn’t work on your version, try replacing it with contains instead, while keeping the same arguments within the brackets.
Example – using strfind
I have the following string:
If I want to check whether this string contains the word ‘This,’ (it’s case sensitive, so if you search for ‘this’ it won’t find anything!) I can use strfind(string, ‘This’). The first word string is the variable I had stored my sentence in above, and the word ‘This’ in single quotes is the word I am searching for within my string.
This command will return either 1 or 0 – we got a 1, and 1 means true (the word ‘This’ is in the sentence)! If we got a 0, that would mean false (the word ‘This’ is not in the sentence).
We will use strings more as we progress through this course!