Kahibaro
Discord Login Register

Working with Scripts and Live Scripts

Introduction

Scripts and live scripts let you save commands in files so you can run them again, share them, and build longer pieces of code. They move you from typing one command at a time in the Command Window to writing reproducible programs. In this chapter you will see what scripts and live scripts are, how they behave compared to commands you type interactively, and where they fit into a basic MATLAB workflow. Details of how to create, run, and enrich them are covered in the child chapters.

What Scripts Are

A script is a plain text file that contains a sequence of MATLAB commands. The file has the extension .m. When you run a script, MATLAB executes the commands in the file as if you had typed them, line by line, in the Command Window.

Scripts do not have their own separate workspace. They use and modify variables in the base workspace. This means any variable that exists in the base workspace before you run a script can be read by that script, and any variable that the script creates or changes will appear in or update the base workspace.

Script files are often used for small analyses, exploratory work, quick calculations, or any situation where you would otherwise repeat the same set of commands manually. They are also the natural first step before learning to write functions, because the syntax inside a script is almost the same as in a function, just without an input or output definition line.

What Live Scripts Are

A live script is similar to a script but stored in a different format with extension .mlx. A live script supports rich content. You can mix executable MATLAB code with formatted text, titles, equations, images, and interactive outputs in one document.

Code in a live script is organized into sections. Each section can be run on its own. Results are usually shown next to the code instead of only in the Command Window. Live scripts are well suited for teaching, reports, tutorials, and exploratory analysis where you want to describe what you are doing, show the code, and show the results together in one document.

Internally, the MATLAB commands in a live script behave like those in a script. They are still executed by the MATLAB engine and they still work in a workspace. What is different is the way you view, edit, and present them.

Scripts and Live Scripts in the Workflow

When you use MATLAB as a calculator or to try a quick idea, you usually type directly in the Command Window. As soon as you want to repeat work, keep a record of what you have done, or build something longer than a few lines, you should move those commands into a script or a live script.

A typical beginner workflow is to experiment in the Command Window until some commands are working, then copy the useful ones into a new script file. Over time, you can turn that script into a live script if you want to add explanations or present it to someone else, or into functions if you want reusable building blocks with inputs and outputs.

Scripts and live scripts are saved in files under your project folders. MATLAB runs them based on the current folder or on the search path, so where you save these files becomes part of how you organize your work.

Differences Between Scripts, Live Scripts, and Functions

Scripts and live scripts both hold sequences of MATLAB commands. Functions also live in .m files, but have some important differences. A function file begins with a function definition line that defines the function name, input arguments, and output arguments. A script or live script does not have this line. As a result, you run a script or live script simply by using its file name, while you call a function by writing its name followed by parentheses and any input arguments.

A function executes in its own workspace which is separate from the base workspace and from the workspaces of other functions. Scripts and live scripts execute in the base workspace. For beginners this usually makes scripts easier to understand, but it also makes them less controlled and more dependent on the current state of the workspace.

Live scripts differ from plain scripts mostly in the editor experience and the storage format. They support formatted text, output inline with code, and some interactive controls. The underlying code is still MATLAB code that can often be converted to or from a plain script when needed.

You will learn the exact syntax for scripts and functions in the specific chapters that follow, so this chapter only highlights the conceptual differences.

Naming and Running Behavior

Both scripts and live scripts must follow MATLAB naming rules. The file name, without its extension, is also the program name you use to run it. For example, if you save a script as myAnalysis.m or a live script as myAnalysis.mlx, you run it by typing myAnalysis in the Command Window or using the Run controls in the Editor. The name must start with a letter, can contain letters, digits, and underscores, and should not match a built-in function or keyword.

Since scripts and live scripts share the workspace with the Command Window, running one can overwrite variables that already exist. In small experiments this is often acceptable, but for larger tasks you should be aware of this side effect. As you advance, you can reduce such issues by using functions or by clearing variables explicitly within scripts, which is described in later chapters.

Choosing Between Script and Live Script

For many simple tasks, a basic .m script is enough. If your goal is to automate a small calculation, test some code, or write something that only you need to read, a script file is usually the simplest choice.

Live scripts are more suitable when you want to explain your steps, keep notes with your code, or present results. A live script can serve as a combination of a lab notebook and a report. You can show it directly in MATLAB, or later convert it to other formats with the tools that will be described in child chapters.

Over time, you may start a project with a live script while exploring data, then move stable parts of the code into functions, and keep the live script as a documented top level analysis.

Important reminders:
Scripts and live scripts share the base workspace, so they see and change the same variables as the Command Window.
Scripts are plain .m files with sequences of commands. Live scripts are .mlx files that support formatted text and rich output.
You run a script or live script by its file name, and file naming rules in MATLAB still apply.
Use scripts for simple automation and live scripts when you want both runnable code and human readable explanations in one document.

Views: 3

Comments

Please login to add a comment.

Don't have an account? Register now!