Matlab Basics

This instructable will cover some of the most basic functions of matlab. You will learn how to make matlab run a periodic function internally and plot and how to pull the same periodic function from an excel file instead and plot it.

These functions are some of the most basic and widely used in matlab.  This instructable is geared towards those of you who have never used matlab before and just need to perform some simple tasks with it.  The code highlighted in each picture is included as a comment so you can copy and paste the code.  Feel free to take this code and modify it to fit your application.

Step 1: Starting Matlab

The first step is to get matlab up and running so we can begin to work with it.  When you first start matlab it should look like the screenshot below.  The first step is to assign a directory for matlab to work from.  This is where the program will pull all the files from and is where you should save all your matlab work.

I recommend making a new folder somewhere you will remember it, and naming it something you will recognize.  Once you have created a new folder, click the "..." located in the top right of the screen as highlighted in the second picture.  This will pop up a browse box as seen in the third picture.  Find the new folder you created on your computer and select it.  For this example the file is called "370" and is located on the desktop.

Step 2: Creating an M-File

Now what we need to do is create a new M file.  An M file functions exactly like typing code directly into matlab, but you can save and modify the code, and run it repeatedly.  When entering code directly into matlab you type each line of code individually.  In an M file you write your whole code then run it at once.

To open a new M file click on file.  Place your cursor on "New" then click on "Blank M File" as shown in the first picture.  What opens should look like the second picture.

Since this code can be run repeatedly, it is a good idea to close everything and clear all the variables before it is run each time.  This is accomplished through two lines of code:

close all
clear all

As seen in the third picture, ensures everything is cleared and closed.

Step 3: Creating a Time Vector

The first thing we will do is create a graph of a function in matlab.  The first step is to create the independant variable.  In this case we will call it "t" for time.  The method we will use to create this variable is to make a vector.  A vector is basically a series of numbers.  For example, 1,2,3,4 would be a short vector.  The code to create this vector is:


The first number, 0.1 refers to the start point.  The second number, the 0.01 refers to the step size.  The third number, 10, refers to the end point.  So this vector corresponds to 0.1 , 0.11 , 0.12 ... all the way up to 10.

To see if creating the vector worked, click the green run button highlighted in the second picture.  This runs the program.  To see our vector go to the main matlab window.  Click desktop, then mouse over desktop layout, and then click default as outlined in the third picture.  Now your screen should look like the fourth picture.  On the right you will see our newly created variable, t.  Double click on it and like in the fifth picture you will see the series of numbers created.

Step 4: Running and Graphing a Function

Now we will graph a function created in matlab.  The first step is to create the function.  This is as simple as writing out the desired math function.  An example is shown in the first picture.  The code used for this function is:


The period before the multiplication in the cosine, and before the square of the cosine tell matlab to perform those functions simply on the valuables of the time vector, not to treat the time vector as a matrix and try to do matrix functions on it.

The next step is to create the figure itself.  This is accomplished using the code shown in the second figure.  The order of the variables in the plot command is very important so be sure to set up your code just like it is set up below.

xlabel('Time (s)')
ylabel('Y Value')
Title('Y Value vs Time')
grid on

Finally, just click the green run arrow again and the figure should pop up like in the the third picture.

Step 5: Pulling Data From Excel

We will now create the same graph as before, but by importing the function data from an excel spreadsheet.  The first picture is a screenshot of the excel spreadsheet that will be used.  It is the exact same data points created in matlab in the previous steps, just made in excel.

To start we can delete the code creating our time vector and the code for our function from the previous steps.  Your code should now look like the second picture.

Insert the code as shown in the top red box of the third picture.  This is the code to read the excel file.  "A" refers to a matrix that will include all the numbers in the spreadsheet, and "B" includes all of the text from the spreadsheet.  The t and y variables are pulled from the first and second collumn as shown in the code.


The figure code can also be modified as shown in the lower red box on the third picture.  This will actually pull the chart title and axis labels from the spreadsheet and put them on your graph.


The final thing to do is run the program again and you will see the same figure pops up as seen in the final picture.

Step 6: Creating a Specgram

In this step we will use matlab to create a specgram by reading a wav sound file.  A specgram is sometimes called a "2.5D graph," because it uses a two dimensional graph, with the addition of color to show amplitude.  The color provides more detail then a simple 2D graph, but not the detail of a 3D graph, hence the term "2.5D."

The specgram function of matlab takes a set of data points from the wav file and performs a Fourier Transform on the points to determine the frequencies present in the signal.  For this instructable, it is not important to know how a Fourier Transform works, just know that the specgram will plot which frequencies are present, and how strong they are with respect to time.  The function plots time on the X-axis and frequency on the Y axis.  The strength of each frequency is displayed by color.

In this case the wav file is a sound recording of a piece of metal being struck, and then the vibrations of the metal are recorded as sound.  Using the specgram, we can easily determine the resonant frequency of the piece of metal, because that will be the frequency that persists longest with time.

To perform this task, first have matlab read the wav file by using the following code:


In this case, flex4.wav is the title of our wav file, the variable x is the data points in the file, and fs refers to the sampling frequency.

To perform the specgram, just type the following code:


The 256 corresponds to the frequency that the FFT is performed at when analyzing the data.  Matlab is basically chopping the sound file into chunks and taking an FFT on each chunk   The 256 tells it how big each chunk should be.  The details of this are not important, and 256 is a safe value to use for most applications.

Now if you run the code, you will see a figure pop up as seen in the second picture.  From this it is easy to see that the resonant frequency corresponds to the red peak in the bottom right hand corner of the figure.  This is the peak that persists longest with respect to time.



    • Pocket Sized Contest

      Pocket Sized Contest
    • Remix Contest

      Remix Contest
    • Organization Contest

      Organization Contest