In this video, learn how to accept one or more input values for your MATLAB calculations.
- [Instructor] In the previous movie I showed you how to create a script to record a MATLAB procedure. The main limitation of that script, and it's an important one, was that every value was written directly into the code. There was no way to accept input. In this movie I will show you how to accept one or more input values for your MATLAB calculations. I have run MATLAB and I'm looking at a new blank command window. I want to create a script so I will go to the home tab and click the new script button.
I could also press control n. And doing so displays a new editor window. I've been working for a while so mine is called Untitled4. Yours might have a different number. I want my script to be able to accept input from a user. So, I'll start by assigning a value to a variable. I'll call that radius. And my goal is to develop an equation that calculates the volume of a sphere. Rather than specifying the value directly, I want to accept input, and as you might imagine that uses the input keyword.
So I'll type input followed by a left parentheses then a single quote because I'm providing a prompt which is a string, and I will ask Enter the radius of the sphere. A stylistic note is that it's considered good practice to add a colon after the input prompt. That way the reader knows that you are in fact asking for their input because the colon indicates that it's not quite a full stop, but it's expecting something more afterwards.
Right, then type a single quote, and then a right parentheses, and there you have your input. And the red underlining indicating an error has gone away so everything looks good. Then enter and then the volume should be set equal to four times pi which is a built-in constant, multiplied by the radius cubed, so that would be a caret and a three divided by three.
So there is my formula. Right, everything looks good, so I will press control s to save, and I will call it inputvolume just to differentiate it from the sphere volume script that we created elsewhere in the course. Press enter and there I have my file. I'll close it by going over to the editor window and click the close editor button. And then in the command window I will run my script by typing in the name of the file without the .m file extension.
So that is inputvolume and enter. My input asks me to enter the radius of the sphere. That's the prompt I created. So I'll type in three and enter. The radius equals three and volume equals 113.0973. You can also accept multiple inputs, basically through multiple input statements. I'll create a new script so I will press control n. So I get a new editor window and my command window moves down.
And for this I will create a calculation to calculate the area of a triangle, which is one half of the base times the height. So I'll press control s to save before I start and I'll call it try area and enter. So I'll have the base equals = input('Enter the base of the triangle: ') and everything looks good.
Enter. Then the height so that'll be height = input('Enter the height of the triangle: ') and I'm getting an indication up here, and I bet the program is wondering if I want to actually display the base that was input. So if I hover my mouse pointer over the one for height it says terminate statement with semicolon to suppress output.
Yeah, okay, so I'll put a semicolon after height and a semicolon after the base line, then I'll press enter. And now I will type t_area to indicate triangle area. And that is equal to one half, or 0.5 times the base times the height and enter. And I definitely want to display that value. So I'll press control s. Everything looks good. I'll close the triarea file by clicking the close editor button.
And then in my command window I will type the name of the file minus the .m extension. So triarea enter. Enter the base of the triangle. Let's do 10 enter. And the height will be six. So if we're lucky the t_area will be 30, one half the base times the height. And there is our result. Accepting input gives you a lot more flexibility when you are working in MATLAB. It also avoids the common programming error of hardcoding values that really should be put in by the user every time they do the calculation.
- 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