6p. First script _printable_

6p. First script _printable_ - Creating Matlab Scripts...

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

View Full Document Right Arrow Icon
1 ©2009 by L. Lagerstrom Creating Matlab Scripts • Creating, saving, and running script files • Comments on comments • A simple script • Example: Finding quadratic roots • Using the disp function • Quadratic roots script, take 2 • The input function • Quadratic roots script, take 3 ©2009 by L. Lagerstrom Script Files So far we have used the Matlab Command Window to enter examples of Matlab code using variables, assignment statements, functions, etc. This is okay if we're just doing a line or two, but it's not very convenient for more involved calculations. Matlab allows us to save code sequences (or "scripts") in so- called "script" files. A script file is simply some Matlab instructions stored in a text-only file and saved with a ".m" extension. If we then enter the name of the file at the prompt in the Matlab Command Window, Matlab will retrieve the file and execute all the instructions found in the file, from the beginning to the end. Results will displayed in the Command Window. ©2009 by L. Lagerstrom Creating Script Files A script file may be created using any text editing software. (Full- fledged word processing software such as Microsoft Word is not recommended for use.) Fortunately, the Matlab software has a built-in text editor that comes with many useful features. To use it, select the File menu at the top of the Command Window, then "New" and "M-File." A new window labeled "Editor" will open. Matlab instructions may be typed here, line by line. ©2009 by L. Lagerstrom Saving Script Files Once you have typed the Matlab instructions using the Editor (we will do an example in a minute), you save the file as usual by selecting "Save As" from the File menu and entering a name for the file. It will be saved with a .m extension. Naming rules: Do NOT use spaces in the file name, nor any special characters except the underscore (_) character. (Letters and numbers are okay.) VERY IMPORTANT: Make sure that you save the file in the "Work" folder that is inside the MATLAB folder. If you don't save it there, Matlab may not be able to find it when it's time to execute the code in the file. (We'll say more about this later.)
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 ©2009 by L. Lagerstrom Running Script Files Once the script file is saved we can run it (i.e., execute the instructions in it) by going to the Command Window and entering the name of the script file, but without the .m extension. So if the name of our file is firstscript.m, then we enter: >>firstscript Assuming that the instructions in firstscript.m are error-free, we would then see the results displayed in the Command Window. (If there were one or more errors, we would see an error message for the first error found.) ©2009 by L. Lagerstrom A Word re. Script Presentation
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 02/23/2010 for the course ENG 42325 taught by Professor Lagerstrom during the Spring '10 term at UC Davis.

Page1 / 5

6p. First script _printable_ - Creating 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