MATLAB can implement common loops such as while loops.
- [Instructor] Another handy loop to have in our toolbox is the while loop. So let's go ahead and set one of those up. Let's stay in our let's loop file here but just comment out our for loop code. So I'll right click and say comment. Give myself some more space here and let's do a comment here. We'll call it while loop. While loops will continue to run while a certain flag is true and as soon as that flag goes false, the while loop will stop. So let's set up our while loop this way. I'll create a value.
Let's set that to something like 50. We'll just subtract something from this until it gets below zero. So we'll say, N is equal to five. We'll subtract that from value in our loop. So then we'll say, while. And let's say while value is greater than zero, we'll do something simple like say that value is now equal to itself minus N. Let's display on screen value.
And we'll end our loop. So while loops, you always need to be careful to make sure that you're not going to get yourself into an infinite loop situation. Here we should be okay. We get to zero pretty quick. Let's run it and see how it goes. Perfect. So we see are value starts out being subtracted by five and gets down to zero and then our loop exits. Let's go ahead and clear the workspace and our screen. While loops are also useful for testing threshold values which can be kind of fun.
So let's check out an example doing that. Let's set some threshold. Maybe you know this is a voltage that we're measuring off of an ADC or something like that. I'll just set that to eight. And then in our while loop here, let's go ahead and clear all that stuff out, let's just set our while loop equal to true. Meaning it's going to forever until we make some situation where we break out of the while loop. So while true, we're going to create a number. So maybe this is the thing that we're measuring. But for this example, I'll just set it to a random integer with the randi function.
This randi function will give me random integers anywhere between one and whatever value I tell it here. So we'll get values between one and 10. And then let's check. We'll say, if our random number that we get here is greater than our threshold, then we want to tell our user that something bad has happened. So we'll say... over the limit... shut down. And then we use the keyword in here, break.
This will exit the loop that we're currently in. So if the number is below the threshold, let's just tell the user that everything is going alright. So we'll display on screen, everything is okay. Alright, let's save that. Give ourselves a little more real estate here in the command window and let's run our loop. We got really unlucky on our first try. We were over our limit immediately. Let's try running it again. There we go, everything is okay for a little while, but then we shut down. So while loops can be really handy for these kind of checks.
Just make sure you always break out of your loop and you don't get yourself into an infinite loop situation.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models