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 Fles in an appropriate place and organize your work, write m-Fle 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 Frst 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 Fles 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 Fles on the hard drive of a machine that is not your own, we strongly suggest that you purchase a little ±ash 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 Fles on the ±ash drive and you will always have them with you. To get MATLAB to save Fles to your ±ash drive, Frst put the ±ash 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 ±ash 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 Fle that you create in MATLAB will be saved here, and you will also be able to access any of the Fles that are here. Writing an M-fle 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 Frst circle again. This could involve a lot of typing and be frustrating. ²or 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-±le script . An m-Fle script is 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 simply a sequence of commands that are saved in a Fle. (The Fle is saved with a .m extension. That is why it’s called an m-Fle.) When you type the name of the Fle at the prompt in the command window, MATLAB looks for a Fle with that name and, if it Fnds one, executes the commands in sequence. So to get MATLAB to draw the smiley above, we’ll create an m-Fle 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.

This note was uploaded on 09/21/2010 for the course BISC 13004 at USC.

Page1 / 13

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online