Getting Started with MATLAB | MATLAB Tutorial
by Ratnesh Shah
This is a typical MATLAB application screen. Most frequently used tools and toolbars are available under HOME, PLOT, APPS, EDITOR, PUBLISH and VIEW categories available at the header.
- MATLAB Application Screen
Also option for help is available in the header toolbar as highlighted below by the red box.
We will start with the Command Window and Code Editor.
- Command Window and Code Editor
Command Window is shown as below. Here, user can write and execute their code in realtime. Scripts and Functions can be called from this command window using “run(FuntionName/ScriptName)” command. Usually very small commands such as function calling or plotting is performed directly using Command window. Thus for larger codes, they need to be written as script and function to be executed later. Moreover, the Command window is capable of accessing global functions offered by MATLAB. Therefore, results that are not related to plotting or other tools are printed as lines in command window itself. Command Window is shown below
The “>>” signifies that MATLAB is ready for next input.
The code editor window is highlighted in the image shown below,
Codes can be written and saved and executed using the Code Editor in MATLAB. Scripts and Functions can be created in the code editor.
Furthermore, we have had mentioned something as script and function. We have a utility in MATLAB to create scripts and functions. Scripts refer to standalone program that can directly be executed by calling in command window. Script can be termed as the main() function or method in analogy to other programming languages. A script can call other scripts and functions in itself. Scripts are saved as “.m” files.
Whereas a function is a piece of code that gives output or number of outputs for given inputs. It cannot be used as a standalone program and needs to be called using Command Window or by a script. Basic structure of a function in MATLAB can be summarized as
function [ y ] = square_area( a )
The part “function ” is an identifier for MATLAB to understand that the following piece of code is a function. The arguments in square bracket are outputs that the function returns. To the Right Hand Side of the equal sign lies the name of the function. Users need to be sure that the function needs to be saved using the “FunctionName.m” format, so that the function can be called easily. The function name should not conflict with the inbuilt functions of MATLAB for the sake of easy accessibility of all functions.
Performing Simple Mathematical Operations
Now that we know about the Command Window, we can try some basic mathematical operations.
Following are the mathematical operations that we are going to use,
‘+’ – Addition
‘-‘ – Subtraction
‘*’ – Multiplication
‘/’ – Division
We can directly perform these operations on numbers or we can create variables and apply these operations on the variables. We are going to do it by both ways.
Firstly, the direct way. Follow the steps –
- Click on the command window to ensure that you are going to type in it. It should be showing ‘>>’ and that means the command window is ready for next command.
- Now let us add any two numbers. For example 2 and 6.
- Write ‘2 + 6’ in the command window.
- You would obtain a result in variable ‘ans’, this variable is created by MATLAB explicitly to store result and display it on command window. You would obtain 8 as answer. Now this is just beginning, we will use MATLAB for greater purposes!!!
- Perform subtraction by writing ‘2 – 6’ and you would get ‘-4‘ as answer.
- Likewise, write ‘2 * 6’ to get the multiplication of 2 and 6. You would obtain ‘12’.
- To perform division, write ‘6 / 2’ to get answer as 3 and even if you write ‘2/6’, you would obtain ‘0.3333’ as answer. Decimals are printed to 4 places by default in MATLAB.
The screenshot shows the result of simple operations.
These were examples of writing up small programs and operations. But what if the user wants to perform these operations at one go or write bigger codes. We have to use Code Editor Window to write these programs as MATLAB script files. Script execution can be understood by following screenshot,
Here, I would like to introduce some other windows on the MATLAB window before moving further.
The current folder window denotes the current directory of work. The scripts and functions created by users and saved in this directory can be accessed from the command window.
There is a workspace window in MATLAB where all the variables are stored are shown. It is helpful so as to avoid overwriting any variable by mistake or just to find values after execution of a code.
There is a window named variables, it shows the value of a single variable. This window is accessed by simply double clicking any variable in workspace. This window display all of the values contained in a variable, whether it is a matrix or a vector or any type of variable.
Now that we have an idea about the windows in a MATLAB application, we can move forward.
CLC, CLEAR and CLOSE
There are times when you are done with a piece of code written in command window, or there are too many variables in workspace which have no relevance to current program. Moreover, sometimes we forget to close the figures and dialog boxes that popup. Now for newer codes to be executed flawlessly and without any conflicts, we need to get rid of older variables and close all the dialog boxes such as older figures so as to ease the things up.
‘clc’ – This command is used to clear up the command window.
‘clear’ – It is used to remove the variables, functions.
‘clear all’ – It is used to remove variables, functions and any other links formed during execution of previous program.
‘close all’ – It closes all the dialog boxes, mostly figure windows that are currently open.
When you write up a code and want to save it, just press ‘ctrl + s’ or click on Save in Editor options as shown in next image.
You can save your code written in code editor as ‘.m’ files.
To save the workspace variables, user can click on the workspace menu and click save. Matlab can store the variables in ‘.mat’ format. User has to choose variables to save selected variables or can save all variables. More can be understood from the image given below,
Some users may have notices that MATLAB creates a ‘.asv’ file in the directory in which user is working. It is nothing but an autosave file that is created by MATLAB in case the program crashes or accidently gets switched off. This ‘.asv’ file is helpful in such an event. User may disable the creation of ‘.asv’ file but it is strongly advised to let MATLAB create autosave files. To toggle this utility, user may follow this path,
File/Preferences/–>”Editor/Debugger” –> Autosave