Open In App

Introduction to MATLAB

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984.It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorithms and creation of user interfaces.

Getting Started with MATLAB

It is both a programming language as well as a programming environment. It allows the computation of statements in the command window itself.

  • Command Window: In this window one must type and immediately execute the statements, as it requires quick prototyping. These statements cannot be saved. Thus, this is can be used for small, easily executable programs.
  • Editor (Script): In this window one can execute larger programs with multiple statements, and complex functions These can be saved and are done with the file extension ‘.m ‘
  • Workspace: In this window the values of the variables that are created in the course of the program (in the editor) are displayed.
  • Command History window : This window displays a log of statements that you ran in the current and previous MATLAB sessions. The Command History lists the time and date of each session in the short date format for your operating system, followed by the statements from that session.
  • This window displays the exact location(path) of the program file being created.

MATLAB Library comes with a set of many inbuilt functions. These functions mostly perform mathematical operations like sine, cosine and tangent. They perform more complex functions too like finding the inverse and determinant of a matrix, cross product and dot product Although MATLAB is encoded in C, C++ and Java, it is a lot easier to implement than these three languages. For example, unlike the other three, no header files need to be initialised in the beginning of the document and for declaring a variable, the data type need not be provided. It provides an easier alternative for vector operations. They can be performed using one command instead of multiple statements in a for or while loop.

Basic Functions in MATLAB

Function Description
disp() The values or the text printed within single quotes is displayed on the output screen
clear To clear all variables
close all To close all graphics window
clc To clear the command window
exp(x) To compute the exponential value of x to the base e
abs(x) To compute the absolute value of x
sqrt(x) To compute the square root of x
log(x) To compute the logarithmic value of x to the base e
log10(x) To compute the logarithmic value of x to the base 10
rem(x, y) To compute the remainder of x/y
sin(x) To compute the sine of x
cos(x) To compute the cosine of x
tan(x) To compute the tangent of x
atan2(x, y) To compute the arctangent or inverse of y/x

Writing a MATLAB Program

  1. Using Command Window: Only one statement can be typed and executed at a time. It executes the statement when the enter key is pressed. This is mostly used for simple calculations. Note: ans is a default variable created by MATLAB that stores the output of the given computation.
  2. Using Editor: Multiple lines of code can be written here and only after pressing the run button (or F5) will the code be executed. It is always a good practice to write clc, clear and close all in the beginning of the program.Note: Statements ending with a semicolon will not be displayed in the command window, however, their values will be displayed in the workspace. Any statement followed by % in MATLAB is considered as a comment
  3. Vector Operations: Operations such as addition, subtraction, multiplication and division can be done using a single command instead of multiple loops

We can also extract separate rows and columns by using the colon(:) operator. Consider a matrix A of size 3X3. The following commands can be used to extract rows and columns from Matrix A

Command Description
A(:, n) To extract the elements of all rows in column n of the matrix
A(m, : ) To extract the elements of all columns in row m of the matrix
A(:, m:n) To extract the elements of all rows between columns m and n of the matrix
A(m:n, : ) To extract the elements of all columns between rows m and n of the matrix
A(p:q, m:n) To extract the elements of rows between p and q and columns between m and n of the matrix
A(m, n) To extract the elements of row m and column n

Plotting in MATLAB

The MATLAB graphics system consists of high-level commands for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level commands that allows to fully customize the appearance of graphics as well as to build complete Graphical User Interfaces. Given below is a code for plotting a Parabola:

x = 0:0.5:10;

%Indicates that x varies from 0 to 10 with intervals of 0.5

y = x>^2;

%Indicates that square of each element in x is stored in y

plot(x, y)%plotting x and y
xlabel(X)%naming x axis as x
ylabel(Y)%naming y axis as y
title('Graph of y=x^2')%Title of the graph

This code will give the following graph:

You can change the color of the graph by adding another statement in the plot command. For example, plot(x, y, ‘r’) will give the graph line in red. Graphs such as sine, cosine and other trigonometric functions’ curve can also be plotted. The code for sine curve is given below:

x = 0:pi/100:2*pi;
y = sin(x);

plot(x, y, ‘g’)%Will give the graph line in green.MATLAB is a very user friendly language and it is also easy to understand.It’s library is equipped with functions that help us perform a variety of mathematical functions.


Similar Reads

How to Select Random Rows from a Matrix in MATLAB?
A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array. Syntax:matrix_name(i,j)where, i is the row number, and J is the column number which is to be indexed. Example 1: C/C++ Code % MATLAB code for select % random matrix elements mat = magic(5); mat(
2 min read
MATLAB Annotation
Annotations in MATLAB is a way of adding explanation or notes to the plots. Annotations add more information to the plots. There are different syntax formations for adding annotations to a plot: annotation(lineType,x,y)annotation(lineType)annotation(shapeType,dim)annotation(shapeType)annotation(___,Name,Value) Let's discuss all the above functions
3 min read
Global Variables in MATLAB
Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to change the value of the local variable in another fun
4 min read
How to Calculate Harmonic Mean in MATLAB?
Harmonic mean is a type of mean, which is a measure of central tendencies of data, in statistics that gives large weightage to smaller data and small weightage to larger data. The Harmonic Mean in mathematical terms is nothing but the reciprocal of the mean of reciprocal values of all the data elements. Harmonic Mean in MATLAB:MATLAB provides a bui
2 min read
Copy Objects in MATLAB
In MATLAB, there are two kinds of objects - handles and values. The value objects are ordinary MATLAB objects which behave normally to copy operations. It means that when a value object is copied, the new copies of that object are completely independent of the original one i.e., if the original's value is changed, the copied object will not be chan
2 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: function output_params = function_name(iput_params) %
2 min read
Creating Apps Using App Designer in MATLAB
MATLAB is a powerful, high-level programming language. Matlab is widely used for designing systems by engineers and scientists and we all know that the best way to represent any idea is by using a simple but effective GUI. Matlab app builder provides you the power to build different apps, to represent your idea in a GUI-friendly manner. In this art
3 min read
Image Processing in MATLAB | Fundamental Operations
1. Reading Images Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument For Example: >> I=imread('nature.jpg'); This will read JPEG image 'nature' into the image array. Note: The semicolon(;) at the end of command line is used to suppress the output in MATLAB.If ';'
3 min read
MATLAB | RGB image representation
RGB image can be viewed as three different images(a red scale image, a green scale image and a blue scale image) stacked on top of each other, and when fed into the red, green and blue inputs of a color monitor, it produces a color image on the screen. RGB color model is the model in which Red, Blue, and Green colors are blended together to form an
4 min read
MATLAB | RGB image to grayscale image conversion
An RGB image can be viewed as three images( a red scale image, a green scale image and a blue scale image) stacked on top of each other. In MATLAB, an RGB image is basically a M*N*3 array of colour pixel, where each colour pixel is a triplet which corresponds to red, blue and green colour component of RGB image at a specified spatial location. Simi
4 min read