Creating a Function in MATLAB
by JordanStolle in Circuits > Computers
46486 Views, 7 Favorites, 0 Comments
Creating a Function in MATLAB
MATLAB is a tool that engineers and other professionals can use to quickly and efficiently analyze data, make calculations, and display information. One of the many ways that the user can interact with MATLAB is through the use of functions. Functions receive specific information, known as inputs, and use the inputs to calculate other information to display, known as outputs. This instructable will detail the process of creating a function and will guide the user through creating a function of their own.
To complete this instructable, you will need:
1. A computer with access to MATLAB
2. 5-10 minutes
Open MATLAB
First, open the start menu by pressing the Windows button in the bottom left corner of the screen, or by pressing the Windows key on your keyboard. Then, type MATLAB into the search bar and select "MATLAB R2013a." Note that the exact name of the program may vary slightly if your version of MATLAB is older. You should still see the same symbol as shown in the image even if your version is older, so you should use this to find the right program.
Open Script Window
When MATLAB opens, the user is taken to MATLAB's primary window, also known as the Command Window. In order to create a function, however, the user must open the Script Window. Select the highlighted button to open the Script Window. Scripts are saveable sequences of commands that can be run all at once. A function is a special kind of script that can take different inputs each time it is run. Note that in older versions of MATLAB, the highlighted button will not appear, and the user must select File > New > Script in order to open the Script Window.
The First Line
Once the Script Window opens, type "function f = make_a_square(x)" into line 1. The word "function" tells MATLAB that this script will be a function. The text between the word "function" and the = signifies the output of the function, in this case being "f". The text inside of the parentheses signifies the input, in this case being "x." Finally, the text directly to the left of the parentheses is the name of the function, which in this case is called "make_a_square."
Finishing the Function
Once the first line of the function is entered, complete the code by entering "f=x^2;" on line 2 and "end" on line 3. The second line tells MATLAB to take the input, square it, and set the new number as the output. The semicolon ending the line stops MATLAB from outputting that line later. Were the semicolon not used, the output would display twice when we used the function later on. Line 3 tells MATLAB that the function has concluded.
Saving the Function
Once your function is complete, save the function using the save button. The default name of the save file will be the same as the name of the function. Make sure that you do not change this. Changing the name of the save so that it is not the same as the name on line 1 can cause the function to not work properly. Note that, as in Step 2, older versions of MATLAB will not have the highlighted button, so saving must also be accomplished through the File menu.
Running the Function
Return to the Command Window. Enter "make_a_square(3)" and hit Enter on your keyboard to test your function. Your code should square the number 3, outputting 9. If the code does not do this, check to make sure that you spelled the function correctly in the Command Window. If this does not solve the problem, reopen your function in the Script Window and ensure that there are no errors. Once you code is outputting correctly, you will have successfully created your first MATLAB function!