Running your MATLAB code is a fundamental task, typically performed directly within the MATLAB environment using the Editor or Live Editor.
You can run MATLAB code from the Editor or Live Editor using dedicated buttons provided in the interface.
Methods to Run MATLAB Code
MATLAB offers convenient ways to execute your scripts or functions, whether you want to run the entire file or just a specific part of it. The primary methods involve using the controls located on the Editor or Live Editor tabs.
1. Running the Entire File
To execute the full contents of your .m
file (script or function) or .mlx
file (live script):
- On the Editor or Live Editor tab, in the Run section, click Run. This command executes the code sequentially from the beginning of the file.
This is the most common way to test a complete script or run a function with default inputs (if applicable).
2. Running a Section (Live Editor or Code Sections)
MATLAB allows you to divide your code into logical sections, especially useful in the Live Editor or when structuring complex scripts. Running sections helps in testing specific parts of your code interactively.
- On the Editor or Live Editor tab, in the Section section, click Run Section. This will execute only the code within the current section where your cursor is placed.
- In the Live Editor, you also can click the blue bar to the left of the section. Clicking this bar is a quick visual shortcut to run that specific section.
Using sections and running them individually is particularly powerful in the Live Editor for creating interactive narratives that combine code, output, and formatted text.
Quick Reference Table
Location | Action | Button/Indicator | What it Runs |
---|---|---|---|
Editor/Live Editor Tab | Click in Run section | Run | Entire File |
Editor/Live Editor Tab | Click in Section section | Run Section | Current Section |
Live Editor | Click to the left of section | Blue bar | Current Section |
These simple clicks provide immediate execution of your code, allowing for rapid testing and development within the MATLAB environment.