Introduction: How to Write a User-defined Function in MATLAB
This MATLAB instruction is designed to help general engineering students write a user-defined function.
- Computer with MATLAB programming software.
- Beginner level skills in MATLAB.
When you use a mathematical function f(x) many times for different variables in a MATLAB program, a user-defined function is beneficial. A user-defined function is a separate file which is usable in any MATLAB program. A function file is a .m file, but different from a script file. Scripts are the simplest type of program since they store commands exactly as you would type them at the command line. However, .m functions are more flexible and more easily extensible. Instead of manually updating the script each time, you can make your program more flexible by changing it to a .m function.
Step 1: How to Open a Function File
- Open the MATLAB software on your computer.
- Once you open MATLAB, open a new script file by double clicking the “new script” icon on the top left of the MATLAB file, as in the picture above.
Step 2: Get to Know the MATLAB Interface
Once you've opened a new script file, you should see the above interface. The interface includes four different windows.
- Command Window: Contains the command prompt (<<). This is the main window where you will type all commands.
- Command History: Displays a list of previously typed commands.
- Workspace: Lists all the variables you have generated in the current session. It shows the type and size of variables.
- Current Directory: Shows the files and folders in the current directory.
Step 3: Writing Your Function in a Script File
- Once you understand the interface, you can start writing any function that you want. Refer the first image above for the rules on how to write functions.
- Write the function using the instructions in the second image above. The function must be able to find the area and perimeter of any given circle with a radius. The declaration includes the function keyword, the names of the input and output arguments, and the name of the function.
Step 4: How to Save and Run the User-defined Functions.
- After you finish writing your function, you must save the function in the name of your function. As you see the first image above I called my function circle.
- Make sure there are no errors by checking if there is a square green light on the right of your function screen. If the color is red, then you must review your function. As you see the second image above
- Once you check, do a sample run. As an example, I choose my radius to be 10. In the command window, you can give any names for the output but you can’t change the function name.
Step 5: Congratulation
Congratulation! you successfully created your first user-defined MATLAB function.
Once your code is outputting correctly, you can change the values anytime on the command window. Now you can create any function on .m file. Practice! Practice! The more you practice, the more you get comfortable with the software. If you need more help, you can get on MATLAB help section. Type anything you want on MATLAB help section, you can get more examples.