Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to talk about M-file scripts in MATLAB. These scripts are essentially a way to save a series of commands so you can run them multiple times. Can anyone tell me why this might be useful?
It would save time because you don’t have to enter the commands repeatedly.
Exactly! Saving time is a big benefit. You can also edit and modify your commands easily. Remember, the files end in .m. Does anyone know what you call these files?
They are called M-files!
Right! M-files can be scripts or functions. Now let’s dive into how to create one. It's very straightforward. You can use the MATLAB editor to do this. Has anyone used the MATLAB editor before?
Yes, I have! It’s a text editor for writing code, right?
That's correct! Let’s summarize this session: M-file scripts allow you to save and execute commands, making your workflow more efficient.
Let’s walk through creating a simple script file. When you want to create a new script, what do you do in MATLAB?
You go to File, then New, and select M-file!
Exactly! Then you write your commands in the file. For example, let's define a matrix A and a vector b, and solve for x. What do you think happens when we save it as example1.m?
You can run it by typing example1 in the Command Window!
Right again! Running the script will execute all the commands in order. And then, we can use whos to check which variables are in the workspace after running the script.
Will that include variables that are already in the workspace?
Great question! Yes, this leads us to the concept of side effects, which we'll discuss next. Remember, scripts can overwrite existing variables!
Let's look at some examples of M-file scripts. The first one we discussed solves a system of equations. Can someone describe the steps taken in that example?
We defined the matrix A and vector b, then used the backslash operator to solve for x.
Exactly! Now, what about the second example with the cosine functions? Who can explain what we did there?
We created a range of x values, defined three cosine functions, and then plotted them!
Fantastic! Plotting is a crucial aspect of MATLAB. The visualization helps in understanding the results. Let’s recap: we explored solving equations and plotting functions using scripts.
Now, let's discuss the side effects of scripts. What do you all think happens when we run a script and overwrite some existing variables?
It could change the results unexpectedly since the script might use those variables.
Exactly! That’s a significant concern. This is why for complex tasks, we often prefer using function M-files that have a separate workspace.
Can you explain the difference between scripts and functions again?
Sure! Scripts do not accept input or return output, while functions can. Functions have their own workspace, avoiding the side effects we've discussed. To remember this, think 'Scripts Save, Functions Function!'.
That's a catchy way to remember it!
To save commands, you write them down, in an M-file, your coding crown.
Imagine you’re a chef writing a recipe. Each command in an M-file is a step in your culinary creation, easy to follow and repeat.
Remember: 'Scripts Save, Functions Function!' to distinguish their roles.
M-File: 'MATLAB File' — the key file extension for scripts and functions.
Term
What file extension do MATLAB scripts use?
Click to flip
Term
What is the purpose of a script in MATLAB?
Click to flip
A file containing MATLAB code saved with a .m extension.
A sequence of MATLAB commands saved in an M-file that can be executed as a single program.
A storage area in MATLAB that holds variables created during a session.
Unintended consequences that may result from executing a script, such as overwriting existing variables.
M-file scripts in MATLAB are external files containing a sequence of commands that can be executed, making it easier to reuse and modify code.
In this section, we explore M-file scripts, which allow users to save and execute sequences of MATLAB commands in .m files. We also discuss the creation and execution process, potential side effects of script execution, and differences between scripts and functions.
M-file scripts, commonly referred to as scripts, are external files with a .m extension that contain a sequence of MATLAB statements. These scripts facilitate the automation of tasks in MATLAB by allowing users to save a series of commands and execute them multiple times without needing to re-enter the commands manually in the Command Window.
example1). The commands in the script will execute sequentially.