After reading this lesson, you should be able to:
perform the basic steps to start a new program in MATLAB,
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.
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.
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.
In this next step, we will write the code that will output/return our
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).
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).
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.