Examples

  1. Command Window example

    Calculate the value of ""

    Type the following in the Command Window and press the Enter after each command statement.

    >> sin(pi/4)

    You should see the value 0.7071 which represents the value of the sine of a 45 degree angle. Notice that the sin function requires angles to be entered in radians, not degrees and that the value of "" is entered by typing the predefined variable name pi.

    You can evaluate the value of other functions in the same way. Just type in the function's name and the input arguments to the function enclosed in parentheses and press Enter. You can also see the value of pi that has been pre-defined by Matlab. Just type the pi in the Command Window and press Enter.

  2. Command History example

    Click on the command sin(pi/4) in the Command History window.

    Note that the expression and its result value are shown in the Command Window.

    Here are three other ways to use the Command History:

    1. Double-click to select and execute commands directly.
    2. Click and drag the command to the Command Window.
    3. Copy and paste from the Command History window to the Command Window.
  3. View the Workspace window

    Click on the tab labelled Workspace to see this window.

    You should see one variable named ans with the value 0.07071 that was the result of sin(pi/4).

    For our purposes, the Command Window and a file Editor window (not open yet) will be most useful until we start writing more complex program code. It is not necessary to close the other windows, but you can close any window that you're not using by clicking on the x in the upper right hand corner of the window frame that you wish to close. To reopen the window, use the Desktop menu and select the window that you wish to show on your desktop.

  4. Create an examples directory.

    Create a new directory named examples in the cs310 directory that you created in the Topic Discussion section. You can use this directory to store files that you create while working on examples and exercises.

    Use this widget to change the current directory.

    image of the change current directory widget

    Having a separate directory to store all of your practice work will make it easier for you to find the work you do for Examples and Exercises. It is also a good idea to create a new directory for each homework assignment.

  5. Clear all currently assigned variables.

    Type clear in the Command Window to clear all variables from the Workspace window before starting new problems. Check the Workspace window to see that your variables have been removed after executing the clear command from the Command Window.

    Use the clear command to avoid the situation where variable data from earlier problems is accidentally used and thus interferes with the results of a new problem.

  6. Close the Command History window.

    Click the small drop-down arrow and select Close to close the Command History window.

    Note that the other windows expand to fill the space.

  7. Reopen the Command History window.

    Select Layout->Command History->Docked (or Popup) from the menu to reopen the Command History window.

  8. Open the Editor window

    Here are several ways:

    • Click the New m-file button.
    • Select File -> New -> M-file from the menu.
    • Select File -> Open from the menu and choose the file you wish to open from the directory.

    Once the Editor window is open, you may wish to add it to your desktop layout. This is known as docking a window. To dock your Editor window, select Desktop -> Dock Editor from the menu.

  9. Type help sin in the command window and press Enter.
    Note that there are multiple ways to call the sin function.
  10. Type helpbrowser in the command window and press Enter.
    Note that a new window is created that allows you to search the entire help system.