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.
Welcome everyone! Today we will start learning about M-files in MATLAB. Does anyone know what an M-file is?
Is it a file that contains MATLAB code?
Exactly, Student_1! M-files are files with a '.m' extension that store sequences of MATLAB commands. They allow us to save and run commands multiple times. Why do you think it’s beneficial to use M-files?
So we don’t have to retype commands every time we want to run them?
That's right! Saving commands in M-files makes it easier to organize and reuse your code. Let’s remember that: M-files = Save + Reuse. Can anyone think of examples why we might need to reuse commands?
We might need to use the same commands for different datasets!
Great point! Let's move on to how to create an M-file. You can create one using the MATLAB editor, which is optimized for writing and debugging these files.
Now let’s talk about how to create an M-file. What steps do you think are important?
We need to open the editor and then write some commands.
Exactly! After writing our commands, we can save the file with a '.m' extension. Student_1, can you describe how you would run this M-file once it’s saved?
I can just type the name of the file in the Command Window!
Correct! For example, if we save it as 'example1.m', we would type '>> example1' in the Command Window. Does anyone remember the effect of running a script?
All variables created in the script will stay in the workspace.
Exactly, and sometimes that can lead to issues if we overwrite existing variables. We will discuss that next. Remember: Scripts save time but can create conflicts.
We've learned about creating M-files, now let’s discuss side effects. What do you think the term 'side effects' means in this context?
Does it mean something negative that happens when we run a script?
Exactly! Scripts can have side effects like overwriting existing variables in the workspace. Why might this be problematic?
If we’re using a variable in our workspace and then the script changes it, it could mess up our calculations.
Great observation! This is why for complicated applications, it’s often better to use M-file functions instead. Remember: Scripts = Easy Reuse, but watch out for Side Effects!
Let’s transition to M-file functions. Who can tell me how functions differ from scripts?
Functions can accept input arguments and return outputs, while scripts cannot.
Exactly, Student_2! Functions have their own workspace separate from the base workspace. This protects against variable conflicts. What do we call the first line of a function file?
It's the function definition line?
Yes! The function definition line indicates the name of the function and its input/output arguments. Side Note: Remember, the file name should also match the function name!
Now, how about we create a sample function together? What function could we create?
We could create a function to calculate the factorial of a number!
Great idea! Can anyone outline the steps we need to create this factorial function?
First, we need to define it and then calculate the product of numbers from 1 to n.
Exactly! Let’s remember the structure: function definition, then the body with code. And remember, functions separate their variables from the base MATLAB workspace.
So we would use 'function f = factorial(n)' and then calculate with 'f = prod(1:n);' right?
Spot on! Remember to keep practicing creating functions. Functions in MATLAB provide clarity and prevent variable conflicts.
Files in MATLAB for storing commands.
Sequence of commands that can be saved and executed.
Programs that accept inputs and return outputs.
Storage area for variables in MATLAB.
Issues that arise from executing scripts affecting existing variables.
M-files are neat, commands on repeat!
Once a programmer had many commands written down. They found a way to save them in a file so they could run them over and over, never losing a line.
Remember: S = Store, R = Run, where scripts Save and Run commands.
M - Multiple, F - Functions: M-files allow Multiple Functions to be written and saved.
Term
What does M-file stand for?
Click to flip
Term
What is a key difference between scripts and functions?
Click to flip
A script or function file used in MATLAB that contains a sequence of commands, with a '.m' file extension.
An external file containing a sequence of MATLAB commands that can be executed all at once.
A program that can accept input arguments and return output arguments within its own workspace.
A storage area in MATLAB that contains variables, configurations, and data that can be accessed during execution.
Unintended consequences resulting from executing scripts, often involving the alteration of existing variables in the workspace.
This section introduces M-files in MATLAB, explaining their importance in executing and saving commands for repeated use.
The introduction discusses the limitations of executing commands in the Command Window and presents the concept of M-files as a solution for creating, saving, and running scripts or functions. The significance of organizing commands into these files for ease of use and maintenance is emphasized.
In this section, we delve into the importance of using M-files in MATLAB for executing commands that are frequently reused. Unlike the Command Window where commands are ephemeral, M-files allow users to create script files (.m files) that encapsulate a series of MATLAB statements. These scripts can be modified and executed multiple times, providing advantages in both computational efficiency and organization. The section further distinguishes between M-file scripts, which simply execute commands, and M-file functions, which can accept inputs and return outputs. By illustrating this with examples of solving equations and plotting functions, we highlight how scripts can streamline tasks and prevent undesired side effects commonly associated with workspace variables. The idea is that effectively utilizing M-files can enhance MATLAB programming efficiency and clarity.