{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Matlab lesson 3

Matlab lesson 3 - Lesson 3 Programming in Matlab Scripts...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 3: Programming in Matlab - Scripts and For Loops Goals of this lesson: In this lesson you will learn how to: change folders in MATLAB so that you can save files in an appropriate place and organize your work, write m-file scripts, and write for loops. You will also further develop your familiarity with MATLAB and start getting a feel for how you can get MATLAB to do things that you might not at first think that you could do. Changing Folders in MATLAB In this lesson you will start programming with MATLAB. This means that you won’t just be using MATLAB interactively, but will be creating files that need to be saved. Most of you probably don’t have MATLAB on your own personal computer, so when you are using MATLAB you’ll be working on a public machine. Rather than save your files on the hard drive of a machine that is not your own, we strongly suggest that you purchase a little flash drive (if you don’t already have one) that will plug into a USB port on the machine you are working on. You can then save your files on the flash drive and you will always have them with you. To get MATLAB to save files to your flash drive, first put the flash drive in the USB port and create a folder in it called 125 and a folder inside that called Lesson3. Then open up MATLAB. In the top right hand corner of the command window you will see a little square with 3 dots in it. Click on that square and a browser window will open up. In the browser window, navigate to your flash drive and then to the folder in it called 125 and then to the folder in that called Lesson3. Select this folder. You should see its path name written out in the top of the command window. This is now your working folder in MATLAB. Any file that you create in MATLAB will be saved here, and you will also be able to access any of the files that are here. Writing an M-file Script Let’s see how we can get MATLAB to draw the smiley shown below. Notice that the smiley is essentially one circle for the face, a circle for each eye and an arc of a circle for the mouth. To plot all of these circles and the arc on the same plot we’ll use the hold on command. However, if we make a mistake, say, in plotting the third circle, we’ll have to go back and start all over with plotting the first circle again. This could involve a lot of typing and be frustrating. For this reason, it’s useful to be able to edit a whole sequence of commands without executing them. You can do this by creating what is known as an m-file script . An m-file script is 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 simply a sequence of commands that are saved in a file. (The file is saved with a .m extension. That is why it’s called an m-file.) When you type the name of the file at the prompt in the command window, MATLAB looks for a file with that name and, if it finds one, executes the commands in sequence. So to get MATLAB to draw the smiley above, we’ll create an m-file script that does this.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}