Module 1: INITIAL SETUP AND BASIC OPERATION

Lesson 1.2 – Hello World

Learning Objectives

After reading this lesson, you should be able to:

  1. perform the basic steps to start a new program in MATLAB,

  2. run a simple program.

This lesson walks you through creating the classic “Hello World” program. Unlike other lessons, we will not explain much of the “how” or “why” in this lesson. You do not need to understand everything in this lesson: just observe! The purpose here is to give beginning programmers something to relate to in subsequent lessons.

  

Where can I find and open the MATLAB program?

The procedure for opening MATLAB® is described in this section for Windows. First, go to the start menu and look for “MATLAB” in the list of your installed programs. Once there, click on the appropriate version of MATLAB installed on your computer. Do not click on the “Activate/Deactivate” options if MATLAB is already installed on your computer. When the program opens, a window that is similar to the one in Figure 2 should open.

  

Step 1: Create a New m-file

First, we need to create a file to store our program. In the top left portion of the MATLAB screen, create a new m-file by pressing the “New Script”. Note that in many languages a script is a common term for the file that holds your code. Save the blank file as “helloWorld”.

Figure 1: Click “New Script” on the far left under the “Home” tab to open a new m-file.

  

Step 2: Write the ‘Hello World’ Code

In this next step, we will write the code that will output/return our message: ‘Hello World’. Note that the green text after % will be ignored and will not be executed. See Figure 2 if you are not sure where to type your code.

Figure 2: MATLAB window showing where to type your code (in Editor window).

  

Example 1

Write a program that outputs the message “Hello World” to the user.

Solution

Figure 3: Output a message to the user.

Figure 4: The Command Window output for Example 1.

  

Step 3: Run the Program

To get our program to do something, we need to “run” the program. Figure 5 shows the “Run” button, which you should click to run the program. This is located under the “Editor” tab in the toolbar (top left of screen).

Figure 5: Run the program by clicking the highlighted button under the “Editor” ribbon tab (top).

Once the “Run” button is pressed, the program will execute our command. The result of our code will show in the Command Window (the window on the right).

  

Step 4: Make Your Program a Little Fancier

Finally, we can do a couple more things to demonstrate some other simple concepts. This time we will include our name and age in the “Hello World” greeting. Again, just observe. Each of these concepts will be explained in the lessons to come.

  

Example 2

Write a program that outputs a fancy “Hello World” to the user including your name and age.

Solution

Figure 6: Output a fancier message to the user.

Figure 7: The Command Window output for Example 2.

Multiple Choice Quiz

The content of this page is intentionally blank

Problem Set

The content of this page is intentionally blank