Introduction to MATLAB Command Window

Hello friends! I hope you all had a great start to the new year.

In our first lecture, we had looked at the MATLAB prompt and also learned how to enter a few basic commands that use math operations. This also allowed us to use the MATLAB prompt as an advanced calculator. Today we will look at the various MATLAB keywords, and a few more basic commands and MATLAB functions, that will help us keep the prompt window organized and help in mathematical calculations. We are also going to get familiar with MATLAB’s interface and the various windows. We will also write our first user-defined MATLAB functions.

MATLAB keywords and functions

Like any programming language, MATLAB has its own set of keywords that are the basic building blocks of MATLAB. These 20 building blocks can be called by simply typing ‘iskeyword’ in the MATLAB prompt.

The list of 21 MATLAB keywords obtained as a result of running this command is as follows:

  • 'break'
  • 'case'
  • 'catch'
  • 'classdef'
  • 'continue'
  • 'else'
  • 'elseif'
  • 'end'
  • 'for'
  • 'function'
  • 'global'
  • 'if'
  • 'otherwise'
  • 'parfor'
  • 'persistent'
  • 'return'
  • 'spmd'
  • 'switch'
  • 'try'
  • 'while'

To test if the word while is a MATLAB keyword, we run the command

iskeyword(‘while’)

The output ‘1’ is saying that the result is ‘True’, and therefore, ‘while’ is indeed a keyword.

‘logical’ in the output refers to the fact that this output is a datatype of the type ‘logical’. Other data types include ‘uint8’, ‘char’ and so on and we will study these in more detail in the next lecture.

Apart from the basic arithmetic functions, MATLAB also supports relational operators, represented by symbols and the corresponding functions which look as follows:

Here, we create a variable ‘a’ which stores the value 1. The various comparison operators inside MATLAB used here, will give an output ‘1’ or ‘0’ which will mean ‘True’ or ‘False’ with respect to a particular statement.

Apart from these basic building blocks, MATLAB engineers have made available, a huge library of functions for various advanced purposes, that have been written using the basic MATLAB keywords only.

We had seen previously that the double arrowed (‘>>’) MATLAB prompt is always willing to accept command inputs from the user. Notice the ‘’ to the left of the MATLAB prompt, with a downward facing arrow. Clicking this downward facing arrow allows us to access the various in-built MATLAB functions including the functions from the various installed toolboxes. You can access the entire list of in-built MATLAB functions, including the trigonometric functions or the exponents, logarithms, etc.

Here are a few commands that we recommend you to try that make use of these functions:

A = [1,2,3,4];

B = sin(A);

X = 1:0.1:10;

Y = linspace(1,10,100);

clc

clear all

quit

Notice that while creating a matrix of numbers, we always use the square braces ‘[]’ as in the first line, whereas, the input to a function is always given with round brace ‘()’ as in the second line.

We can also create an ordered matrix of numbers separated by a fixed difference by using the syntax start:increment:end, as in the third command.

Alternatively, if we need to have exactly 100 equally separated numbers between a start and and end value, we can use the ‘linspace’ command.

Finally, whatever results have been output by the MATLAB response in the command window can be erased by using the ‘clc’ command which stands for ‘clear console’, and all the previously stored MATLAB variables can be erased with the ‘clear all’ command.

To exit MATLAB directly from the prompt, you can use the ‘quit’ command.

In the next section, let us get ourselves familiarized with the MATLAB environment.

The MATLAB Interface Environment

A typical MATLAB work environment looks as follows. We will discuss the various windows in detail:

When you open MATLAB on your desktop, the following top menu is visible. Clicking on ‘new’ allows us to enter the editor window and write our own programs.

You can also run code section by section by using the ‘%%’ command. For beginners, I’d say that this feature is really really useful when you’re trying to optimize parameters.

Menu Bar and the Tool Bar

On the top, we have the menu bar and the toolbar. This is followed by the address of the current directory that the user is working in.

By clicking on ‘New’ option, the user can choose to generate a new script, or a new live script, details of which we will see in the next section.

Current Folder

Under the Current Folder window, you will see all the files that exist in your current directory. If you select any particular file, you can also see it details in the bottom panel as shown below.

Editor Window

The Editor Window will appear when you open a MATLAB file with the extension ‘.m’ from the current folder by double clicking it, or when you select the ‘New Script’ option from the toolbar. You can even define variables like you do in your linear algebra class.

The code in the editor can also be split into various sections using the ‘%%’ command. Remember that a single ‘%’ symbol is used to create a comment in the Editor Window.

Workspace Window

Remember that the semicolon ‘;’ serves to suppress the output. Whenever you create new variables, the workspace will start showing all these variables. As we can see, the variables named ‘a’, ‘b’, ‘c’, and ‘x’, ‘y’, ‘z’. For each variable, we have a particular size, and a type of variable, which is represented by the ‘Class’. Here, the ‘Class’ is double for simple numbers.

You can directly right click and save any variable, directly from this workspace, and it will be saved in the ‘.mat’ format in the current folder.

Live Editor Window

If however, you open a ‘.mlx’ file from the current folder, or select the option to create a ‘New Live Script’ from the toolbar, the Live Editor window wil open instead.

With the Live Script, you can get started with the symbolic manipulation, or write text into the MATLAB file as well. Live scripts can also do symbolic algebraic calculation in MATLAB.

For example, in the figure below, we define symbol x with the command

syms x

We click ‘Run’ from the toolbar to execute this file.

The Live Editor also allows us to toggle between the text and the code, right from the toolbar. After that, the various code sections can be run using the ‘Run’ option from the toolbar and the rendered output can be seen within the Live Editor.

Command History

Finally, there is the command history window, which will store all the previous commands that were entered on any previous date in your MATLAB environment.

Figure window

Whenever you generate a plot, the figure window will appear which is an interactive window with it’s own toolbar, to interact with the plots.

We use the following commands to generate a plot, and you can try it too:

X = 1:0.1:2*pi;

Y = sin(X)

plot(X,Y)

The magnifier tools help us to zoom into and out of the plot while the ‘=’ tool helps us to find the x and y value of the plot at any particular point.

Also notice that now, the size of the ‘X’ and ‘Y’ variables is different, because we actually generated a matrix instead of assigning a single number to the variable.

Creating a New User-Defined Function

By selecting New Function from the toolbar, you can also create a new user-defined function and save it as an m-file. The name of this m-file is supposed to be the same as the name of the function. The following template gets opened when you select the option to create a new user-defined function:

The syntax function is used to make MATLAB know that what we are writing is a function filee. Again notice that the inputs to the function, inputArg1 and inputArg2, are inside the round braces. The multiple outputs are surrounded by square braces because these can be a matrix. We will create a sample function SumAndDiff using this template, that will output the sum and difference of any two numbers. The function file SumAndDiff.m looks as follows:

Once this function is saved in the current folder, it can be recognized by a current MATLAB script or the MATLAB command window and used.

Exercises:

  1. Create a list of numbers from 0 to 1024, with an increment of 2.
  2. Find the exponents of 2 from 0th to 1024th exponent using results of the previous exercise.
  3. What is the length of this matrix?
  4. Plot the output, and change the y axis scale from linear to log, using the following command after using the plot function: set(gca, ‘yscale’, ‘log’)
  5. Let us go ahead now and import an image into MATLAB to show you what the image looks like in the form of matrices. You need to have the image processing toolbox installed in order for the image functions to work.

Run the following command in the MATLAB prompt:

I = imread(‘ngc6543a.jpg’);

This calls the image titled ‘ngc6543a.jpg’ which is stored inside MATLAB itself for example purposes. Notice the size of this image variable I in the workspace. You will interestingly find this to be a 3D matrix. Also note the class of this variable.

In the next tutorial, we will deep dive into the MATLAB data types, the format of printing these data types and write our first loops inside MATLAB.

How to use MATLAB

Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge about How to use MATLAB. This is an on demand tutorial. The tutorial will help you to learn the the basics of the MATLAB and will elaborate the use of the different tools of it e.g. command window, m.file, Graphical User Interface (GUI) and simulink. Before into the details of this tutorial you must go through Introduction to MATLAB first for the better understanding of this tutorial. Each of the tools has its own importance while making an algorithm in MATLAB. Command window basically deals with the shorter commands where there is a need of quick execution. Command window shows the result of all of the executed commands if there is no semicolon at the end of the particular statement. You must also have a look at Declaration of Variables in MATLAB. MATLAB provides high level of technical computing. It provides an easy to use environment for computation and programming to solve problems and their solutions can be expressed in mathematical symbolic notations. MATLAB can be used for a lot purposes e.g. computational mathematics, simulation, mathematical modeling, engineering graphics, data analysis, prototyping etc. MATLAB works on the basis of matrices whose basic building is an array. MATLAB system has different parts which will be discussed later in this tutorial. MATLAB has a lot of tool boxes in order to provide easiness to the users. Signal processing tool, fuzzy logic tool, control systems tool and simulation tool are its major tools that are used most frequently.

How to use MATLAB

Here in the tutorial, How to use MATLAB, I will tell you about the basics of the MATLAB and the necessary steps to use the different tools of the MATLAB e.g. command window, GUI, simulink and m.file. First of all I would like to tell you a brief description of the MATLAB software.
Parts of MATLAB
  • There are five different main parts of the MATLAB.
  1. MATLAB Language.
  2. MATLAB Working Environment.
  3. Handle Graphics.
  4. MATLAB Mathematical Functional Library.
  5. MATLAB Application Program Interface.
  • All of these five parts are shown in the figure below.
  • The brief description of each of the part is given below.
1) MATLAB Language
It provides high level matrices language. This language includes the features like flow statements, data structures, object oriented programming, input/output etc. It provides the facilities for programming at lower as well as on higher scales. We have posted many Matlab projects in which I have used MATLAB Language, here are few of these projects:
2) MATLAB Working Environment
This part provides set of tools and facilities to the MATLAB users. The facilities include the management of variables in workspace, debugging, m.files and import and export of the desired data. MTALB working environment is shown in the figure below:
3) Handle Graphics
This is a MATLAB system for graphics and it includes image processing and data visualization in two dimensions as well as in three dimensions. It also includes the designing of complete Graphical User Interface in MATLAB. Handle graphics in MATLAB is shown in the figure below:
4) MATLAB Mathematical Functional Library
  It has a wide range of computational algorithms from the lower level (e.g. sum, sine, cosine) to the higher level (e.g. matrices inverse, matrices eigen values etc.). Mathematical functional library is shown in the figure below.
5) MATLAB Application Program Interface (API)
It provides you a library to write C programs as well as Fortran programs which interact with the MATLAB. It provides a lot of facilities e.g. MAT files. MATLAB application program interface (API) is shown in the figure below.
  • So that was the brief description of the five different parts of the MATLAB.

How to use MATLAB Command Window

Here in this section of the tutorial How to use MATLAB, I will tell you about the the steps to use the command window in MATLAB and its importance in different circumstances. Command window is used to display the results of the algorithm immediately. When you do not specify an output variable whose values is desired to be obtained, then MATLAB uses a variable named as ans which is the short form of the word answer. This step is shown in the figure below. Command window is the window which displays the output in non-graphic form. The symbol >> shows that the MATLAB is ready for the input to be entered.
  • Here I would like to show you the use of command window.
  • I have declared three different variables named as a,b and c.
  • I have stored the value of the sum of first two variables in the third variable.
  • The result is shown in the figure below.
  • Command window will only display the result of that statement which has no terminating semi colon.
  • Now I am going to compute the values of trigonometric functions.
  • The result is shown in the figure below.
  • So that was the overview of how to use MATLAB command window.

How to use MATLAB m File

Here in this section of the tutorial How to use MATLAB, I will tell you about how to use the m.file in MATLAB. It is also known as the editor in MATLAB. You must also visit How to Create m.file in MATLAB for the better understanding.
  • I am going to add the simple two of the variables and storing it into the third variable while creating m.file in MATLAB.
  • The result of the above description is shown in the figure below.
  • Now, I am going to plot a simple sinusoidal signal while creating an m.file in MATLAB.
  • The source code is shown in the figure below.
  • The resulted plotted sinusoidal signal is shown in the figure below.
  • So, that was the brief description about How to create or use m.file in MATLAB.
Note:These are few MATLAB projects in which I have written the MATLAB code in m File:

How to use MATLAB Simulink

Here in this section of the tutorial How to use MATLAB, I will explain you that how to use this amazing tool of the MATLAB. It represents the source code in terms of different blocks. First of all I would like to tell you that how to open the simulink library in MATLAB.
  • You need to follow the steps given below.
  • Open your MATLAB software, a new window with different MATLAB sections will be appeared on your screen.
  • Click on the encircled button as shown in the figure below.
  • As you press this button, a new window having simulink library in it, will be opened on your screen.
  • The simulink library is shown in the figure below.
  • Now, I am going to generate two simple sinusoidal signal in MATLAB simulink.
  • Press Ctrl+N and a new window will appear upon which we need to place the desired blocks.
  • Go to the Sources and select the sine wave block from it.
  • Drag this blcok and drop it on the second window as shown in the figure below.
  • Similarly copy and paste the same block.
  • Here I am going to add the both of the sinusoidal signals having phase shift of 90 with each other.
  • I have placed a scope to observe the addition of the both of the signals visually.
  • All of the above steps are shown in the figures below.
  • The selection of the add block is shown in the figure below.
  • The selection of the scope is shown in the figure below in order to visualize the results.
  • Now, the complete source code for the addition of two sinusoidal signals is shown in the figure below.
  • After making this algorithm just run the program by clicking on the button encircled in the above figure.
  • Double click on the Scope in order to visualize the desired result.
  • The result of the addition of both the signals is displayed on the scope and is shown in the figure below.
  • So, that was the brief description about how to open and use simulink library in MATLAB.

How to use MATLAB GUI

In this section of the tutorial How to use MATLAB, I will explain you about how to create Graphical User Interface (GUI) in MATLAB and how to operate it. GUI displays the desired results visually which helps us in better understanding of the algorithm instead of observing the results in analog form or in digital data. It also provide better external look to the designed algorithm. You should also have a look at How to Create a GUI in MATLAB. I have designed many projects using GUI in MATLAB so you go through these below projects: In order learn that how to perform different tasks using GUI in MATLAB, you need to follow the below steps:
  • Go to the command window and type guide there.
  • The step is shown in the figure below.
  • When you press Enter a new window will be appeared on your screen.
  • Go to the Create New GUI and select the blank GUI and hence a new GUI will be opened and its source code will be generated automatically in m.file of the MATLAB.
  • The above steps are shown in the figure below.
  • Now, pres OK button and a GUI will be appeared on your screen.
  • The GUI appeared on the screen is shown in the figure below.
  • Now you can pick and place the buttons, tables and graphs from the left side of the GUI.
  • I picked a graph, a table and a button from the left bar of the GUI.
  • All of the above steps are shown in the figure below.
  • That was the overview of the basics of the MATLAB.
So that is all from the tutorial How to use MATLAB. I hope you enjoyed this tutorial. If you face any sort of problem regarding anything, you can anytime ask me in comments, freely, without even feeling any kind of hesitation. I will try my level best to solve you issues in some better way, if possible. I will explore MATLAB and other software and hardware too in my later tutorials later and will surely share them with all of you guys as well. So, till then, Take Care :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir