Use the Editor to Start Creating Useful MATLAB Scripts
Tags: matlab, beginners, tutorial
Published: 2022-08-23 07:32:33

The command window is a handy tool if we want to write a few lines of code to do calculations, but if we need to do more complex operations is not optimal to write and execute them one by one. Instead, we can use the editor to create scripts that will run multiple code lines and give us the final result.

## Introduction to the Editor

We save the files we create using the editor as m-files; also, we can use them as many times as we want.

To open the script editor, we must open the HOME window (using the buttons above) and go to New -> Script or write edit in the Command window.

![Create bew script.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1661173621295/eaDT2_y_I.png)

When we type in the edit window, if we press enter, MATLAB won't execute the program; it will simply move to the following line to allow us to continue writing. This way, we can create more complex programs than in the command window, with multiple lines.

### Creating our first script

Let's start by creating our first script. Type the following code in the command window:

```
x = 3+2

```

Save this file in your current MATLAB folder by going to Save -> Save as... And give it the name you want, for example, trial.m.

![Saving a script.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1661173741406/MHdq8X9fE.png)

Now, write the name of your script in the command window:

```
trial

```

MATLAB will print the following:

![Trial script.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1661173798551/DA9yq0HRQ.png)

First, MATLAB will look for a variable called trial, and if there is none, it will execute the trial.m script inside the current folder.

## Commenting your code

In programming, we use comments to help other users understand what a program does. MATLAB (or any programming languages) will ignore these lines marked as comments so they won't affect your software.

In MATLAB, we create comments with the % sign at the beginning of each line. For example:

```
% This is a comment
% for my MATLAB program

```

MATLAB will colour these lines in green. This way, the user will have an easier time identifying comments in the code.

![Comments.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1661173918912/Yh8zSq4aU.png)

We can also add a comment on a line of code by adding a % after that line. For example:

```
x = 3+2 % Here we add 3 and 2

```

We can also comment on more than one line using %{ and %}:

```
%{
This is a 
multiple-line
code
%}

```

## Challenge

Try adding, subtracting and multiplying by two a variable you define in the script, and print the results. For example, if I create var=5, my results would be:

Sum = 7

Subtract = 3

Multiplying = 10

## Conclusion

Congratulations on reaching the end! Now you know how to write multiple lines programs using the scripts in MATLAB.

Creating scripts is very handy in MATLAB, as it is the first step to creating more complex problems that will help you solve different challenges. Remember to comment on your code lines. This way, your code becomes easier to understand for anyone or a future yourself!

## Useful links

Check other articles in my [MATLAB](https://dev.to/sandra1me/series/19226) series if you want to know more!