From the course: MATLAB 2018 Essential Training

Include if and if-else statements in a script - MATLAB Tutorial

From the course: MATLAB 2018 Essential Training

Start my 1-month free trial

Include if and if-else statements in a script

- [Instructor] When you perform an experiment, you can never be sure what sort of result you'll receive. That means your MATLAB scripts will need to have some flexibility in handling your data. In this movie, I will show you how to use if, and if else statements to handle different cases. I'm working in a blank command window, but what I really need is a code window or a script. So I'll go to the home tab at the user interface, and click the new script button. I can also press control + n. In the code editor, I need to define the function that will calculate the value that I want based on if then statements. So I'll start with a function name, so I'll call it function and then ct. That will be the output variable, equal and that will be cooltime, C-O-O-L-T-I-M-E, and I won't pass at any arguments. I'll just wait for input from within the function. So I will type in open and close parentheses with nothing between them, and press enter. So the idea is that I want to evaluate the amount of time it took glass, heated to a certain temperature, to cool by 20 degrees. I will assign that value to a variable called val one, so I'll press space twice to give myself a little differentiation between the levels in my code. Val one equal, and it will be equal to the input with the prompt time in seconds for the glass to cool 20 degrees, followed by a colon, and a new line character. Then I will close the string and then close the input argument, and everything looks good. My string is in magenta like it should be. Press enter and I'm on the next line. Now I will just check for one condition, and that is if the value is greater than 25 seconds. So I'll start with if, a space, val one, which again is the time it took to cool, is greater than 25, then I'll press enter. Note that MATLAB is providing some indentation for me. If the value is greater than 25, I want the script to display a message that the time is above the projected range. So I will type disp, which is for display, left parenthesis, then a single quote, and the prompt will be That time is above the projected range. Follow that with a period, a single quote, and a right parenthesis to close out, and enter. Now I can end the if statement by typing end and MATLAB knows which statement it corresponds to, so it lines it up. Press enter. Type end again. That goes with function. I'll press enter one more time, and now I can save, so I'll press control + s to save, and I see that the name of the file Be Cool Time, which will be the name of the function. I'll leave it there. Click save, and it appears on the left in my current folder panel. Now my function is ready to go, and I can close the editor window, and I'm back in my command window. Now to run the function I just created, all I need to do is type its name, so I'll type cooltime. Again, I don't need to pass in any arguments, so I'll press enter, and I get my prompt. So my test is for 25, so I'll type in 26 and enter, and it says that the time is above the projected range. Great, that worked well. Let's try it again, cooltime, enter, and now let's say 19, and it just returns the value. So there are a couple of things that I need to fix. The first is that I see the value, and second I don't get any sort of indication if a value is correct, or if it's too low. To make those changes, we need to go back into the cooltime file, and edit the text. So I'll go over to the current folder, and double click cooltime.m. All right, I've got my edit window back. The first thing I will do is to remove the duplication of val one. In other words, I don't want it displayed, so I'll go to the end of the first line, the input line, and type a semicolon. That suppresses the output. Now I can add other elements, other tests using else statements. I'm going to leave val one greater than 25 saying that the time is above the projected range, the same. Now I will click after that displays, right parenthesis and enter to get a new line, and I will type the else keyword, E-L-S-E, and you can see that MATLAB is working my spacing for me. So let's check to see if the value is greater than 10. So I'll say if val one is greater than 10 and enter, then disp for display, left parenthesis, single quote, that time is within the expected range, followed by a single quote, and a right parenthesis. I have one more case to account for, so if the value isn't greater than 25, but it is greater than 10, that means it's in the expected range, but we need to know what to do if the value is less than 10. So I'll type in another else, then enter, and that will be display, left parenthesis, single quote, that time is below the expected range, followed by a single quote, and a right parenthesis. So again, what we're doing is we're checking for the values greater than 25. If that's the case, than it's above the projected range. If it's less than 25 but above 10, it's within the expected range, and every other value which is smaller than 10 is below the expected range. So I've got my elses altogether, and I can type end, which goes with that if, and let me see, I have one, two, three. Let's see, what I'm doing is I'm looking for mismatched or unterminated ifs, and it looks like everything is correct, so I'll press control + s to save, and we'll find out soon enough if I didn't match everything up, so I will click the close editor button, and type cooltime again, enter, time and seconds. Now let's go with 26 and enter. It's above the projected range, cooltime again, between 19. The time is within the expected range. Cooltime one more time, and this time the time in seconds will be five, and we see that the time is below the expected range. There are couple of things to note about these scripts. The first is that I didn't do any error checking, so I didn't make sure that the values coming in were in fact numbers. And also, if you're checking if one number or value equals another, remember to use two equal signs. That's how you check for equality in MATLAB. A single equal sign assigns one value to a variable.

Contents