Mat Lab 0100

Embed Size (px)

Citation preview

  • 7/31/2019 Mat Lab 0100

    1/45

    1

    MATLAB Introduction

    ContentsMATLAB Introduction .................................................................................................................................. 1MATLAB ....................................................................................................................................................... 2Documentation ............................................................................................................................................... 2Obtaining MATLAB ...................................................................................................................................... 3Setting up for Work ........................................................................................................................................ 4The Main IDE and Current Folder ................................................................................................................. 4Accessing A Working Directory .................................................................................................................... 5The Command Window ................................................................................................................................. 6Creating an *.m file for a Standard Work Session ......................................................................................... 7Alternative ...................................................................................................................................................... 9Adjusting Font Size .......................................................................................................................................10Closing and Opening Files in the Editor Window .........................................................................................10Add a Program to the File .............................................................................................................................11Retrieving Files .............................................................................................................................................12Saving Figure Images ....................................................................................................................................13Viewing the PNG File ...................................................................................................................................14About *.asv Files ...........................................................................................................................................14More on Reopening a MATLAB Program File .............................................................................................15A Second Iteration .........................................................................................................................................16Programs and Files in the Current Folder Panel ............................................................................................17Command Window Activities .......................................................................................................................17Command window Clearing ..........................................................................................................................19Command History .........................................................................................................................................19History Options .............................................................................................................................................20Command History Uses .................................................................................................................................21Command History and Command Prompt ....................................................................................................22Arrow Keys and the Command List ..............................................................................................................23Selective Deletion ..........................................................................................................................................23Using History for Script Files .......... .......... ........... .......... ........... .......... ........... .......... ........... .......... ........... .....24The Workspace, Briefly .................................................................................................................................26External Resources ........................................................................................................................................26Using Externally Developed M Files ............................................................................................................27Saving Figure Files ........................................................................................................................................29Running without Opening .............................................................................................................................30Viewing Multiple Code Files in the Editor ........... .......... ........... .......... ........... .......... ........... .......... ........... .....30More on Saving Files.....................................................................................................................................34Publishing to HTML and Word .....................................................................................................................35

    Creating an HTML File .............................................................................................................................35Generating a PDF File ...............................................................................................................................39

    Setting Paths ..................................................................................................................................................40Notes ..............................................................................................................................................................42Cleaning Up Your Session ............................................................................................................................43Notes ..............................................................................................................................................................44How MATLAB Responds to Absent Paths ...................................................................................................44

  • 7/31/2019 Mat Lab 0100

    2/45

    2

    MATLAB

    The name MATLAB is derived from matrix laboratory. MATLAB is an applicationthat allows you to create mathematically oriented programs. It is generally recommended

    that you can get the most out of it if you have familiarity with linear algebra. However, as

    it is, MATLAB can be used in many ways, an almost any context, ranging from the most

    elementary forms of math to projects pursued at a doctoral level. It is a calculator, aplotter, and a programming tool. It has many modules that can be added to it that address

    different specializations, such as biology, physics, and electrical engineering, among

    many others. At CU, MATLAB is used in Calculus III labs and other upper division

    courses, such as Differential Equations and Linear Algebra.

    The programming language used with MATLAB is usually referred to as MATLAB

    script or M-script. After becoming familiar with the basic syntax of the M-script, a

    number of useful utilities are available to you that allow you to make extended uses ofMATLAB. You can, for example, write programs that involve simulation. You can alsocreate graphics, web pages, and GUI applications. When you develop programs using

    MATLAB, you can output the results to a number of media, including graphics files,HTML pages, PDF files, and Word documents. You can also connect up MATLAB withother applications, such as Excel or LabView to make extended uses of it. Since it is

    programmed in part using Java, you can modify it in the background using Java.

    MATLAB was first developed in the 1970s by Cleve Moler, who was on the faculty of

    the University of New Mexico. It was then extended through partnerships Moler formed

    with others. During the middle 1980s, MathWorks was formed, and since that time,

    MATLAB has been continuously extended to accommodate more users. Currently, over amillion people in industry and academia are users of MATLAB.

    DocumentationThe primary site for information on MATLAB is maintained by MathWorks, thecompany that manufactures MATLAB. Bookmark this URL so that you have ready

    reference to it as you go:

    http://www.mathworks.com/help/techdoc/ref/rmpath.html

    The site appears as shown in the following figure.

  • 7/31/2019 Mat Lab 0100

    3/45

    3

    This site can also be accessed through the help menu of MATLAB. The path is Help >

    Web Resources. (More on this topic is presented later in the lab.)

    The MathWorks site gives you access to a number of linked resources, one of which of

    which is MATLAB Central. To access this URL, click on the User Community tab. Thistakes you to a blog and other resources:

    http://www.mathworks.com/matlabcentral/

    In addition to the blog, you can access thousands of sample files that are useful inlearning efforts.

    If you are seeking a general set of tutorials, here is the MathWorks site for suchresources:

    http://www.mathworks.com/academia/student_center/tutorials/launchpad.html

    And here is the link to the MATLAB Getting Started Guide:

    http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf

    Obtaining MATLABThe University has a site license for MATLAB, and some students in applied

    mathematics can obtain a copy for use on their computers. There are restrictions on this,however. Other departments might provide similar access.

    http://www.mathworks.com/matlabcentral/http://www.mathworks.com/academia/student_center/tutorials/launchpad.htmlhttp://www.mathworks.com/academia/student_center/tutorials/launchpad.htmlhttp://www.mathworks.com/help/pdf_doc/matlab/getstart.pdfhttp://www.mathworks.com/help/pdf_doc/matlab/getstart.pdfhttp://www.mathworks.com/help/pdf_doc/matlab/getstart.pdfhttp://www.mathworks.com/academia/student_center/tutorials/launchpad.htmlhttp://www.mathworks.com/matlabcentral/
  • 7/31/2019 Mat Lab 0100

    4/45

    4

    Setting up for WorkWhen you work in MATLAB, you will seldom want to do so without saving your work

    in one way or another, and to save your work, you must attend to a few preliminaries,

    such as setting up the right work path and creating folders and *.mfiles or *.txt files.

    1. On the desktop of your computer, create a folder for your session. In this instance, useWindows Explorer to create a folder calledML0100. (Note that if you have a work

    folder already made and residing on your flash drive, then you can, of course, copy it

    to the desktop.)

    2. Open MATLAB by selectingMATLAB version. (When you do so, select theMATLAB executable that has been activated. Do not choose Activate or Deactivate.

    Activation involves having an activation number. Deactivation makes it so MATLABwill no longer function for you.)

    The Main IDE and Current Folder

    1.

    From the top menu, select Desktop > Desktop Layout > Default. You see the layoutshown in the following figure.

    2. The collection of all panels in MATLAB is referred to as the desktop. Notice theCurrent Folder (Directory) panel is on the left. This panel provides a list of the files

    in the directory you have designated as the current directory. At the top of the

  • 7/31/2019 Mat Lab 0100

    5/45

    5

    desktop, you see a Current Folder field and a browse button. On the right are the

    Workspace and Command History panels. You can always restore these by selectingoptions from the Desktop menu.

    Accessing A Working DirectoryWhen you work with Matlab, it is necessary to first set a current directly.

    1. Click on the browse button adjacent to the Current Folder field at the top of theDesktop.

    You see the Browse for Folder dialog.

    2. Navigate to theML0100 folder and select it.3. Click OK.

    The desktop is refreshed, and you see the new current directory displayed in

    two places, the top directory field and at the top of the current directory panel.

    No files are as yet visible.

  • 7/31/2019 Mat Lab 0100

    6/45

    6

    The Command Window

    The Command Window is the default starting place for working in MATLAB.

    1. To verify that you have located the Command Window, locate the double anglebraces and type the following command:7 * 787

    You see the following response:

  • 7/31/2019 Mat Lab 0100

    7/45

    7

    Creating an *.m file for a Standard Work SessionWhile you can do a great deal in the Command Window, in almost every situation

    outside the most trivial, the best practice is to begin your session by creating a file thatallows you to save the work you perform so that you can return to it for further work.

    There are several approaches to this, but the most common is to create a code file, alsoknown as an M-file.

    A file that contains MATLAB code has a *.mfile extension. Sometimes such a file is

    referred to as a MATLAB scriptfile.

    1. To create an M-file, right click in the Current Folder panel and select New File >Script.

    (Alternatively, select File > New Blank M File from the top menu. Likewise,you can click the icon beneath the File menu.)

    You see the MATLAB Editor window. This window displays the file you have

    created. Initially, it displays the title, Untitledn.

    2. Note that the file name in the Current Folder panel is initially highlighted.

  • 7/31/2019 Mat Lab 0100

    8/45

    8

    3. Verify that the Save in directory isML0100. (This is the current directory, which willbe the default for any work you do until you change it.)

    4. In the File name field, typeMLP0100.5. To open theMLP0100.mfile for editing, double click on it in the Current Folder panel.

    The Editor window appears.

    6. By default, the MATLAB desktop goes out of focus when you are working in theEditor window. To return to the desktop, type Alt + Tab.

    7. Saved the new file to theML0100 directory, the file appears in the file list in theCurrent Folder panel (on the left of the desktop).

    8. To return to Command line, press Ctrl + 0 (zero). Click the file in the dialog to openit. (Alternatively, press Alt + Tab.)

    9. To go from the Command Window press Ctrl + O and click on the file you want toopen).

  • 7/31/2019 Mat Lab 0100

    9/45

    9

    10.Alternatively, you can select from the top menu or press Ctrl+Shift+0.

    How to use the script files is covered later on in the lab.

    AlternativeThis section presents only what is obvious to most people acquainted with Windows

    applications, but it is useful for one reason, to remind you that most of the standardoperations of MATLAB can be performed using the main menu options.

    As a note, you can also create M-files by selecting File > New > Blank M-file from the

    top menu. You then immediately see the Editor window.

    From the top of the Editor window, select File > New Script.

    In the Editor window, you then select Save As. You can then save the file to the current

    directory by default.

  • 7/31/2019 Mat Lab 0100

    10/45

    10

    Adjusting Font SizeThe default font setting in MATLAB is 10 pt, and this can be difficult to read. However,

    it might be the case that if you are working in a public lab, someone using MATLABbefore you will have set the font so that you find it too large or too small. Use thefollowing procedure to adjust the font display of the editor to 12 pt, Courier.

    1. From the top menu of desktop, select File > Preferences.2. You see the Preferences dialog.3. In the Preferences dialog click Fonts and select Monospaced, Bold 12 (or 10) pt.

    4. Click Apply and then OK.

    Closing and Opening Files in the Editor Window1. To open the MLP0100.m file, double click on it in the Current Folder list. This opens

    the file in the Script Editor window.

    2. To close the file without closing the editor, press Ctrl + W or select File > Close.If you have made any changes to the file, a dialog box asks you to confirm the

    close action.

  • 7/31/2019 Mat Lab 0100

    11/45

  • 7/31/2019 Mat Lab 0100

    12/45

    12

    6. Click the control icon to close the Figure 1 window.

    Retrieving FilesMATLAB displays the Editor widow and the figure documents separately from the

    desktop. To keep things in view, you must use the Window menu or the Alt + Tab keys.

    1. Press Alt + Tab so that you see both the MATLAB desktop and the Editor window.(Cycle through the key sequence twice to get the Editor window in front of the

    desktop.)

    2. Click the control icon in the upper left of the Editor window (not the desktop!) toclose the Editor window.

    The desktop appears without the Editor window. On the left, in the Current

    Folder panel, you see theMLP0100.mfile. All the files you create in a given

    work session will appear in the Current Folder panel.

  • 7/31/2019 Mat Lab 0100

    13/45

    13

    3. Right click on theMLP0100.mfile and select Open.This action opens the file for further editing.

    Saving Figure ImagesMATLAB is (among other things) a tool for generating graphical output, so it helps fromthe start to know both how to save both the script file and the output of the script file.

    1. If it is not open, open theMLP0100.mfile.2. Click the run icon in the Editor window for the MLP0100.m file.

    The Figure window for the file appears.

    3. From the top menu of the Figure 1 pane, select File > Save As.The Save As dialog appears. By default, it shows the current working

    directory.

    4. Save the file asMLP0100.png. The file types can be selected from the drop-down list.Although the name is the same as the name used for the script file, the *.png

    extension makes the two files distinct.

    5. Select File > Close to close out of the Figure 1 pane. You again see the Editorwindow.

    6. Press Alt + Tab to get the desktop in view once again.

  • 7/31/2019 Mat Lab 0100

    14/45

    14

    7. Note that theMLP0100.png file has been written to the current directory.

    Viewing the PNG FileYou cannot open the PNG file you just created in MATLAB for viewing as an image.

    If you right click on the file and select Open, you will end up, not with an image, but with

    an array (or matrix) containing the data that defines the file (its pixel values).

    1. To view the image, use Windows Explorer to open youML0100 folder.2. Right click on the theMLP0100.png select Preview.

    The following figure shows the result in the Windows Picture and FaxViewer.

    About *.asv FilesAt times, you will see *.asv files in your working directory. These are a MATLAB

    automatic save files. Leave these files alone during your work sessions. If you want, youcan delete them at the end of a work session if you want to save disk space.

    At the same time, note that the *.asv file will usually contain a copy of your working file,

    so if the working file becomes corrupted, you may able to retrieve a fresh copy of yourwrok from the *.asv file.

  • 7/31/2019 Mat Lab 0100

    15/45

    15

    The *.asv files can be a nuisance, however, if you happen to open them rather than your

    working files as you are programming, putting new code in them rather than the workingfile.

    If you want to turn off the autosave feature (or change it so that it saves to a different

    extension or directory) use the following steps:

    1. Select File > Preferences from the top menu of the desktop.2. Select Editor/Debugger > Autosave.

    You see the Autosave dialog. (See the following figure.)

    3. Note your opptions:a. Click the checkbox next to Enable autosave to disable the autosave utility.b. Check the option to automatically delete the autosave files when you close out

    of a session.

    c. Change the extension.d. Change the directory to which the autosave files are written.

    4. As a bit of advice, it is not wise to turn off the autosave utility if you are working withlong programs. Autosaves every 5 minutes are standard. Saving work to asubdirectory visible in your working directory is a good practice.

    5. Click Cancel, for now, to close the Autosave dialog.

    More on Reopening a MATLAB Program File1. To close out of the program file, from the top menu of the Edit pane, select File >

    CloseMLP0100.m.

  • 7/31/2019 Mat Lab 0100

    16/45

    16

    2. Note that you have several Close options: Close Editor closes the editor and the file. Close alone gives you the option of closing several files. Close filename closes only the current file.

    3. To reopen theMLP0100.mfile, select File > Open. Your current directoryautomatically appears. Select theMLOP0100 file and click Open. You can also double

    click it. It is ready for editing.

    4. Note, alternatively, as mentioned before, you can open and close files by rightclicking on them in the Current Folder panel and selecting the appropriate options.

    A Second Iteration1. If necessary, use Alt + Tab to get the Editor window and the desktop in view. (If theMLP0100.mfile is not opened, right click on it in the Current Folder panel and select

    Open).2. In the Editor window for theMLP0100.mfile, select File > Save As, and save the file

    asMLP0102.m.

    3. Note that you see the name of the new M-file,MLP0102.min the title bar of the Editorwindow.

    4. Alter your code so that it appears as follows:x=1:1000;

    y=log(x);

    plot(x,y)

    5. Press F5 or click the icon on the toolbar to save, compile, and generate a newfigure.

  • 7/31/2019 Mat Lab 0100

    17/45

    17

    6. From the top menu of the Figure 1 pane, select File > Close.7. Select File > CloseMLP0102.m.8. You see the Close dialog. Click Close Selected.9. The Editor window is left in view. Click the control button on the upper right to close

    it.

    Programs and Files in the Current Folder PanelTry the following approaches to access or run the files that are displayed in the Current

    Folder panel.

    1.

    Open in the Editor. With the files in theML0100

    directory in view in the CurrentFolder panel, double click on theMLP0100.mfile. This opens the file in the Editor.

    Click the control button on the Editor pane to close it.

    2. Right click and run. Still working in the Current Folder list, right click on theMLP0100.m file and select Run File. You see the graphic pane.

    Click the control button on the Figure 1 pane to close it.

    Command Window Activities

    Generally, as mentioned previously, use M-files if you are going to write extendedprograms. You can write extended programs in the Command window, but if you do this,

    you risk losing your work. On the other hand, using the Command window for heuristic

    purposes is helpful.

    1. Bring the Command window into focus. It is, by default, located to the right of theCurrent Folder panel.

  • 7/31/2019 Mat Lab 0100

    18/45

    18

    2. A prompt is provided for you, identified as follows:>>

    3. The Command window provides a flexible work area. Working in it, you can copy,paste, erase, and perform a variety of other editing operations.

    4. Type the following command at the command prompt. (Note that you can also copyand paste the commands from this lab into the Command window.)

    edit MLP0100

    5. Press Enter.Commands (or statements) that you enter at the command prompt are not executed

    until you press Enter.

    The file opens in the Editor.

    6. Click the control button on the Editor window to close it.7. At the command prompt, as shown in the following figure, type only the name of the

    file,MLP0100 , and press Enter.

    This command executes the script file. (The figure window for it appears.)

    8. Click the control button on the Figure 1 window to close it.9. As shown in the following figure, in the Command Directory panel, click and hold on

    theMLP0100.m file and drag it into the command window.

    You instantly see the path to the file, and the Figure 1 pane appears.

    Note that if you drag and drop or in other ways successively run different files, the Figure

    1 pane is automatically refreshed with the output from the new file.

  • 7/31/2019 Mat Lab 0100

    19/45

  • 7/31/2019 Mat Lab 0100

    20/45

    20

    2. Select Clear Command History to remove all of the commands issued so far.3. A dialog asks you to confirm the clearing of the command. Click Yes.

    History Options1. Type the following command in at the command prompt:9 * 67

    2. Press enter to execute the command.3. Right click on the command as logged in the Command History panel.

    Note that it is generally not a good idea to delete all of the commands in the

    History panel. You have several options.

    Evaluate Selection. This results in the same action as clicking on the expression.It is transferred to the Command window and evaluated.

    Copy/Cut. These copy or cut the expression in the usual fashion. Create Script.See the section entitled Using History for M Files. Create Shortcut. This copies the expression to a shortcut list, which might make

    it more convenient to access if you have a long queue of expression.

    Delete Selection. Deletes the selected expression only. Delete to Selection. Deletes a list of expressions. To select a list of expressions,

    hold down the Shift key and click on the items to be included in the list.

    Profile Code. This is a somewhat advanced feature that allows you to review theperformance characteristics of the code. Performance characteristics includes thetime it takes to run.

  • 7/31/2019 Mat Lab 0100

    21/45

    21

    Command History UsesAs you may have noticed, the prompt in the Command window is often referred to as the

    command prompt.

    1. Copy and paste the following expressions to the command prompt.5*(3/4) + 9/5

    2. Press Enter.3. Note that you see the following output.

    4. Observe in the Command History panel that the expression is displayed. The answeris not shown, however. (This you see in the Workspace paneldiscussed at length in

    a future lab.)

    5. Note that all commands entered at the command prompt are automatically written tothe command history.

    6. Double click 5*(3/4) + 9/5 in the Command History panel.It is again executed in the Command window.

    7. Right click on the expression in the command history. You see the following contextmenu:

  • 7/31/2019 Mat Lab 0100

    22/45

    22

    8. Transfer the command:a. Right click on 5*(3/4) + 9/5 in the Command History panel and select Copy.b. Right click at the command prompt and select Paste.c. Press Enter.

    The command is executed, but note that when you copy a command inthis way and execute it, it is not again reported in the CommandHistory panel.

    Command History and Command PromptAlthough script are the best approach to saving your work, it remains that when you are

    working with MATLAB, in many situations you do so in a casual way, experimentingwith ideas rather than writing programs. In such situations, you can use the Command

    window, choosing afterward to save your work as an script (*.m) or a *.txt journal file.

    1. Right click in the Command History window and select Clear Command History. Awarning dialog appears. Click OK.

    2. At the prompt, type the following command:clc

    3. Using the procedure given in the previous section, enter and evaluate the followingexpressions . (Do not copy them as a group. Copy them one at a time. Each time,press Enter.)

    5*(3/4) + 9/5

    r = 4^3

    x = sqrt(36)

    4. Note that as you enter the expressions and evaluate them, the command historygrows.

  • 7/31/2019 Mat Lab 0100

    23/45

    23

    5. Double click on r=4*3. Note that the command history list grows.6. Double click on x = sqrt(36). The list grows again.7. Double click two or three times on either of the expressions. Note that the command

    history does not record duplicated commands if issued several times in a row withoutother, intervening commands.

    Arrow Keys and the Command ListNote that as long the command history consists of a number of expressions, you can use

    the up and down arrow keys to move up and down the queue.

    1. Click in the command window.2. Press the up arrow key until you see 5*(3/4) + 9/5. Press Enter. The expression is

    evaluated.

    3. Press the up or down arrow keys until you locate r=4*3. Press Enter. The expressionis evaluated.

    Selective Deletion1. Enter the following (flawed) expression at the command prompt.

    x = 3 log10(x)

    2. Note that in each instance, you see an error message in the command window.

    3. Even though the expression contains an error, it is still recorded in the commandhistory.

  • 7/31/2019 Mat Lab 0100

    24/45

    24

    4. To delete the item from the history, right click on it in the Command History paneland select Delete Selection.

    Using History for Script FilesA valuable feature of MATLAB is that the Command window can be used heuristically,

    allowing you explore ideas as they occur to you. The Command History panel stores the

    expressions you enter. If what you have explored is something that you want to keep, youcan store it to a script file.

    Use this routine to investigate possibilities.

    1. Right click in the Command History panel and select Clear Command History.Perform the following routine to get a sense of how this works.

    2. Right click in the Command window and select Clear Command Window.3. In the command window, copy and paste the following expressions, line by line,

    pressing Enter after each line.

    x = [0:0.01:5];

    y=sin(2*x +3);

    plot(x,y), axis([0 5 -1 1])

    4. Inspect the code. The first sets up an array of values to be evaluated. The sequence ofnumbers is from 0 to 5, and the sequence proceeds in increments of .01. The second

    line calculates the value ofy for each value in the array. The third line plots the

    values ofx and y using and axis with x < 0 < 5 and -1 < y < 1.

    Here is the output. (See the following figure.)

    5. Close the Figure 1 window.6. To create a script file using these three lines of code, press the Shift key and left click

    in the command history panel to select the three expressions.

  • 7/31/2019 Mat Lab 0100

    25/45

    25

    7. With the expressions selected, right click and select Create Script.You see the Editor, and the lines are already written to it.

    8. Save the file as follows:a. From the top menu of the Editor window, select File > Save.b. In the Save dialog, name the fileMLP0103. Click Save. (The file extension is

    added automatically appended.)

    You now see the MLP0103.m file in the list in the Current Folder panel.

    9. IN the editor window, press Ctrl + W twice to close the script and the Editor window.

  • 7/31/2019 Mat Lab 0100

    26/45

    26

    The Workspace, BrieflyNote that the Workspace panel has been accumulating icons as you have worked. As will

    be covered in a future lab, the Workspace stores all the data that you generate during yourwork sessions.

    This data is in the form of single variables, vectors, matrices, and other data types.

    1. Left click and pull the r value from the Workspace into the Command window.2. It appears at the prompt.3. Press Enter.

    You see the following output.

    r =

    64

    Much more will be said about the Workspace. For now, it is important to note only that itis a valuable feature of MATLAB, and like the Command History panel, provides items

    that can be saved to or retrieved from files.

    External ResourcesThis section is for information only. A vast number of script files are available fordownloading from MathWorks. To access these files, from the top menu of MATLAB,

    select Help > Web Resources > MATLAB File Exchange. At this site, you find

    thousands of executable code files, documents that relate to the use of MATLAB, and

    other resources. You are also invited up upload your own material.

  • 7/31/2019 Mat Lab 0100

    27/45

    27

    Using Externally Developed M FilesTo make use of external code samples or other resources in script files, use the followingprocedure:

    1. Download the Resources file associated with this lab and place it in theML0100directory. Unzip the Resources directory and place its unzipped version in the

    ML0100 directory. The following figures shows Windows Explorer.

    2. In the MATLAB desktop, click on the Browse button next to the Current Resourcesfield and navigate to the Resources directory folder.

    3. Click to highlight the Fractals directory.

  • 7/31/2019 Mat Lab 0100

    28/45

    28

    4. The Current Folder panel is refreshed, and you see the contents of the Fractalsdirectory.

    5. Right click on the glynnset1.mfile and select Open.6. The Editor window opens, with the code displayed.

  • 7/31/2019 Mat Lab 0100

    29/45

    29

    7. From the top menus, select Debug > Run glynnse1.m. Alternatively, in thecurrent directory panel, right click and select Run File. (You can also pressF5.)

    The file takes several seconds to a minute to compile, depending on the speed

    of your computers processor. When it is finished compiling, you see the

    following output.

    8. Take a while to examine the figure. This has been generated from scratch using theMATLAB program. It is a fractal image.

    9. Do not close the figure window.

    Saving Figure FilesWorking in the figure window for the glynnset1. m script, perform the following steps:

    1. From the top menu, select File > Save As.Note that the file will be saved by default to the directory that contains thescript file.

    2. Save the Figure 1 File as Fractal01.fig.3. Close the Figure 1 window.4. Close on the glynnset1.mfile.5. In the Current Folder panel, double click on the Fractal01.fig file.

  • 7/31/2019 Mat Lab 0100

    30/45

    30

    6. It opens instantly.7. Save the file in a standard graphics format for other uses:

    a. From the top menu of the figure window, select File > Save As.b. From the Save type as field, select TIFF.c. Name the new file Fractal01, as before.

    8. Close the Figure window.9. Right click on the Fractical01.tif file and select Open Outside MATLAB.

    Depending on how your computer has been configured, the file will open in

    Photoshop or some other application. If no application is available by default(that is, if no application has been associated with the file type), then can open

    the file by starting the application and opening the file from it.

    Running without Opening1. In the Current Folder panel, right click on the otherjulia1.mfile and select Run.2. The file executes without being opened.3. It compiles much more quickly, than the first, and you see the following output.

    4. Close the figure 1 file that is generated for this script file.5. Save this image as Julia01.tif to the directory that contains the script file.

    Viewing Multiple Code Files in the EditorBy default, the Editor window is set up so that you see only one file at a time, but you

    often work simultaneously with several files. Such situations arise if you are modifying agiven file, working to a new version. A common practice is to save the original as a new

    version. You can then open both versions and make updates. The original is at hand in the

    event that you need to refer back to it as you work.

  • 7/31/2019 Mat Lab 0100

    31/45

    31

    1. Click on the browse button adjacent to the Current Folder field and navigate back tothe ML0100 directory.

    (You can also navigate using the blue arrow in the Current Folder panel.)

    2. Likewise, in the Current Folder panel, navigate toML0100 directory.3. Right click on theMLP0103.mfile and select Open. (Or just double click on its

    name.)

    The code appears in the Editor window.

    4. As shown in the following figure, notice in the upper left of the Editor window thatyou see either a set of window options or a drop-down field. Click on the drop-downfield and select Float or click on the Float icon.

    5. You then see the floating mode for the Editor window. The file you haveopened is visible.

  • 7/31/2019 Mat Lab 0100

    32/45

    32

    6. Notice that the Editor window, by default, remains detached from the desktop andappears atop it if it is active.

    7. On the other hand, when you activate the desktop, the Editor window disappears fromview if is has been positioned atop the desktop. If this happens, select Window >

    Editor or press Ctrl + Shift + 0. (The keyboard option is the most convenient if youare programming.)

  • 7/31/2019 Mat Lab 0100

    33/45

    33

    8. In the Current Folder panel, right click on theMLP0102.mfile.You now see two code files in view.

    The windowing options allow you to tile them in a number of ways, and the

    file that is in focus is affected by the main menu and toolbar options.

    9. Close multiple files using this approach:a. From the top menu of the Editor window, select File > Close.

  • 7/31/2019 Mat Lab 0100

    34/45

    34

    You see a dialog with all current script files.

    b. Click select all. Note that you can select a subgroup for closing.c. Click Close Selected.

    More on Saving Files1. OpenMLP0103.mfile into.2. From the top menu, select File > Save as, and save the file asMLP0104.m.3. Notice that theMLP0103.mfile is removed from view, so you now see only theMLP004.mfile.

    4. Bring theMLP0104.mfile into focus and alter its code so that it reads as follows:x = [0:0.01:10];

    y=sin(2*x +3);

    plot(x,y), axis([0 10 -1 1])

    5. Click the run to run theMLP004.mfile. A figure is generated.

  • 7/31/2019 Mat Lab 0100

    35/45

    35

    6. Close the Figure 1 file without saving it.7. Save and close andMLP0104.mfile.8. Close out of the Editor window.

    Publishing to HTML and WordThe output of MATLAB is often used in academic papers or industrial publications, so

    being able to transfer its output to HTML files, Word documents, LaTex documents, or

    PDF files (to name a few) is important. The next two sections cover approaches togenerating Word and HTML files.

    Creating an HTML FileThis section assumes that you have set the path to the Fractals directory and can run the

    fractal script files. If this is not so, then refer to the section entitled Using Externally

    Developed M Files.

    1. In the Current Folder field, navigate to the Fractals directory. In the Fractalsdirectory, right click on the otherjulia1.mfile and select Open. (Alternatively,

    highlight the file and press Enter.)2. You see the otherjulia1.mfile in the Editor. (Note that the following figure shows

    the single-document viewing mode of the Editor window.)

  • 7/31/2019 Mat Lab 0100

    36/45

    36

    3. From the top menu of the Editor window, select File > Edit Publish Configuration forotherjulia1.m > Other Configurations for otherjuila1.m.

    You see the Edit M-File Configurations dialog.

  • 7/31/2019 Mat Lab 0100

    37/45

    37

    At the top of the Edit M-File Configurations dialog, the Publish configuration

    field is used to assign a name to the configuration you use to publish thedocument. To assign a name to the document, rename it after it is generated.

    Note that in the top text area of the panel (MATLAB expression), you caninsert code to format the MATLAB output. For now, the only important term

    is the name of the file (otehrjulia1). Without the name of the file, no htmlfile will generated.

    In the bottom part of the dialog, note that several options can be set. The

    height and width of the figure can be set, along with the file type of the image

    (by default set to *.png in this instance.) A thumbnail can be generated.

    4. Click in the Output file format field to see the drop-down list of options.

    5. Select html from the file format list.6. Click Publish.

    The file is processed by MATLAB, and the output is generated to an HTML

    file. By default, this file is written to directory in which the script file resides(in this case Fractals). When it finishes processing, MATLAB displays the

    sample HTLM file.

  • 7/31/2019 Mat Lab 0100

    38/45

    38

    7. Close the generated file.A folder, entitled html, is automatically generated, and the generated file

    resides in it, along with the image file.

    8. Click Close to close out of the Edit M-File Configurations dialog.

  • 7/31/2019 Mat Lab 0100

    39/45

    39

    Generating a PDF FilePDF files are often the required format for assignments submitted in class.

    1. In the Current Folder panel, navigate to the Fractals directory. In the Fractalsdirectory, right click on the otherjulia1.mfile and select Open. (Alternatively,

    highlight the file and press Enter.)

    You see the otherjulia1.mfile in the Editor.

    2. From the top menu of the Editor window, select File > Edit Publish Configurationfor otherjulia1.m > Configurations for otherjuila1.m.

    You see the Edit M-File Configurations dialog.

    3. Click in the Output file format field to see the drop-down list of options.

    4. Selectpdf from the file format list.5. Click Publish. If you get a dialog at this point that requests you to set the path for the

    file, click OK.

    The file is processed by MATLAB, and the output is generated to a *.pdf

    file and automatically opens in Adobe Reader or Adobe Acrobat. By default,this file is written to directory in which the script file resides (in this case

    Fractals).

  • 7/31/2019 Mat Lab 0100

    40/45

    40

    f

    6. Click Close to close out of the Edit M-File Configurations dialog.

    Setting PathsThe actions in this section are not usually necessary as long as you navigate to the current

    directory using the Current Folder field at the top of the work area.

    However, at times you might add a new module to MATLAB that is not in the defaultpath. Such a module might be something that you develop on you own, download from

    MATLAB, or obtain in some other way. In this case, you must include the path to the

    module.

    1. From the top menu of MATLAB, select File > Set Path. (As shown in the followingfigure.)

  • 7/31/2019 Mat Lab 0100

    41/45

    41

    2. You see the Set Path dialog.

    3. Given the current scenario, you are going to add a folder that already exists. Toaccomplish this, clickAdd Folder. You see the Browse For Folder dialog.

    4. The default display shows the contents of the Desktop (in Windows), but you mustcollapse theMy Documents folder to see the desktop folders distinctly. Find your

    ML1011 folder (or your current project folder), click to select it, and click OK.

  • 7/31/2019 Mat Lab 0100

    42/45

    42

    5. You see the Set Path dialog once again. The path you have just created to yourworking folder has been added to the top of the list.

    6.

    Click Save to save the path.7. Click Close to Close the Set Path dialog.

    NotesThe path is now set for your project folder. In this instance, you have created the projectfolder using Windows Explorer. Generally, this is the best approach and the one you will

    use most often. On the other hand, you might also start your session by copying a file

    from your flash drive onto the desktop.

    As the following figure shows, however, you can also generate a folder on the fly by

    clicking Make New Folder. Use this option to create subfolders during a work session orto create wholly new folders.

  • 7/31/2019 Mat Lab 0100

    43/45

    43

    By default, MATLab can find material only if it is in the default MATLAB directories,

    and these directories are not good places to store files you are working on. Always beginyour session by setting up a working path to your current project folder.

    Cleaning Up Your SessionGenerally, if you are working on a lab installation of MATLAB, at the end of your

    session, it is good practice to remove the path you have inserted for your session . Toaccomplish this, use the following steps:

    1. Select File > Set Path. You see the Set Path dialog. As shown in the following figure,At the top, if you have set up a path, the path is displayed :

    2. Click to select the path you have set up for the current work session (ML0100).3. ClickRemove. The Set Path dialog is refreshed, and you see that the path has been

    removed. Note that the folder itself is completely safe. MATLAB will not delete it.

  • 7/31/2019 Mat Lab 0100

    44/45

    44

    NotesIf you are working in a public setting, always clean up after yourself. Even if ITS sets up

    your working session so that you have a unique desktop, the desktop is likely to be erasedregularly, and by removing the path you have set up for a temporary session, you make it

    so that MATLAB will not become cluttered with invalid paths.

    How MATLAB Responds to Absent PathsGenerally, if you use only the Current Folder setting, MATLAB will not create problemsfor you. However, if you set paths, then you might have to periodically remove them.

    If you place a folder on your desktop and map the Current directory to it, MATLAB

    retains this setting even after it is closed. The next time you (or someone else) opens

    MATLAB, a warning like that shown in the following might appear.

    If you encounter such a message, use the following approaches to fix it. Before tryingthese things, however, note that it is essential not to remove any of the directories that are

    associated with the toolbox\matlab directory. These two approaches are not guaranteed to

    solve the problem, but they will in most cases.

    Clear the path from the path set.a) From the top menu of MATLAB, select File > Set Path. In the Set Path dialog,

    locate the path that is causing the message to occur.

    b) Click on the path to select it.c) Click Remove. Click Save. Click Close.

    Remove the Current Folder History.a) From the top menu of MATLAB, select File > Preferences.

  • 7/31/2019 Mat Lab 0100

    45/45

    b) Click the Current Folder item in the preference tree, as shown in the followingfigure.

    c)

    Click Clear History. Click Apply. Click OK.