MATLAB - Editors



Matlab comes with a built-in powerful editor that helps to write your code and also to compile , debug and see the output. When you log in to Matlab this will be the screen that is displayed to you.

matlab editor

To open a editor you can type edit or type edit along with filename in command window.For example

>>edit
>>edit test.m

The matlab files are saved with the .m extension. Let us first just type edit in the command window and see the editor opening.

matlab files

Now, let us type edit test.m in the command window and see the editor opening.

command window

On enter you will see the dialog box asking to confirm since the file is new. The file test.m is not existing and will be created, if it exists it will be opened directly.

Click on Yes and it will open a new file test.m in the editor as shown below.

matlab drive

Now, let us write a small piece of code in the editor and run the same.

function [m,n] = test(x)
   n = length(x);
   m = sum(x);
end

The function name:test will take the vector as input and return the sum of the vector and the length of it.

Let us enter the function inside the editor test.m, as shown below.

matlab test file

Save the changes and in the command line you can execute as shown below.

>> a = [10, 20, 30, 40]
a =  10    20    30    40
>> [m, l] = test(a)
m =  100
l =  4
>> 

We are giving the vector a = [10, 20, 30, 40], to the function test. It returns m and l where m is the sum of the vectors and l is the length.

Another thing to do in the editor is you can write the instruction inside the editor as shown below −

instruction

So we have the above instruction , let me select line no 1 to 3 and execute it as shown below.

evaluate selection
>> x = 10
y = 20
z = x*y
x =  10
y =  20
z =  200
>> 

Live Editor in Matlab

The live editor is yet another powerful editor wherein the user can write/edit code, debug and run it in Matlab.

It allows you to do analysis with data that supports the following −

  • Display output together with your MATLAB® code.
  • Use formatted text to describe your approach.
  • Use equations to describe the underlying mathematics.
  • Use images to illustrate important points.
  • Add links to background material.
  • Use controls to modify parameters and re-run the analysis.
  • Plot data for visualisation.
  • Invite colleagues to extend your analysis.

How to use a Live Editor?

In Matlab you will get the Live Editor tab directly when you open matlab. Or else inside the home tab you can click on New Live Script, it will open the Live Editor.

live editor

Enter your code inside the editor and use the run button to execute the code. The output will be displayed at the right side section. In the section below let us enter code and execute it.

Create Live Script

To create the live script you can click on the Home tab. Inside there select New Live Script. The files in the live script are stored with the .mlx extension.

You can also open the file in live script using the edit command. So specify the file name after edit command for e.g

edit  test.mlx

If the file test.mlx already exists it will open in the live editor. If the file is a new one it will open the blank editor.

Here is the code which simply plots the random number from 25 to 1.

n = 25;
r = rand(n,1);
plot(r)

When you execute above the display is as follows −

execution of code

In the above case when you execute you can see the output is placed at the right side.

Formatting Text in Your Live Editor

As mentioned earlier there is a lot of stuff you can do inside the live editor and one of it is formatting text.

You can formattext, add hyperlinks, images and maths formulas.

To add text you can do the following

formatting text in live editor

See the highlighted block. It allows you to select title, heading etc and enter inside the editor.

You can also make use of keyboard shortcuts as shown below −

Text Style Auto Formatting Keyboard Shortcut
Title # text + Enter Ctrl + Alt + L
Heading 1 ## text + Enter Ctrl + Shift + 1
Heading 2 ### text + Enter Ctrl + Shift + 2
Heading 3 #### text + Enter Ctrl + Shift + 3
Bullets

* text

- text

+ text

Ctrl + Alt + U
Hyperlink URL + space or enter Ctrl + K
Bold

**text**

_text_

Ctrl + B

Enter Equations in You Live Editor

Adding equations is very easy inside a live editor. To do that, just click on the INSERT tab in MATLAB as shown below.

equations in live editor

Click on Equation where you want to insert the equation. The Equation shows a variety of symbols that are used to construct the Maths equations −

click on equation

Once you click on the Equation the cursor in the editor will prompt you to Enter your equation.

Click on the symbols to construct your equation you want. Besides using the Equation tab, you could also make use of keyboard shortcuts.

For example you want the symbol pi, Inside Enter your equation section just type \pi and you should see the pi symbol as shown below.

starting with pi

Once you type \pi , the dropdown filters the symbols starting with pi. You can select the one you want and you should see the pi symbol as shown below.

pi symbol

If you want to see the full list available for you to select from keyboard shortcuts just type \ and the dropdown will give you the full list.

For Example

keyboard shortcuts

You can make use of symbols like _, ^ and / to achieve below results.

  • x_2 will give you x2 in the equation you want.
  • x^2 will give you the result x2
  • x/2 will give you the result x/2

Let us see the example in the Live Editor

example in live editor
Advertisements