Home

How to Write Your First MATLAB Script

|
|  Updated:  
2016-03-27 09:31:02
|   From The Book:  
MATLAB For Dummies
Explore Book
Buy On Amazon

MATLAB provides many different ways to write scripts. Some of them don’t actually require that you write anything at all! However, the traditional way to create a script in any application is to write it. The most common first script in the entire world is the “Hello World” example. The following steps demonstrate how to create such a script using MATLAB.

Click New Script on the Home tab of the menu.

Click New Script on the Home tab of the menu.

You see the Editor window appear. This window provides the means to interact with scripts in various ways. This Editor tab is the one you use most often when creating new scripts.

Type’Hello World’.

Type’Hello World’.

The text is highlighted in a light orange, and a squiggly red line appears under it. When you hover your mouse over the squiggly line, you see an error message.

In this case, you ignore the error because you want to see the output. However, if you wanted to correct the problem (the way MATLAB thinks you should), you could either type a semicolon or click Fix to resolve the issue. MATLAB will always tell you if it thinks that you’re making a mistake, but sometimes MATLAB is overzealous (as in this situation).

Click Run on the Editor tab of the Editor window.

Click Run on the Editor tab of the Editor window.

You see a Select File for Save As dialog box. MATLAB always requests that you save your script before you run it to ensure that your script doesn’t get lost or corrupted in some way should something happen when it runs.

Create your directory, type FirstScript.m in the File Name field, and click Save.

Create your directory, type FirstScript.m in the File Name field, and click Save.

MATLAB saves your script to disk. All your script files will have an .m extension.

At this point, you may see a MATLAB Editor dialog box appear. If that happens, simply click the box’s Add to Path button to make the dialog box disappear. If you don’t see this box, continue to Step 5.

Select the MATLAB window.

You see the following script output:

>> FirstScript
ans =
Hello World

The output is telling you that MATLAB has run FirstScript, which is the name of the file containing the script, and that the output is Hello World. This output has been assigned to ans, the default variable.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jim Sizemore is Professor of Physics and Engineering at Tyler Junior College. For over 25 years he s worked in the semiconductor and software industries as a process engineer, device physicist, and software developer and has been teaching college physics, engineering, and math for the last 13 years.