How to use Print 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 Print MATLAB. As you all know that the design of an algorithm is not enough until we observe its results too. So, there should be a debugging tool in order to check the performance and accuracy of the designed algorithm. Debugging tool helps us to improve the algorithm’s performance and accuracy too by observing its output again and again. In this tutorial I will tell you about using
print command in MATLB for different purposes. So, in this tutorial I am going to use print command as a debugging tool in order to observe the output of the algorithm. You should also have a look at
how to use MATLAB ?
I am going to use print command to either print the desired statement on the command window in MATLAB or to display the graphs on a figure and to automatically save it on any destination wherever you want to. I will elaborate this command with the help of the different examples later in this tutorial. if you new to MATLAB then you should read
Introduction to MATLAB. So, now let's have a look at How to use Print MATLAB:
How to use Print MATLAB
In the tutorial
How to use Print MATLAB, I will tell you about the use of this command in MATLAB while explaining a lot of examples and by displaying their results. The examples include the printing of the statement on the command window in MATLAB as well as to display the graphs and to save its image.
- Here, I am going to print some statement (the name of our website) on the command window using this particular command in m.file.
- The use of the command fprintf is shown below.
clc % clears the command window
fprintf('www.TheEngineeringProjects.com\n')% prints the desired statement
- Editor also displays that command in the figure shown below.
- You can observe the result of this statement on the command window in MATLAB.
- The result displayed on the command window is shown in the figure below.
- So, that was about how to print the statement on the command window.
- Now, I am going to print the result of some mathematical operations applied on some variables, on command window using the same command.
- I have assigned different values to the two variables a and
- I have calculated their sum and displayed on the command window.
- The commands written on the editor are shown in the figure below.
- The result of the above commands displayed on the command window is shown in the figure below.
- So, that was the description of how to print the results mathematical operations applied on some variables on command window.
- Now, I am going to perform some operations on matrix.
- The source code written in editor of the MATLAB is shown below.
- I have initialized a matrix named as
- I have then displayed that matrix on the command window.
- At the end, I have printed all the values of that matrix on the command window.
- The result of all the above commands displayed on the command window is shown in the figure below.
- First row in the above figure displays the matrix.
- And the second row prints each value of the matrix in next ling using \n.
Graphics Format File
- You need to specify file name and format switch to save your figure as graphics.
- The supported output formats for exporting from MATLAB and switch settings are shown in the table given below.
Print Paper Copy of Figure
In this method a new bar chart will be created and printed to your system default computer. If you do not specify the figure to be printed, then print will use the current figure.
- The source code written on the editor is shown below.
clc
bar(1:10);
print
- The editor window with the code is also shown in the figure below.
- The figure generated from the above code is shown below.
- That was the detailed discussion about using print command as a debugging tool in MATLAB.
So, that is all from the tutorial How to use Print MATLAB. I hope you enjoyed this tutorial. If you face any sort of problems regarding anything, you can ask me freely in comments every time, without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. There's another interesting MATLAB Projects named as
Eye Ball Detection in MATLAB. I will explore different software including MATLAB in my next tutorials and will share all of them with you guys as well. So, till then, take care :)
Multi Color Detection in MATLAB
Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge about
Multi Color Detection in MATLAB. Color Detection scheme plays a vital role at some places. Like in Image processing technique the detection of specific color among the different colors is necessary to perform some action on it. For example there are three cars of the colors red, blue and green and we want to wash the red car. You should also have a look at
Color Detection in Images using MATLAB because I am gonna use the same code and will add some improvements in it. Moreover, if you are working on Live Videos instead of Images then you should have a look at
Color Detection in Live Video using MATLAB.
For this purpose we need to design an algorithm which is capable of detecting red color among the different colors. Similarly there are a lot of other examples are there to perform the tasks like that. This tutorial will elaborate the step by step explanation of the algorithm designed for multi color detection in MATLAB using Graphical User Interface (GUI). The tutorial consists of two different parts. In first part the GUI will be created while the second part will consist of an algorithm to detect a particular color among the different colors. Further details about the designed algorithm is elaborated in the section below.
Multi Color Detection in MATLAB
Here, in the tutorial
Multi Color Detection in MATLAB, I will explain the step by step description about designing an algorithm for multi color detection in MATLAB GUI. The total project is divide into two parts, GUI will be created in the first part and algorithm designing will be explained in the second part.
- You can download the complete MATLAB GUI simulation here by clicking on the button below.
- Download .rar. file, extract it and enjoy the complete package :)
Download MATLAB Simulation
GUI Design for Multi Color Detection in MATLAB
This section of the tutorial Multi Color Detection in MATLAB will elaborate you all of the steps involved during the design of Graphical User Interface (GUI).All of the steps are given below.
- GUI design consists of several different steps which are explained below.
- GUI consists of the two different panel.
- The left panel is for browsing or loading an image from the laptop or computer.
- Left panel has a button to browse an image, an axis to visualize the loaded image and edit box to view the image's complete path.
- The right panel is designed for color detection for the browsed image.
- Right panel has three different buttons in order to detect red, green and blue colors and an axis to display the detected color on the image loaded in the left panel.
- So that was the detailed description of the GUI, designed for multi color detection in MATLAB.
- Initially designed GUI without panels is shown in the figure below.
- The updated GUI with both the panels with proper tags is shown in the figure below.
Other Image Processing Projects:
Algorithm Design for Multi Color Detection in MATLAB
In this section of the tutorial Multi Color Detection in MATLAB, I will explain you about the design of an algorithm for detecting multi colors from the image loaded into GUI. The algorithm is further divided into two different parts. In the first part image is loaded into the GUI for the detection of multi color. Whereas, the second step consists of the detection of multi colors and its display on the GUI.
- Go to the call back back function of Browse button and just copy paste here, the MATLAB code given below.
global ImageFile
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
ImageFile = strcat(pathname, filename);
axes(handles.axes1);
imshow(ImageFile) % displays the image on particular axis
set(handles.edit1,'string',ImageFile); %sets the text of the edit box on GUI
- I have first write a line to load the image from its path into the GUI.
- After loading the images I have adjusted an axis on which the image is to be displayed.
- At the end I have set the text of the edit box as the address of the image.
- So that the was the logic to load an image into GUI of MATLAB.
global ImageFile
ImageFile
data = imread(ImageFile);
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); % subtracts the red color from the image
%Use a median filte tFileo filter out noise
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
axes(handles.axes2); % axis adjustment
imshow(data) % displays the image with the detected colors
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'blue');
end
hold off
- The main part of this code is to subtract the red color from the loaded image and to display it on the axis.
- I have made rectangular areas around the detected colors at the end.
- So that was the description of the algorithm to detect the red colors from the loaded image.
- The final Updated GUI is shown in the figure below.
- The image is to be loaded in the left panel of the updated GUI.
- Loaded image in to the GUI is shown in the figure below.
- Color will be detected in the right panel.
- Red color detected from the loaded image is shown in the figure below.
- Green color detected from the loaded image is shown in the figure below.
- Blue color detected from the loaded image is shown in the figure below.
So that is all from the tutorial Multi Color Detection in MATLAB. I hope you enjoyed this tutorial. If you face any sort of problems, you can freely ask me in comments any time without even feeling any kind of hesitation. Our team will try our level best to entertain you and to solve your issues in some better way, if possible. I will explore MATLAB as well as other software in my later tutorials and will surely share all of them with you guys as well. So, till then, Take Care :)
How to use MATLAB input Function
Hello everyone! I hope you all will be fine and having fun. Today, I am going to share my knowledge about
How to use MATLAB input Function. In this tutorial I would like to tell you about the importance of getting data from the user in MATLAB. If you want to display the data to users, you need to get data from them as well according their requirements. I hope you have read
How to use MATLAB ?
In this way the same source code written in MATLAB will give different result according to the requirements of the each user. So, in other words the source code becomes generic in this way. As we run the program, command window of the MATLAB asks user to enter the desired input. The results on this window are generated according to the
Matlab input given by the user. You should also have a look at Laplace Transform in MATLAB.
How to use MATLAB input Function
Here, in this tutorial I will explain you the importance and the procedure about
How to use MATLAB input Function. The one of the main advantage of getting the data from the user is, the algorithm becomes generic in this way. The different users can get different types of results from the same program according their requirements. The user has to enter the input on the command window and can get the desired results according to the given input.
Getting User Data
- First of all open your MATLAB software and go to the editor in MATLAB.
- Write a simple code in editor as shown below.
clc
x=input('Enter the value of x = ');
y=input('Enter the value of y = ');
x+y
- Move to the command window and observe the results.
- As you Run the program you will automatically move to the command window.
- Command window will ask to enter the value of the first variable, which is x in this case.
- The result of the above program is shown in the figure below:
Note:You should also have a look at these MATLAB projects as well:
- After entering the value of x command window will ask to enter the value of the second variable, which is y in this case.
- The figure shown below displays the above results.
- As you enter the value of y and press Enter, you will be able to see the result of the addition of the both of the variables.
- The result is shown in the figure below.
- Till now, I have performed addition operation on the variables whose values are given by the users and the program shows the different results for each user.
- You can also perform a lot of other operations on these variables.
- Here, I am going to perform basic mathematics operations on these two variables.
- The code for multiplication operation is shown below.
clc %clearing command window
x=input('Enter the value of x = ');%takes the value of x from the user
y=input('Enter the value of y = ');%takes the value of y from the user
product = x*y %performing multiplication
- The input MATLAB code will look something as shown in below figure:
- The result on the command window is shown in the figure below.
- Now, I am going to perform the division operation on the both of the variables.
- The source code for the division operation is shown below.
clc %clears comand window
x=input('Enter the value of x = '); %takes the value of x from the user
y=input('Enter the value of y = '); %takes the value of y from the user
product = x/y %performing division operation
- The result of the above source code is shown in the figure below.
- Similarly you can perform a lot of other operations on the variables whose values are given by the user.
- So, that was the brief discussion about How to use MATLAB input Function.
- Here's the video in which I have explained in detail How to use MATLAB input Function:
So, that is all from the tutorial How to use MATLAB input Function. I hope you enjoyed this tutorial. If you find any sort of problem, you can ask in comments anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. You should also have a look at
Introduction to MATLAB. In my next tutorial I will elaborate that how to generate rectangular signals in MATLAB and how to manipulate them. I will further explore MATLAB in my later tutorials by making further projects on it and will share them with all of you as well. So, till then, Take Care :)
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.
- MATLAB Language.
- MATLAB Working Environment.
- Handle Graphics.
- MATLAB Mathematical Functional Library.
- 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 :)
How to Clear MATLAB Windows
Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge with all of you guys about
How to Clear MATLAB Windows. If the command window and workspace window are not clear they present a very complicated look and hence the user feel it difficult to write code on command window. Here, I have compiled a list of awesome
MATLAB Image Processing Projects so must check them out to get some inspiration. :)
So, for the ease of the user we need to clear the both window specially the command window because it contains the entire code for any of the task. Before going into the details of this tutorial, you should go through
Introduction to MATLAB and
Declaration of Variables in MATLAB for the better understanding of this tutorial. If we want to avoid the complications while writing the code on command window we must need to clear the both command window as well as the workspace window. If you don't wanna work on Command Windows then you should have a look at
How to Create m File in MATLAB. Anyways, in today's tutorial we are gonna have a look at matlab clear command window and its quite easy but I must say its quite handy, especially when you are working on some complex MATLAB Project.. So, now let's get started with today's tutorial and have a look at How to Clear
MATLAB Windows:
How to Clear MATLAB Windows ???
In the tutorial,
Clearing MATLAB Windows I will show that which commands are required to use to clear the command window and workspace window In order to avoid the complications for the user while writing the source code on the command window.
- The commands for clearing the command window and workspace window are shown below.
- clear all (used to clear the workspace window)
- clc (used to clear the entire command window)
- The command window with some of the commands written on it is shown in the figure below.
- The workspace window having some of the variables is shown in the figure below.
Clearing both the Windows
- First of all I will show you that how to clear the workspace window.
- Go to the command window and write the command clear all and press
- All of the above steps are shown in the figure below.
- You will see that all the variables from the workspace window has been removed.
- The cleared workspace window is shown in the figure below.
- Now go to the command window and write the command clc on it.
- The command written on the command window is shown in the figure below.
- If you press Enter after writing the command show in the figure above, on the command, you will see that the command window has be cleared.
- The empty command window is shown in the figure below.
- So, by clearing both of these window a user can easily make an algorithm without any complication.
- You should also have a look at this video tutorial as well:
That is all from the tutorial, Clearing Command Window and Workspace in
MATLAB. I hope you enjoyed this tutorial. If you face any sort of problem in clear command window matlab, you can ask me in comments anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will explain you that how to Calculate the Values of the Trigonometric Functions in MATLAB. I will explore MATLAB further in my later tutorials and will share all of them with all of you as well. So, till then, Take Care :)
How to Create m File in 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 Create m File in MATLAB. It is also known as
New Script or
Editor in the MATLAB. It's an amazing MATLAB tool. We can write the complete source code on editor. If we want to visualize the result of the particular statement we should not put a semicolon after that statement in MATLAB. Before going into the details of this tutorial, you must first have a look at
Introduction to MATLAB and if you don't know about MATLAB variables then you must read
Declaration of Variables in MATLAB.
By doing this and by pressing the
Run (Green Color) button at the top of the editor we will be able to see the result on the command window. In other words, we have to write the entire source code in the editor and to run the program and the desired results will be displayed on the command window. It is easy to do so as compared to write and manipulate every single statement on command window.
How to Create m File in MATLAB ???
Here, in the tutorial
How to Create m File in MATLAB, I will show you the step by step procedure to create an m file in MATLAB first and then to observe the desired results on command window.
- First of all you need to open your MATLAB software.
- Go to the top left corner of the front window of the MATLAB and click on New Script.
- The above steps are shown in the figure below.
- A new window will be appeared on your screen right after clicking on the New Script.
- The second window appeared on the screen is shown in the figure below and it is also known as an Editor.
- A very simple code written on the editor by declaring three different kinds of variables and their manipulation is shown in the figure below.
- Now, if you want to observe the result on the command window you have to do two things.
- The first thing is to remove the semicolon at the end of the statement as shown in the figure above.
- And the second thing is to press Run button encircled in the figure shown above.
- After following all of the above steps you will be able to visualize the results on the command window of the amazing software i.e. MATLAB.
- The result observed on the command window is shown in the figure below.
- You can see the result is obtained in the form of an equation.
- That was the detailed discussion on Creating m.file in MATLAB.
So, that is all from the tutorial Creating m.file in MATLAB. I hope you enjoyed this tutorial. If you face any sort of problem you can ask me in comments anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will explain you that how to Calculate the Values of the Trigonometric Functions in MATLAB. I will explore MATLAB further in my later tutorials and will share all of them with all of you as well. So, till then, Take Care :)
Declaration of Variables in MATLAB
Hello everyone! I hope you all will be absolutely fine and having fun. In the tutorial
Declaration of variables in MATLAB, I will elaborate you that how to declare different variables in MATLAB and how to manipulate those variables without assigning them with the values. Before going into the details of this tutorial you must go through
Introduction to MATLAB, because understanding MATLAB first is better. This will help you in better understanding of this tutorial.
Variable declaration is one of the most important and compulsory steps while writing the code to perform any task. Without the declaration of the variables we can not proceed further to perform our task properly. So, if we want to do the different tasks in a proper way we must need to declare the variable before doing anything in the code. Once the variables have been declared, we can proceed further to explore the desired task.
Declaration of Variables in MATLAB
Here, in the tutorial
Declaration of variables in MATLAB, I will show you that how the variables can be declared in the MATLAB and how to manipulate them without assigning them with the values. The command
syms is used to declare the different variables in the
MATLAB.
- Here, I will show you that what happens if you have not declared the variables.
- The error is shown in the figure below, when you have not declared the variables.
- I have written an equation having three different variables named as f,t and r without declaring these variables.
- MATLAB has shown an error on the command window as undefined function or variable.
- So the error shown in the above figure shows the desired results can not be obtained without declaring the variables first.
- Now, I am going to declare all the variables first and will then manipulate them.
- The declared variables are shown in the figure below.
- Since I have declared all of the variables now which are being used in the equation.
- So, now I am going to manipulate them and going to observe the results.
- The result of the addition of the two variables, which is stored in the third variable, is shown in the figure below.
- The above figure shows the result of the addition of the two different variables in the form of an equation.
- The result has been obtained because all of the variables has been declared first, i.e. declaration of variables plays a very important role while writing an algorithm to perform any task either simple or complicated.
- You can also see all of the process in the video given below:
So, that is all from the tutorial Declaration of Variables in
MATLAB. I hope you enjoyed this tutorial. If you find any sort of problems you can ask me in comments without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will explain you that how to create an m.file in MATLAB. I will further explore MATLAB in my later tutorials and will share all of them with you as well. So, till then, Take Care :)
Introduction to MATLAB
Hello everyone! I hope you all will be absolutely fine and having fun. In this tutorial, I am going to give you a detailed
Introduction to MATLAB. I will explain in detail, step by step procedure of using
MATLAB for the first time. First of all, I would like to tell you a bit about the MATLAB software. MATLAB basically works on matrices and it is an excellent software for different kinds of simulation based projects as well as hardware based projects and specially, it is good for the
Engineering Projects.
MATLAB can be used for the manipulation purpose in order to obtain the proper results. It is very easy to use this software and most of the students prefer to use this software for their projects. So, this shows that MATLAB is a user friendly software as well. MATLAB has a very wide range of applications. It can also be used for the real time projects. Robotics, image processing and medical related projects are its major applications. You should also have a look at
How to Create GUI in MATLAB. So, let's get started with
Introduction to MATLAB:
Introduction to MATLAB
So, in the Introduction to MATLAB, I will show you the step by step procedure about how to use the MATLAB software for the first time.
- As you open the MATLAB software, a new window will be appeared on your screen.
- The window is shown in the figure below.
- The above window is the starting window which appears on the screen when you open the MATLAB.
- After sometime, you will be able to see another window on the screen.
- The second window is shown in the figure below.
Sections of MATLAB
There are six basic section of the MATLAB and understanding them is essential part of Introduction to MATLAB. The names of all of these sections are given below.
- Current Folder
- Command Window
- Workspace
- Command History
- View or Change Directory
- Help Section
Current Folder is the first section of the MATLAB’s front window at the left side of the window. It displays the files that has already been saved. You can open the files that are already saved somewhere in your PC or laptop.
- The current folder section is shown in the figure below.
Command Window is the second section and is used to show the results of the entire source code. If we want to observe the results on the command window, we must not put a semicolon after the statement then. If there is a semicolon at the end of the statement, result of that statement will not be shown on the command window.
- The command window section is shown in the figure below.
Workspace is the third section of the MATLAB’s front window which shows all the variables that are used while writing the complete algorithm.
- Workspace section is shown in the figure below.
Command History is the fourth and the last section of the MATLAB’s front window. It shows all of the commands that have been used in the entire algorithm.
- The command history section is shown in the figure below.
View or Change Directory helps us to view the directory or the complete destination of the file that is opened in the MATLAB. You can also open a new file by changing its directory.
- Directory box is shown in the figure below.
Help Section is also an amazing tool of the MATLAB. It provides help regarding of the command or any other thing related to MATLAB. By typing the thing which is not clear, you will be able to find a lot of help.
- The button to open the help section is shown in the figure below.
- As you press this button a new named as help will be appeared on your screen to help you.
- The help window is shown in the figure below.
- So that was the brief discussion about how to getting started with the MATLAB.
So, that is all from the tutorial Introduction to MATLAB. I hope you have enjoyed this tutorial. If you find any sort of problem in introduction to matlab, you can ask in comments anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will elaborate the
Declaration of Variables in MATLAB and how to manipulate them without assigning them with the values. Moreover, you should also have a look at
How to Create m file in MATLAB. I will further explore MATLAB in my later tutorials. So, till then, Take Care :)
DC Motor Direction Control in LabVIEW
Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge with all of you about how to make a simple program for
DC Motor Direction Control in LabVIEW. In my previous tutorials, I have also worked on
DC Motor Direction Control using Arduino. You should go through these tutorials they will be helpful in better understanding of the tutorial DC Motor Direction control using NI LabVIEW. The word DC
is basically an abbreviation of
Direct current. So, a direct current motor is commonly used motor having two input terminals, one is positive and the other one is negative. If we connect these terminals with the voltage supply the motor will rotate. If you change the polarity then motor will rotate in opposite direction. You should also have a look at
Difference between DC & AC Motors to get a better idea about these motors.
DC motor has a lot of applications. You can use it in automation projects, for controlling static as well as mobile robots, in transport system, in pumps,fans,bowers and for industrial use as well. In this tutorial I will work on
DC Motor Direction Control using NI LabVIEW. In my previous tutorial, I have done the
DC Motor Direction Control in MATLAB and I have used the same hardware but instead of controlling it from NI LabVIEW I have controlled it using MATLAB so you must have a look at that tutorial. Now let's get started with DC Motor Direction Control in LabVIEW.
DC Motor Direction Control in LabVIEW
In this tutorial, I will make a simple program to work on the DC Motor Direction Control in LabVIEW. NI LabVIEW is an amazing software tool specially for the students, because it is very easy to use and understand. So, its a student friendly tool. Before going into the details of this tutorial, you must go through my previous tutorials because I am going to use the same hardware setup and same Arduino source code as well. I will made a simple GUI (Graphical User Interface) in LabVIEW for
DC Motor Direction Control in LabVIEW. There will be three different buttons on the GUI for clockwise rotation, counter clockwise rotation and stopping the stepper motor respectively.
- You can download the complete SImulation for DC Motor Direction Control using NI LabVIEW here:
Download LabVIEW Simulation
- Download .rar file, extract it and enjoy the complete simulation for DC Motor Direction Control using NI LabVIEW.
How to Build Complete VI
- First of all open NI LabVIEW software on your laptop or PC so that we could design the GUI for DC Motor Direction Control in LabVIEW.
- Go to the Block Diagram window and Right Click on it.
- Go to Functions-> Instrument I/O-> Serial and you can see different serial blocks like VISA Write, VISA Read, VISA Serial etc.
- Choose the encircled VISA Configure Serial Port and place it on the Block Diagram window.
- VISA Configure Serial Port block will help us to open the Serial Port before executing the algorithm.
- The screen shot of the Block Diagram is shown in the figure below.
- Go to the first input terminal of the VISA Configure Serial Port block and go to Create-> Constant.
- Above step will be helpful to select the COM port of the Arduino board in order to run the program properly.
- Updated Block Diagram window is shown in the figure below.
- Now go to Functions-> Instrument I/O-> Serial, you can see there different serial blocks.
- Choose the encircled VISA Close block and place it on the Block Diagram window.
- The VISA Close block is shown in the figure below and it will be help in closing the Serial Port if needed.
- Now, go to the Functions-> Programming-> Structures and you can see the different structures there like For Loop, While Loop, Case Structure etc.
- Choose the encircled block as shown i the figure below.
- Place all the above blocks in a way shown in the figure below.
- Now, go to the Functions-> Programming-> Structures-> Flat Sequence.
- Flat sequence block is encircled and is shown in the figure below.
- Put your cursor and go to Add Frame After.
- Similarly ad another case after this as shown in the figures below.
- Newly added frame is shown in the figure below.
- Now, go to Functions-> Instrument I/O-> Serial, you can see different serial blocks there.
- Choose the encircled VISA Write Block and place it on the Block diagram window.
- The figure shown below elaborates the above steps.
- Make the connections as shown in the figure below.
- Now, go the Functions-> Programming-> Structures and you can see different types of structures like for loop, while loop, flat sequence etc.
- Choose he encircled block as shown in the figure below.
- Select the Case Structure block and place it on the block diagram window.
- The figure shown below displays the above step.
- Now, go to the input terminal of the write block and go to Create-> Control.
- Change the name of this block to Command box as shown in the figure below.
- The block diagram window is shown in the figure below.
- Now, go to Functions-> Programming-> Structures and you can see different structures blocks there.
- Choose the encircled block as shown in the figure below.
- Select the Local Variable Block and place it on the Front Panel.
- Right click on it and select Command box as shown in the figure below.
- Go to the input terminal of this local variable and go to Create-> Constant.
- Place C inside that constant.
- The figure below elaborates the above step.
- The above case structure is for the clock wise rotation of the stepper motor.
- Similarly make two further case structures for counter clockwise rotation and stopping the rotation of the stepper motor.
- All the three case structures are shown in the figure below.
- You can see three different case structures in above figure.
- The command box variable having command C will rotate the stepper motor in clockwise direction.
- A command box variable having command A will rotate the stepper motor in counter clockwise direction.
- The command box variable having command S will stop the rotation of the stepper motor.
- Now, go to the Front Panel and Right Click on it.
- Go to Controls-> Modern-> Boolean and you can see there different Boolean blocks.
- Choose the encircled block as shown in the figure below.
- Select the Round LED block and place it on the front panel.
- Similarly select two more round LED blocks and place them on the front panel as well.
- Change their names from default to Clockwise, Anti clockwise and Stop Motor.
- All of the above steps are explained visually in the figure shown below.
- The LED shown in the above figure will control the stepper motor on clock wise, counter clock wise direction and will stop the motor as well.
- Now go to the block diagram window and connect these blocks as shown in the figure below.
- At the end, after sending all the commands we must need to close the serial port so that unnecessary exchange of commands could be avoided.
- So I have cleared the all the commands in third frame of the case structure i.e I am sending no commands through the serial port.
- This will be helpful in closing the serial port.
- The figure show below explains all of the above steps visually.
- Now add another case structure to start the program when you want so.
- The figure below shows the newly added case structure.
- Now, go to the Front Panel, the button encircled in the figure shown below is used to start the program when needed.
- Now add a Stop button in order to terminate the program whenever you want so.
- The complete output of the program is shown in the figure below.
- A complete NI LabVIEW Virtual Instrument (VI) is shown in the figure below.
Decorated Front Panel
- Since, I want to make the better external look of the program for DC Motor Direction Control in LabVIEW, so I have decorated a bit.
- The figure shown below shows the decorated Front Panel.
- Go to Controls, Modern-> Decorations you can see different decoration blocks there.
- All these blocks are shown in the figure displayed below.
- I have used three decoration block encircled with the red color, to decorate my program.
- Thick red boundary shows all of the decoration blocks to make your program attractive.
- You can also decorate your programs using this amazing tool.
This is all from the tutorial DC Motor Direction Control in LabVIEW. I hope you all enjoyed this tutorial. If you face any sort of problem in DC Motor Direction Control in LabVIEW, then you can ask me anytime without feeling any kind of hesitation. I will try my level best to solve your issue in a better way if possible. I will explore NI LabVIEW further in my later tutorials. Till then, Take care :)
Stepper Motor Speed Control in Matlab
Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to tell you guys that how to design a simple algorithm for
Stepper Motor Speed Control in MATLAB by simply sending different commands to the serial port from Matlab to Arduino. You should first read
Stepper Motor Speed Control using Arduino because I am gonna take that tutorial further and will do the Stepper Motor Speed Control in Matlab. So, I would suggest you to first have a look at that tutorial before reading this one. Moreover, you should also have a look at
DC Motor Direction Control in Matlab,
DC Motor Speed Control using Arduino. Stepper Motor Direction Control Direction Control in Matlab, Stepper Motor Direction Control using NI LabVIEW and Stepper Motor Speed Control using Arduino in which I have discussed in detailed about how to make simple and easy programs to control the direction of DC motor in both clockwise and anti clockwise direction and how to control the stepper motor direction and speed as well.
So, you must go through my previous tutorials before going into the detail of today's tutorial because I have used the same hardware setup and a bit changed Arduino source code. Now, in this tutorial I will elaborate you that how can you send different commands through serial port from Matlab to the Arduino. I will create a simple GUI having different buttons on it through the direction as well as the speed of the DC motor can easily be controlled. So, let's have a look at
Stepper Motor Speed Control in MATLAB.
Stepper Motor Speed Control in Matlab
Here, in this tutorial I will explain about how can make a program for Stepper Motor Speed Control in Matlab
by creating a GUI having different buttons on it to control the direction of rotation of the stepper motor as well as the speed of the stepper motor by sending different commands through the serial port.
- Here, you can download the complete simulation for Stepper Motor Speed Control in Matlab by clicking on the button below.
Download MATLAB Simulation
- Download .rar file, extract it and enjoy the complete Matlab simulation.
Block Diagram
- First of all I would like to elaborate the algorithm for Stepper Motor Speed Control in Matlab with the help of a block diagram.
- Block diagram is shown in the figure below:
- Fist of all power is supplied to the whole system to run it.
- Matlab sends commands towards the Arduino UNO through the serial port using serial communication.
- Arduino transfers those commands to the L298 motor controller andL298 drive the motor.
- Arduino prints the executed commands on the LCD as well.
Initial GUI
- First of all I have created a very simple GUI (Graphical User Interface) for Stepper Motor Speed Control in Matlab.
- GUI has two different panels, Serial Port and Motor Controls.
- Serial Port panel has two buttons for opening and closing the serial port.
- Motor Controls panel has five buttons for clockwise and anti clockwise rotation, accelerating and deaccelerating the Stepper motor and stopping the stepper motor.
- Screen shot of the simple GUI for Stepper Motor Speed Control in Matlab is shown in the figure below.
- Then I made few of the changes by double clicking on all the buttons to make the GUI beautiful.
- The updated GUI is shown in the figure below.
- The figure shown above is the starting GUI when you just run the program.
- You can see below, after entering different commands GUI updates its text according to the command sent to the serial port.
Source Code Description
- First of all you must have to open the serial port by clicking on the button Start Serial.
- Go to the callback function of Start Serial button in the Matlab code.
- Just copy and paste the source code given below, in its callback function.
clc
global tep;
disp('Welcome to TEP');
disp('');
disp('www.TheEngineeringProjects.com');
disp('');
tep=serial('COM5'); % assign serial port object
set(tep, 'BaudRate', 9600); % set BaudRate to 9600
set(tep, 'Parity', 'none'); % set Parity Bit to None
set(tep, 'DataBits', 8); % set DataBits to 8
set(tep, 'StopBit', 1); % set StopBit to 1
%display the properties of serial port object in MATLAB Window
disp(get(tep,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits'}));
fopen(tep); % Open Serial Port Object
disp('Serial port is opened');
set(handles.text3, 'String','Serial port is opened');
- Now, as you press this button GUI will update its text as Serial port is opened.
- The updated GUI is shown in the figure below.
- Now, go the callback function of Clockwise button inside the Matlab source code.
- Just copy and paste the code given below in its call back function.
global tep
fwrite(tep,'C'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “C”.');
set(handles.text1, 'String','Motor is rotating in clockwise direction'); %setting static text's text
- Now, if you press the Clockwise button the command C will be sent from Matlab to arduino via serial port.
- The GUI will update its text as Motor is rotating in clockwise direction.
- The screenshot of the updated GUI is shown in the figure below.
- Go to the callback function of Accelrate button and copy paste the code given below, in its callback function.
global tep
fwrite(tep,'H'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “H”.');
set(handles.text1, 'String','Motor is accerating slowly'); %updating text on GUI
- As you press the Accelrate button the command H will be sent from Matlab to Arduino via serial port.
- The GUI will update its text as Motor is accelerating.
- If you press this button again and again, motor will continue to rotate at higher and higher speed.
- The screenshot of the updated GUI is shown in the figure below.
- Now, go the callback function of the Deaccelrate button in the Matlab source code.
- Just copy and paste the code given below in the callback function of this button.
global tep
fwrite(tep,'L'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “L”.');
set(handles.text1, 'String','Motor is deaccerating slowly'); %updating GUI text
- As you press the Deaccelrate button the command L will be sent from Matlab to Arduino via serial port.
- The GUI will update its text as Motor is deaccelerating.
- If you press this button again and again, motor will continue to rotate at slower and slower speed.
- The screenshot of the updated GUI is shown in the figure below.
- Now, go the callback function of the Anti Clockwise button in the Matlab source code.
- Just copy and paste the code given below in the callback function of this button.
global tep
fwrite(tep,'A'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “A”.');
set(handles.text1, 'String','Motor is rotating in anti clockwise direction'); %updating the text onto the GUI
- As you press the Anti Clockwise button the command A will be sent from Matlab to Arduino via serial port.
- The GUI will update its text as Motor is rotating in anti-clockwise direction.
- If you press this button again and again, motor will continue to rotate in anti clockwise direction.
- The screenshot of the updated GUI is shown in the figure below.
- Go to the call back function of the Stop Motor button inside the Matlab source code.
- Copy and just paste the code given below in its all back function.
global tep
fwrite(tep,'S'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “S”.');
set(handles.text1, 'String','Motor is stopped');%Updating text on the GUI
- Now, if you press the Stop Motor button the command S will be sent from Matlab to Arduino via serial port.
- The GUI will update its text as Motor is stopped.
- The screenshot of the updated GUI is shown in the figure below.
- Go to the call back function of the Stop Serial button in the Matlab code.
- Copy and just paste the code given below in its call back function.
global tep;
fclose(tep);
set(handles.text1, 'String','Serial port is closed');%updating the text on the GUI
- At the end you must need to close the serial port.
- As you press the Stop Serial button the serial port will be closed.
- The GUI will update its text as Serial port is closed.
- The screenshot of the updated GUI for Stepper Motor Speed Control in Matlab is shown in below figure.
That's all from the tutorial
Stepper Motor Speed Control in Matlab. I hope you enjoyed this tutorial. If you face any sort of problem anytime, you can ask me freely without even feeling any kind of hesitation. I will try my level best to solve your problem in a better way, if possible. I will explore Matlab by making different projects and I will share them with you as well in my later tutorials. Till then, take care :)