MATLAB is the product of Mathworks, Inc., “it is language of technical computing” producers of MATLAB claims this. To me, it is mother of all the technical tools. You can do almost every type of simulation using its tools, no matter to which field you are connected with.
It is popular especially among engineers and technical experts, and they use it on daily basis for solving simple to complex problems. Another reason of popularity is that it uses arrays in all of its operation. In fact, MATLAB is acronym for ‘Matrix Laboratory’.
One can use it as a simple calculator to heights of image processing and signal processing complex simulations. It has its own programming language which is considered as one of the 5 largest programming languages around the world. Following are few steps for getting started and become familiar with the MATLAB working environment and how to tackle problems.
MATLAB Basic Layout:
Here, we are going to use MATLAB 2014a. Layout of other versions are almost same. (Students can get a student copy of MATLAB free from the Mathworks official site. (With limited of the tools of course but more than enough for a student to use).
In Figure 1 there is default layout of the MATLAB with three windows in it. The blueone named as current folder orange is workspace and green is command window. On the top left side there are three tabs naming home, plots and Apps. The home tab covers all the menu bars in previous versions of MATLAB. We will see them individually later on. First let’s see the purpose of these three window visible at the very first time;
Current folder: this window shows the directory of the MATLAB in which you are working.
Workspace: all the variables in MATLAB is saved as new matrix and at the time you define a new variable and assign it a new value it will save as matrix and visible to you at this window and remain there assigned unless you assign that variable another value or you clear the memory of MATLAB.
Command window: All the commands to the MATLAB is given through this window. It is work place in MATLAB. It is also the output display window of the MATLAB. You gives/writes the commands in this window and the results/output after calculation is also shown by this window.
We will take a quick look to the main tab (Home). And the options it gives to the user.
From left: Further division of Home is as follow:
File, Variables, Code, Simulink, Evironment and Resources
1. The first sub-section is of FILE
It is further divided into following options:
i. New script: is the first option in file which is to create a new .m file (dot m file). m file is the file for programming in MATLAB. You can open it using ctrl+N command in windows operating system. It is same as working in command window but the code is saved in a file and can be called anywhere. The basic structure of the coding/programming in it is same as using the basic loops, array, conditional statements and structures like simple c programming. (With its own calling/defining methods and keywords). as shown in figure 2 (encircled red)
ii. Second option is New which is a drop down menu and allows to create a new file of script, new graph plotting file, new function (calling or defining a function in programming), new Simulink model or Simulink project and many more.
iii. Third one is to open the already existing project or file to work on it again or whatever purpose.
iv. Next two are finding files and compare the contents of two files.
2. The second subgroup of HOME tab is Variables. It allows to handle variables and make easy to work with them. It has options of defining new variables, assigning values to them, import variables from other files and saving the current working variables into a file for future working. Figure 2 (blue circle)
3. Third tab is for coding/programming having the name of code. It allows the user to handles the commands, run the code and analyze it. Figure 4 (number 1-yellow box)
4. Forth tab is Simulink tab which provides a number of tools to run and simulate the projects and problems in very easy way. It is very vast and like an ocean of tools to work on. This will be discussed in coming sections in detail. Figure 4 (number 2-Green box)
5. Second last is subgroup of environment which gives option of adjusting the working environment and working path. You can change the basic layout of your own desire by clicking the layout drop-down menu. Figure 4 (number 3)
6. Last subgroup is of community and gives the user help about using different tools and access to the Mathworks worldwide community to get help in any problem. Figure 4(number 4)
This was a little intro about the menu bars. Now we will see other windows in basic/default layout.
Current Folder Window:
This is window showing the folder in your computer in which you are working and all your files are going to save. If you are working on a specific project and want a separate folder to contain all the files of that project you will work in that folder as you current folder or working directory. You can see this folder destination in the top directory bar. Just like shown in figure 5 (below) the current folder is Tutorial_1 in folder MATLAB in C drive.
Another method to see the current working folder just type pwd (short of present working directory) in command window and it will show you folder in which you are working and all your work is saving. The figure 6 shows that.
Change current working folder/working directory:
To change the working folder or working directory, there are also methods. On the left side in current folder window, there are all the folders in your computers, by simple clicking and expanding the folders reach your destination folder and click it. When you double click the folder, it will become the current working folder. Following figure 7 will show you in 5 simple steps: (Click on image for larger view)
In other mehtod, find the path of the folder you want to work in just by open it in file explorer and copy the path of it and go to the command window and write cd (short for change directory) and put single inverted comma and paste that path in it and close the single inverted comma and press enter. Now the working directory is your desired folder. To check the working directory, write pwd in command window or just look at the path in directory bar. This procedure is shown in flow chat in following figure 8.
This allows the user to work with MATLAB. Command window writing place looks like >>. The most important command in MATLAB is help command. It can give you help about any of the tool and the command in MATLAB.
When you write help in command window and press enter, it will give you a list of all the documents it contains about any topic. For specific help enter command/keyword after help in command window.
For example if you want to know all about plot command write help plot and press enter. It will give you complete details about this command. (Figure 9). Likewise, you can get help of any other command.
Use of Variables in Command Window
To define variables and assigning them values just write the variable name and put equal sign and write the value you want to assign that variable and press enter. It will store as an array and visible in the workspace window (bottom left corner). For example;
It can be seen that a variable is declared and given the value of 23. When you press enter, the variable will be visible at command window showing its value. If you don’t want to show the value put a semi colon (;) at the end. It will not show the value but it is assigned to that variable. (Figure 10)
From above it can be seen the use of semi colon at the end of variable declaration. Also in bottom left corner it is visible the variables which are defined. The previously defined variables are also present there. They will hold the assigned value until next value is assigned to the same variable or the memory is cleared. To clear the memory write the keyword clear all. It will clear whole cache memory of MATLAB and all the defined variables will be lost.
Clc command will clear the window but not the values of variable defined. (as shown in figure 11 below)
Basic Arithmetic Operations in MATLAB:
To make familiar yourself with MATLAB command window let do a little mathematical operations. Following example shows the variable a and b. both have values assigned to them. The multiplication of a and b result in another variable ans. The variable ‘ans’ is by default assigned to the answer when no variable is given to the output result. If you want result to assign some new variable put variable before multiplication. (Figure 12)