"

1. The Matlab Environment

Learning MATLAB

To use MATLAB you must become familiar with the layout of the user interface of the software. Upon opening the software in Windows, the user will encounter the following page, which is divided into several useful windows. Each window has been colored and labeled in the image below and is discussed in the following section. MATLAB enables the user to make modifications to the position of each window by selecting alternative layouts in the “Layout” button in the toolbar. Different layouts may be more convenient for different users or applications, so feel free to find the layout that is best for you.

 

Figure 2.1.1 The initial page was viewed after opening MATLAB.

 

Figure 2.1.2 Each element of the MATLAB user interface upon startup. Elements are highlighted for clarity.

The Command Window

The command window (shown in blue) will serve as the primary interface the MATLAB user will interact with when writing and reading simple codes or individual operations. The command window can be used as an interface to type directly into MATLAB and as a place to view the results of these codes. Codes will be written in “script” files, which will generate a new window in what is currently shown as the top half of the command window. When running a script, a user will see outputs from the code displayed in the command window and can input data or other information using the command window. The shown example of a command window depicts several mathematical operations. The highlighted portions show what the user typed into the window, while the remainder is the output by MATLAB.

When the user is done viewing code or is restarting a program, the user must clear anything written in the command window. By using the clear command, clc, the user can reset the command window to its original blank state. For example, completing an arithmetic operation in MATLAB such as 238*5 will result in the window to the right.  Typing clc and pressing enter will clear the text in the window.

When writing scripts, you will want to start with a blank command window and reset any operations that the code may have carried out. This can be accomplished by writing two lines of code at the beginning of the script with clc and the close all command. This is demonstrated in the example script below. As a result of these two lines, the text in the command window will reset each time the code is run.

Programs and the Current Folder

Script program files can be saved and referenced later or transferred to other computers, unlike code written directly through the command window. It is good practice to save all scripts to the folders that are clearly labeled and well-organized. You will run your scripts from what is referred to as the “current folder” in MATLAB. The current folder is shown directly below the toolbar and is highlighted in yellow in the above window. By saving your scripts to the same folder you will be able to keep everything in the same place and reference different files without changing folders. Script files, which are saved in .m format, are listed within the red programs section of the MATLAB user interface image above. When a .m file is selected, information about the file will be displayed in the details window, which is depicted in orange in the above image.

As you develop more and more scripts you will save many files in this section, which will quickly reveal the importance of careful file naming. Develop a habit of including the name of the program, your last name, and possibly the date the file was created within the file name to ensure it will be able to be referenced in the future. When naming files in MATLAB, it is important to note that file names cannot include spaces, hyphens, dashes, or numbers as their first digit. To make file names readable, I recommend using underscores and capital letters to make quality file names (i.e. “WaveForceCalculator_Benitz_March2025” is much more informative than “untitled5” and will be easy to locate when needed again.

The Editor

The editor window is the home of MATLAB script files – savable codes that can be shared and saved on a computer for use. MATLAB scripts are the files where all stand-alone programs will be saved. When working in the command window every time an operation is typed and the enter key is pressed, the operation is completed by MATLAB. This fact means that codes are run step-by-step when written in the command folder. This is not the case for scripts within the editor. Codes are written by the user and only ran when commanded by the user by selecting the run button in the editor window of MATLAB.

Multiple scripts opened at the same time overlaid with tabs showing the name of each script. Like programs in Microsoft Windows, scripts can be dragged and dropped to be viewed next to each other or above and below each other, allowing for easier usage when working on multiple scripts.

The editor window also is home to expanded views of variables. When a variable from within the variables window is selected by double-clicking, a window will overlay the editor and any open scripts to show the contents of the variable. This may be a single value, a matrix or array, or a symbolic input. Users may shift back and forth between viewing the contents of variables and scripts using the tabs that appear at the top of the editor window.

Comments

Comments are a built-in way MATLAB has enabled users to make notes within a code without affecting the code’s function. To make a comment within a code, the user can type % at the beginning of a line of code. MATLAB will then ignore everything typed after that percent sign, enabling the user to type information regarding the function of the code or what a part of the code does. Comments are essential to writing efficient, long codes that can easily be read and edited by both the initial author and their peers. By placing many comments throughout codes, the author will ensure that the code is easily read and edited by themselves, their peers, faculty, etc. Forming the habit of commenting often throughout a code is critical to ensuring that codes are useful and effective.

Comments have also been designed to help users who are editing code and would like to “turn off” a section of code. In the instance that a section of code within a MATLAB script does not work or should not be run, the user could place comments with % signs at the beginning of each line that should be ignored by the code. In order to speed up the process of “commenting out” large sections of code, MATLAB has created the notation %{ and %} which can be placed on different lines of code and will comment everything written in lines of code between the symbols. An example of standard comments and a block comment follows below. In the example, a portion of code that does not work correctly has been commented out and a note to correct the code has been made. Comments such as the one in line three are essential to ensure that codes are as legible as possible for all users.

It is a good habit to write consistent comments at the top of each new script that specify the author (likely you), the date, and a brief description of the code. For example:

%Maija Benitz

%September 10, 2024

%Computation of hydrodynamic loads on a monopile support structure

The Workspace

When writing scripts in MATLAB many variables will be introduced to represent data or to be used in formulas. To assist the user in determining which variables have been assigned in a script, the workspace will show each variable and the values that are assigned to them. This will be helpful when writing long codes using many similar variables and will help keep track of variable meanings.

Various forms of variables that exist within the MATLAB coding language are displayed within the Workspace. This includes numbers, characters, words, and more complicated double objects with arrays and matrices, and histograms. An example of a workspace for a code with several variable types is included below. Notice how each variable is assigned its own name by the user, allowing for it to be identified within the code.

The clc and close all commands were discussed above. Other useful commands for cleaning up, include clear and clf. The clear command erases all stored values from the workspace, while clf erases anything plotted on a figure. 

Help Resources

MATLAB is a powerful programming tool that contains many functions which can be used in an assortment of applications.

When writing code, there are several ways for the user to learn more about functions and their operations. When the user knows the name of a command and is wanting to learn more about its function or how it should be written into the code, the user should use the help command. For example, to learn more about the if function, the user would type help if into the command window.

Many other resources exist outside of the program within both the MathWorks’ webpage and resources produced by third-party vendors. The Additional Resources section of this chapter discusses many resources that are provided outside of MATLAB software itself. The documentation page for a specific function can be reached by following the hyperlink at the bottom of the help page in MATLAB or by searching for the “MathWorks MATLAB Documentation” in a search engine.