In this video, learn how to create user-defined functions to which you can pass values as arguments.
- [Instructor] So far in this course, I have shown you how to work in MATLAB using the command window. In this movie, I'm going to show you how to save your commands as a script, that way you can run functions and other procedures whenever you like. I have a blank command window in front of me, but what I really need is a new script, a code window. So for that I will go to the home tab of the user interface, and click new script.
Doing so displays the editor window, and from here I can create a function. It starts out as you might imagine with the function keyword, followed by a space, and then the name of the output variable. And this will be the variable that is calculated within the body of the function. In this case, I will call it crcmf, which is short for circumference. Then a space, then an equal sign.
Now I need to type in the name of the function that will be used inside of MATLAB. That will be circumf another abbreviation for circumference, left parenthesis, and I need to pass it the radius of the circle. So I'll type radius, followed by a right parenthesis, and then enter. Now I'm in the body of the function itself, so I'll press space twice, and this just offsets the lines so I know what level I'm on.
Now I need to calculate the circumference itself and the output variable, crcmf, which remember I defined right after the function keyword. Then space and equal sign, and the calculation is two times pi, which is a keyword, times the radius. Press enter, and then backspace twice, so you can back up the same level as function. Type end, and then press enter. So there I have my calculation.
It's going to accept the radius and calculate the circumference, and I call it from inside of MATLAB using the circumf keyword. Now I can save it, so I'll just press control s, and that takes me to the current folder, and I have circumf, and then dot m. I'll click save, and there I have it. Now I can close my editor window, just by clicking the close editor button. Now I'm back in my command window, and I can try out the function.
So the function name, which I left as the name of the file, and you can see over in the current folder on the left, is circumf followed by left parenthesis, and the radius I'll say is three. So I'll put that in parenthesis and enter, and I get the circumference. You can also accept multiple arguments. So I will create a new script for this new function, and I will call this one, using the function keyword again, tra, will be the output variable, equals, and this will be the area of a triangle, so I will type triarea, left parenthesis, and then base comma height, so just the two arguments.
Enter, and then two spaces, and I'll make the calculation tra equals 0.5 times base times height. Everything appears to be spelled correctly, press enter, backspace twice to line up, and end, enter. And I'll now press control s to save. I'm back in the current folder. Won't change the name, save, and there I have it. Now I can close the editor window, and back in the command window, I can try out the function.
So I'll type triarea, followed by left parenthesis, and we'll make the base six and the height ten. So one half of six times ten should be 30, and when I press enter, that's what I get. User defined functions are one of the most powerful ways that you can customize MATLAB. As long as you get the specifics right in terms of variable names and how you call them, the rest is smooth sailing.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions