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
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
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:
As seen in the third picture, ensures everything is cleared and closed.
Step 3: Creating a Time Vector
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
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.
Title('Y Value vs Time')
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
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
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.