0% found this document useful (0 votes)
6 views

Week_1_Part_6_Fundamentals

The document provides an overview of using MATLAB, including the MATLAB Live Editor, Live Scripts, and code sections. It covers key topics such as arithmetic operations, operator precedence, and how to document code effectively. Additionally, it explains the differences between Live Scripts and MATLAB code files, including their respective functionalities and uses.

Uploaded by

Ngọc Vi Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Week_1_Part_6_Fundamentals

The document provides an overview of using MATLAB, including the MATLAB Live Editor, Live Scripts, and code sections. It covers key topics such as arithmetic operations, operator precedence, and how to document code effectively. Additionally, it explains the differences between Live Scripts and MATLAB code files, including their respective functionalities and uses.

Uploaded by

Ngọc Vi Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Fundamentals

Where do I find MATLAB? Here!

Do I have to have MATLAB installed on my computer right


now? No, you can use MATLAB Online!

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 1


Table of Contents

What we've covered this week in Part 5The MATLAB Live Editor
Live Scripts Code Sections Providing Documentation
MATLAB Code FilesWhat we've covered this week in Part 6

MATLAB Live Script

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 2


What we've covered this week in Part 5
This week in Part 5 we learnt about:

 Arithmetic operations
 Introduction to operator precedence
 Making use of built-in Mathematical Functions

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 3


The MATLAB Live Editor
Using the Live Editor Video

How to Use Live Editor Controls Video

Live script Examples You Can Run

After selecting commands in the Command History, right-clicking displays a context


menu. When you choose Create Live Script from the context menu, the selected
commands are copied into a new Live Script file in the MATLAB Live Editor.

The Live Editor is used to write, edit, run, and share MATLAB Live Scripts, which are
sequences of MATLAB commands together with embedded output, formatted text,
equations, and images. Live scripts have a .mlx extension.

You can open the Live Editor and begin a new file by selecting New > Live Script in the
File section of the Home tab on the toolstrip. To open an existing Live Script in the Live
Editor, simply double-click the file in the Current Folder browser.
© 2023 Introduction to Programming in MATLAB (1), Fundamentals 4
The MATLAB Live Editor
The Live Editor window has its own toolstrip, which contains controls for editing,
navigating, running, and formatting your code.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 5


Live Scripts
The sequence of commands you have just created in the Live Editor is a Live Script.
When you run a Live Script, MATLAB executes the commands as a batch, in order. Any
visible outputs from the commands are embedded directly in the Live Script.

Scripts are used to automate useful blocks of commands. Scripts do not have inputs and
outputs. They operate on data in the workspace, and they can create new data on which
to operate. Variables they create remain in the workspace and can be used in
subsequent computations. Scripts can also produce graphical output by calling
appropriate plotting functions.

Live Scripts display output with the line of code that creates it. By default, MATLAB
displays the output to the right of the code. When you select an output, the cursor
automatically moves to the line of code that generated it.

If a script is displayed in the Live Editor, you can run it by clicking the Run button in the
Run section of the Live Editor tab on the toolstrip. Equivalently, you can press F5.
Running a Live Script runs the code currently shown in the Live Editor, even if the file
contains unsaved changes.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 6


Live Scripts
When you save a Live Script, MATLAB automatically saves it with a .mlx extension. The
file name in the Live Editor title bar is appended with an asterisk if the file has been
modified.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 7


Code Sections
The overall structure of most scripts naturally divides into sections. Especially for larger
files, you may want to focus on one section at a time. To facilitate this, the Live Editor
allows you to create code sections in your file.

A section consists of a section break and the lines that follow, up to the start of the next
section, which is identified by another section break. You can create a section break by
clicking the Section Break button in the Insert section of the Live Editor tab on the
toolstrip, or by pressing Ctrl+Alt+Enter.

You can use the controls on the Live Editor tab of the toolstrip to navigate from section
to section and evaluate the code in a section.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 8


Code Sections
Sections allow you to modify and re-evaluate the code in that section to see how the
changes affect the output. You do not need to commit to the change or rerun the entire
file. This enables you to experiment rapidly with your code.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 9


Providing Documentation
Code files should contain nonexecutable text and other supporting items that explain the
code. The Live Editor lets you add formatted text, hyperlinks, images, and equations to
enhance the readability of your MATLAB code and create a presentable, interactive
document to share with others.

You can add nonexecutable text in a Live Script by clicking Text button in the Text
section of the Live Editor tab. To return to entering code, you can click the Code button
in the Code section of the Live Editor tab. You can press Alt+Enter to toggle between
entering code and text.

Formatting text in a Live Script is like formatting text in a word processor. Use the
controls in the Live Editor tab to make text bold, italic, underlined, or monospaced, or to
add headings, titles, and bulleted or numbered lists.

The Insert tab also contains buttons to insert equations, images, and hyperlinks. When
you insert one of these items, MATLAB places the item on a new text line directly below
the selected line.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 10


Providing Documentation
Equations can be inserted by using an interactive equation editor, or by entering a LaTeX
equation. Only text lines can contain equations.

You may want to include text within block of code so you can keep related lines of code
together in a single code block. Text following a percent sign (%) is a comment, or
nonexecutable code. Comments can appear on lines by themselves, or they can be
appended to the end of a line of code.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 11


MATLAB Code Files
Sometimes it is advantageous to save your MATLAB scripts as plain text files. For
example, when interfacing with external tools that are designed to work with text files,
such as some source control software.

In these cases, MATLAB scripts can be saved as MATLAB code files which have the
extension .m. Like Live Scripts, these code files allow users to run, edit, and share their
code. Many of the features of live scripts, such as code sections, may be used in
MATLAB code files, unlike Live Scripts however the output of the code is not saved inside
the code file.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 12


MATLAB Code Files
To convert a Live Script to a MATLAB code file by clicking Save > Save As... and
selecting MATLAB Code File (*.m) from the Save as Type: menu.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 13


What we've covered this week in Part 6
This week in Part 6 we learnt about:
• Live Scripts
• Code sections
• Providing documentation
• MATLAB code files

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 14


MATLAB Live Script
Click on the link below for this lecture’s MATLAB live Script

• Week_1_Part_6_Fundamentals.mlx

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 15

You might also like