Introduction and Basics of Matlab Programming
The MATLAB is the tool which is used to perform mathematical complex computations. In this MATLAB simplified C is used as the programming language. The MATLAB has various inbuilt toolboxes and these toolboxes are mathematical toolbox, drag, and drop based GUI, Image processing, Neural networks etc. The MATLAB is generally used to implement algorithms, plotting graphs, and design user interfaces. The MATLAB has high graphics due to which it is used to simulate networks. The MATLAB has various versions by current MATLAB version is 2015. The MATLAB process elements in the form of MATRIXs and various other languages like JAVA, PYTHON, and FORTRAN are used in MATLAB. This blog article contains the basics of Matlab programming for beginners.
The MATLAB default interface has following parts:
Command Window:- The Command Window is the first important part of MATLAB which is used to show the output of already saved code and to execute MATLAB codes temporarily
WorkSpace :- The workspace is the second part of MATLAB which is used to show allocation and reallocation of MATLAB variables. The workspace is divided into three parts. The first part is MATLAB variable, variable type and third part is variable value
Command History :- The command history is the third part of MATLAB in which MATLAB commands are shown which are executed previously
Current Folder Path :- The current Folder path shows that path of the folder in which MATLAB codes are saved
Current Folder Data: – The Current Folder Data shows that data which is in the folders whose path is given in Current Folder Path
The MATLAB has three Command which are used frequently during Matlab basics programming and these commands are :-
CLC= The ‘clc’ stands for clear command window
Clear all:- The ‘clear all’ command is used to de-allocate the variable from the workspace
Close all:– The close all is the command which is used to close all the interfaces and return you to default MATLAB interface.
Features of Matlab
Following are the main features of Matlab:
Matlab is a high-level language used for complex numerical based computations, visualization, and for the development of different applications.
For exploration and solving problems, Matlab provides an open-end environment.
Matlab provides an extensive library of certain mathematical functions to perform operations on Linear Algebra, Fourier series, statistics, and for other mathematical computations.
Matlab provides tools for to build applications using custom graphical interfaces.
For improving code quality, Matlab provides certain development tools along with built-in graphics for data visualization.
Matlab can be integrated with external applications and programming languages like C, Java.
For working with basics of Matlab, firstly you have to setup the Matlab environment on your system. After the Matlab environment is set, you can execute commands after >> operator.
Enter an expression like 10 + 10 and then press enter.
Matlab will execute this expression and return the result as:
ans = 10
Use of Semicolon(;) in Matlab: Semicolon is used as a terminating point of the statement.
x = 7;
y = x + 8
Matlab will return the answer as:
y = 15
Comments in Matlab: Comments can be added in Matlab using symbol %.
y = 5 % value 5 is assigned to y
Special Variables and Constants in Matlab
Along with commonly used operators, there are some special variables and constants used in Matlab. Following are those variables and constants:
ans – To indicate the most recent answer
Inf – Indicates infinity
eps – Depicts the accuracy of floating-point precision
NaN – Represents Undefined numerical result
i, j – Represents imaginary unit of value under root -1
Matlab is a Case-Sensitive Language
Commands used in Matlab
There are various commands used in Matlab. Following are some of the commonly used commands in Matlab. The list also includes commands which you may not have heard before.
cd – This command is used to change the current directory
dir – This command lists all files in the current directory
what – It lists all Matlab files in the current directory
format – Controls format of the display screen
fscanf – Reading formatted data from a file
fprintf – Performing formatted write to a file
lookfor – Searching help for a keyword
cat – To concatenate arrays
prod – To find the product of each column
eye – To create an identity matrix
num2cell – Converting numeric array to cell array
The list is incomplete as there are a number of other commands in Matlab and these are few of them.
Type of Program Files in Matlab
There are two types of program files in Matlab having .m extension:
Scripts – In these types of program files, series of commands are written which are required to be executed together. These files do not take any input and hence do not return any output.
Functions – Unlike script files, function files accept inputs to return output. These files are also referred to as program files.
Types of operators used in Matlab
Following are the types of operators used in Matlab:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Set Operators
Arithmetic Operators – Arithmetic Operators include the operators used in linear algebra including +, -, *, /, ^. Arithmetic Operators also include Array and Matrix arithmetic operators.
Relational Operators – Following are the types of relational operators used:
Less than –
Less than or equal to – =
Equal to – ==
Not equal to – ~=
Logical Operators – There are two types of logical operators used in Matlab:
Element-wise – To perform operations on elements of logical arrays
Short-Circuit wise – For operation on scalar and logical expressions
Bitwise Operators – These operators are used to perform bitwise operations on bits and work bit-by-bit using truth table for operations.
Set Operators – These operators perform functions like union, intersection, and testing.
Type of loops in Matlab
A loop is a statement which is used to execute other statements multiple times. Following are the three types of loop statements used in Matlab:
Along with these there are also loop control statements used in Matlab:
Vectors in Matlab
A vector is a 1-d array of numbers. There are two types of vectors:
- Row Vectors
- Column Vectors
In Matlab, row vectors are created as:
r = [1 2 3 4 5 6 ]
It will be executed as:
r = 1 2 3 4 5 6
The column vectors are created as:
r = [1 ; 2 ; 3 ; 4 ; 5 ; 6]
It will be executed as:
r = 1
Uses and Applications of Matlab
Matlab finds its use and application in the following areas:
Matlab is used to evaluate a large amount of data collected using computers and other such appliances. Manual processing of this data is a tedious task, therefore Matlab is used in such cases.
- Matlab finds its application in signal processing and communication.
Matlab is used to perform complex matrix operations which is useful in case of image and video processing.
Matlab provides certain graphical and statistical functions along with our such interesting features which makes it popular for a wide range of use.
Matlab is used in computational biology and computational finance.
Advantages of Matlab
Being an interpreted language, Matlab is easy to use. Matlab provides a built-in integrated development environment using which programs can be written easily. Also, there is a Matlab debugger for debugging.
Matlab is platform independent in the sense that it can run on different platforms. Programs written on one platform can run on other platforms as well.
Matlab has a vast library of predefined functions that provide solutions to various technical problems.
Matlab provides an interactive Graphical User Interface(GUI) to users so that he can work with ease with his programs.
Last but not the least, Matlab has a separate compiler that compiles the Matlab program into an executable code. This provides flexibility and platform-independence features to Matlab.
Limitations of Matlab
Although Matlab provides certain high end uses, there are some limitations of using Matlab. Here are some of these limitations:
The main disadvantage of Matlab is that it is an interpreted language which means that it takes more time to execute than other compiled languages i.e. its execution speed is slow.
The second disadvantage is the cost. It is expensive than the regular C or Fortran compiler. Therefore, individuals find it expensive to purchase.
This was the basic introduction to Matlab. There are various interesting topics in Matlab for thesis, research, and small projects. Contact Techsparks if you want to know more on basics of Matlab programming.