On Matlab editor

In my previous post on kickstarting Matlab, I talked about how to use the command line to make simple calculations. Although nearly everything can be done at the command line, it is obviously difficult to build a complicated program by sending commands one by one.

As for any programming language, Matlab can work with text files that contain a succession of commands sent to Matlab interpreter.  These are simply called M-files. In essence, there are just text files so, if you wanted you could write your Matlab code in any text editor like TextEdit or Notepad. But Matlab is meant to be a comprehensive framework for all your programming needs so it provides you with a nice Editor that incorporates Syntax Highlighting, an interactive debugger, function prewriting, incorporated help sections as well as many others things. The editor is meant to be your main interactive tool with Matlab. Every new Matlab release has added new features in the last years so it has become something quite powerful.

But let’s get started. There are many ways to access the editor. You can type in “edit” in the command line or  you can start the editor here :

You should get a blank window or gray window depending if some M-file is actually open. I am not going to comment on most of the basic function of the editor. It really ressembles a simple word processor. You can type in, save, reload, print, and so on. On all the available buttons, this one is probably the most important :

This button is your Start button. This will actually executes the code contained in the editor line by line from top to bottom. Absolutely any command you can type in the command line, you can also type them here. Let’s now use all the lines of code we used in the kickstarting lesson. Copy and paste this :

X=1:10:100
help Colon
X(3)
X+1
2.3*X
X+X
exp(X)
help ans
Y=exp(X)
X=1:0.1:1000;

As before, this will execute this code as if it was from the command line. So the result will be exactly the same. Each variable will be available to the workspace in the end and the result is displayed on the command line if the semicolon ; is not used.
So now let’s press the green play button. Matlab should ask you to save this M-file somewhere. You can name your first file FirstScript.m. The current code is a script because no functions have been declared. Then, Matlab might ask either to add the file path or to change the current folder. Press Change current folder.
Now as you can see in the command line. All the codes were executed. I will let you play with the editor, but before let me show you one handy thing. right-click on any line. You should get a menu like this :

This is extremely usefull. I personally use it all the time. You can comment one or several lines (it just add the character % at the beginning so that this line is not executed), you can get some help, open sub-functions code. Smart Indent is a very nice feature on which I will post in the future. It helps you make cleaner code. You can also select a number of lines and ask to just executes these lines, …

This entry was posted in Beginners. Bookmark the permalink.

One Response to On Matlab editor

  1. Pingback: MatLab | New ThinKing

Leave a Reply

Your email address will not be published. Required fields are marked *