Sie sind auf Seite 1von 31

MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of Technology

Introduction to

MATLAB
Parthasarathi Satvaya
Department of Electrical Engineering Haldia Institute of Technology MATLAB WORKSHOP Department
of Electrical Engineering Haldia Institute of Technology

What Is MATLAB?

Contents About MATLAB


The MATLAB System Development Environment The MATLAB Mathematical Function Library The MATLAB Language Handle Graphics The MATLAB Application Program Interface (API) Starting and Quitting MATLAB Starting MATLAB Quitting MATLAB MATLAB Desktop Desktop Toolbar Context Menus Desktop Tools Command Window Opening the Command Window Running Functions and Entering Variables Controlling Input and Output Running Programs in M-Files Preferences for the Command Window Font & Colors Preferences for the Command Window 7 7 8 9 9 6 5

5 5 5

9 13 15

16

Command History Viewing Functions in the Command History Window Deleting Entries in the Command History Window Running Functions from the Command History Window Launch Pad Help Browser Current Directory Browser Search Path Workspace Browser Workspace Operations Array Editor Editor/Debug ger Setting Preferences

17 17

17

18

18 19 19 20 20 21 25 25 26

MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of Technology


Additional Development Environment Tools Workspace, Search Path, and File Operations - Use the Workspace browser, Array Editor, search path tool, Current Directory browser, and equivalent functions. Importing and Exporting Data - Techniques for bringing data created by other applications into the MATLAB workspace, including the Import Wizard, and packaging MATLAB workspace variables for use by other applications. Editing and Debugging M-Files - MATLAB's graphical Editor/Debugger and debugging functions for creating and changing M-files (program files containing MATLAB functions).

Improving M-File Performance - the Profiler - Tool that measures where an M-file is spending its time. Use it help you make speed improvements. Interfacing with Source Control Systems - Access your source control system from within MATLAB, Simulink, and Stateflow. Using Notebook - Access MATLAB's numeric computation and visualization software from within a word processing environment (Microsoft Word).

The MATLAB Mathematical Function Library


This is a vast collection of computational algorithms ranging from elementary functions like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigen values, Bessel functions, and fast Fourier transforms.

The MATLAB Language


This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick and dirty throw-away programs, and "programming in the large" to create complete large and complex application programs.

Handle Graphics
This is the MATLAB graphics system. It includes high-level commands for two-dimensional and threedimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level commands that allow you to fully customize the appearance of graphics as well as to build complete graphical user interfaces on your MATLAB applications.

The MATLAB Application Program Interface (API)


This is a library that allows you to write C and Fortran programs that interact with MATLAB. It include facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a

computational engine, and for reading and writing MAT-files.

5 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of

Technology

Starting and Quitting MATLAB


Starting MATLAB
On a Microsoft Windows platform, to start MATLAB, double-click the MATLAB shortcut icon on your Windows desktop. On a UNIX platform, to start MATLAB, type mat lab at the operating system prompt. After starting MATLAB, the MATLAB desktop opens - see "MATLAB Desktop" You can change the directory in which MATLAB starts, define startup options including running a script upon startup, and reduce startup time in some situations.

Quitting MATLAB
To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or type quit in the Command Window. To execute specified functions each time MATLAB quits, such as saving the workspace, you can create and run a finish. m script.

MATLAB Desktop
When you start MATLAB, the MATLAB desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB. The first time MATLAB starts, the desktop appears as shown in the following illustration, although your Launch Pad may contain different entries. You can change the way your desktop looks by opening, closing, moving, and resizing the tools in it. You can also move tools outside of the desktop or return them back inside the desktop (docking). All the desktop tools provide common features such as context menus and keyboard shortcuts. You can specify certain characteristics for the desktop tools by selecting Preferences from the File menu. For example, you can specify the font characteristics for Command Window text. For more information, click the Help button in the Preferences dialog box

Desktop Toolbar
The toolbar in the MATLAB desktop provides easy access to popular operations. Hold the cursor over a button and a tooltip appears describing the item. Note that some of the tools also have a toolbar within their window.

6 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of

Technology

Context Menus
Many of the features of the MATLAB desktop tools are available from context menus, also known as popup menus. To access a context menu, right-click on a selection and the context menu for it appears, presenting the available actions. For example, following is the context menu for a selection in the Command History window.

Desktop Tools
7 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of

Technology

The following tools are managed by the MATLAB desktop, although not all of them appear by default when you first start. If you prefer a command line interface, you can use functions to perform most of the features found in the MATLAB desktop tools. Instructions for using these function equivalents are provided with the documentation for each tool. Command Window Run MATLAB functions. Command History View a log of the functions you entered in the Command Window, copy them, and execute them. Launch Pad Run tools and access documentation for all of your MathWorks products. Current Directory Browser View MATLAB files and related files, and perform file operations such as open, and find content. Help Browser View and search the documentation for the full family of MATLAB products. Workspace Browser View and make changes to the contents of the workspace. Array Editor View array contents in a table format and edit the values. Editor/Debugger Create, edit, and debug M-files (files containing MATLAB functions). Other MATLAB tools and windows, such as figure windows, are not managed by the desktop.

Command Window
Use the Command Window to enter variables and run functions and M-files. The Command Window is the main way you communicate with MATLAB. It appears in the desktop when you first start MATLAB. Use the Command Window to run MATLAB functions (also referred to as commands) and perform MATLAB operations. The main features of the Command Window are: Opening the Command Window Running Functions and Entering Variables Controlling Input and Output- such as suppressing output and command line editing Running Programs, including M-files and external programs Preferences for the Command Window

8 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of

Technology

Opening the Command Window


To show the Command Window in the MATLAB Desktop, select Command Window from the View menu

Running Functions and Entering Variables


The prompt (>>) in the Command Window indicates that MATLAB is ready to accept input from you. When you see the >> prompt, you can enter a variable or run a function. For example, to create A, a 3by-3 matrix, type A = [1 2 3; 4 5 6; 7 8 10] When you press the Enter or Return key after typing the line, MATLAB responds with A= 123 456 7 8 10 To run a function, type the function including all arguments and press Return or Enter. MATLAB displays the result. For example, type magic(2) and MATLAB returns ans = 13 42 If you want to enter multiple lines before running, use Shift+Enter or Shift+Return after each line until the last. Then press Enter or Return to run all of the lines. The K>> prompt in the Command Window indicates that MATLAB is in debug mode.

Controlling Input and Output


You can control and interpret input and output in the Command Window in these ways: Case and Space Sensitivity Entering Multiple Functions in a Line Entering Long Lines Syntax Highlighting Font Used in the Command Window Command Line Editing Clearing the Command Window Suppressing Output Paging of Output in the Command Window Controlling the Format and Spacing of Numeric Output Printing Command Window Contents

9 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of

Technology

i) Case and Space Sensitivity MATLAB is case sensitive. For example, you cannot run the function Plot but must instead use plot. Similarly, the variable a is not the same as the variable A. Note that if you use the help function, function names are shown in all uppercase, for example, PLOT, solely to distinguish them. Do not use uppercase when running the functions. Some functions for interfacing to Java actually used mixed case and the Mfile help accurately reflects that. Blank spaces around operators such as -, :, and ( ), are optional, but they improve readability. ii) Entering Multiple Functions in a Line To enter multiple functions on a single line, separate the functions with a comma ( , ) or semicolon ( ; ). Using the semicolon instead of the comma will suppress the output for the command preceding it. For example, put three functions on one line to build a table of logarithms by typing format short; x = (1:10); logs = [x log10(x)] iii) Entering Long Lines If a statement does not fit on one line, use an ellipsis (three periods, ...) to indicate that the statement continues on the next line, press Enter or Return to advance to the next line, and then continue entering the statement. For example, s = 1 1/2 + 1/3 1/4 + 1/5 1/6 + 1/7 ... 1/8 + 1/9 1/10 + 1/11 1/12; For items in single quotes, such as strings, put the quotes in each line. For example, entering the following long string iv) Syntax Highlighting Some entries appear in different colors to help you better find elements, such as matching if/else statements: Type a string and it is colored purple. When you close the string, it becomes maroon. Type a keyword, such as the flow control function for, or a continuation (ellipsis ...), and it is colored blue. Lines you enter between the opening and closing flow control functions are indented. Double-click an opening or closing token, a parenthesis ( ), bracket [ ], or brace { }. This selects the characters between the token and its mate. Type a closing (or opening) token and the matching opening (or closing) token is highlighted briefly. Type a mismatched closing (or opening) token and a strikethrough character appears on the token. For xample

Use an arrow key to move over an opening or closing token. That token and its matching closing or opening token briefly appear underlined. Type a comment symbol, %, and what follows on the line appears in green. That information is treated by MATLAB as a comment. Type a system command, such as the ! (shell escape), and the line appears in gold. Errors appear in red

10 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

v) Font Used in the C You can specify the font type, style, and size used in the Command Window.. vi) Command Line Editing These are time-saving features you can use in the Command Window: Clipboard features Recalling previous lines Tab completion Clipboard features: Use the Cut, Copy, Paste, Undo, and Redo features from the Edit menu when working in the Command Window. Some of these features are also available in the context menu for the Command Window. Recalling Previous Lines: Use the arrow, tab, and control keys on your keyboard to recall, edit, and reuse functions you typed earlier. For example, suppose you mistakenly enter rho = (1+ sqt(5))/2 MATLAB responds with: Undefined function or variable 'sqt'. Because you misspelled sqrt. Instead of retyping the entire line, press the ' key. The previously typed line is redisplayed. Use the left arrow key to move the cursor and add the missing r. Repeated use of the up arrow key recalls earlier lines. The functions you enter are stored in a buffer. You can use smart recall to recall a previous function whose first few characters you specify. For example, typing the letters plo and pressing the up arrow key recalls the last function that started with plo, as in the most recent plot function. This feature is case sensitive. Tab Completion: MATLAB completes the name of a function, variable, filename, or handle graphics property if you type the first few letters and then press the Tab key. If there is a unique name, the name is automatically completed. For example, if you created a variable costs_march, type Costs and press Tab. MATLAB completes the name, displaying costs_march

11 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Press Return or Enter to run the statement. In this example, MATLAB displays the contents of costs_march. If there is more than one name that starts with the letters you typed, press the Tab key again to see a list of the possibilities. For example, type cos and press Tab. MATLAB does not display anything, indicating there are multiple names beginning with cos. Press Tab again and MATLAB displays cos cosh costfun cos_tr cosint costs_march The resulting list of possibilities includes the variable name you created, costs_march, but also includes functions that begin with cos. Note that the list might include files which are not valid commands, including private functions. vii) Clearing the Command Window Select Clear Command Window from the Edit menu to clear it. This does not clear the workspace, but only clears the view. Afterwards, you still can use the up arrow key to recall previous functions. Function Equivalent. Use clc to clear the Command Window. Similar to clc, the home function moves the prompt to the top of the Command Window. Clearing Commands:If you have typed a command that you then decide not to execute, you can clear it from the Command Window by pressing the Escape (Esc) key. viii) Suppressing Output If you end a line with a semicolon ( ; ), MATLAB runs the statement but does not display any output when you press the Enter or Return key. This is particularly useful when you generate large matrices. For example, typing A = magic(100); and then pressing Enter or Return creates A but does not display the resulting matrix. ix) Paging of Output in the Command Window If output in the Command Window is lengthy, it might not fit within the screen and will display too quickly for you to see it. Use the more function to control the paging of output in the Command Window. By default, more is off. When you type more on, MATLAB displays only a page (a screen full) of output at a time. After the first screen displays, press one of the following keys.

12 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

x) Controlling the Format and Spacing of Numeric Output By default, numeric output in the Command Windows is displayed as 5-digit scaled, fixed-point values. Use the text display preference to change the numeric format of output. The text display format affects only how numbers are shown, not how MATLAB computes or saves them. xi) Printing Command Window Contents To print the complete contents of the Command Window, select Print from the File menu. To print only a selection, first make the selection in the Command Window and then choose Print Selection from the File menu.

Running Programs in M-Files


Running M-Files Run M-files, files that contain code in the MATLAB language, the same way that you would run any other MATLAB function. Type the name of the M-file in the Command Window and press Enter or Return. To display each function in the M-file as it executes, use the Display preference and check Echo on, or use the echo function set to on. Interrupting a Running Program You can interrupt a running program by pressing Ctrl+C or Ctrl+Break at any time. On Windows platforms, you may have to wait until an executing built-in function or MEX-file has finished its operation. On UNIX systems, program execution will terminate immediately. Running External Programs The exclamation point character, !, is a shell escape and indicates that the rest of the input line is a command to the operating system. Use it to invoke utilities or run other programs without quitting from MATLAB. On UNIX, for example, !vi darwin.m invokes the vi editor for a file named darwin . m. After the program completes or you quit the program, the operating system returns control to MATLAB. See the functions unix and dos to run external programs that return results and status. Opening M-Files To open an M-file, select the file or function name in the Command Window, and then right-click and select Open Selection from the context window. The M-file opens in the Editor/Debugger. Examining Errors If an error message appears when running an M-file, click on the underlined portion of the error message, or press Ctrl+Enter. The offending M-file opens in the Editor, scrolled to the line containing the error. Profiling One way to improve the performance of your M-files is to profile them. MATLAB provides an M-file profiler that lets you see how much computation time each line of an M-file uses. Profiling is a way to measure where a program spends its time. Measuring is a much better method than guessing where the most execution time is spent. You probably deal with obvious speed issues at design time and can then discover unanticipated effects through measurement. One key to effective coding is to create an original implementation that is as simple as possible and then use a profiler to

13 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

identify bottlenecks if speed is an issue. Premature optimization often increases code complexity unnecessarily without providing a real gain in performance. An Example Using the Profiler This example demonstrates how to run the profiler. 1 To start the profiler, type in the Command Window prfile on -detail builtin history The -detail builtin option instructs the profiler to gather statistics for built-in functions, in addition to the default M-functions, M-subfunctions, and MEX-functions. The -history option instructs the profiler to track the exact sequence of entry and exit calls. 2 Execute an M-file. This example runs the Lotka-Volterra predator-prey population model. For more information about this model, type lotkademo to run a demonstration. [t,y] = ode23('lotka',[0 2],[20;20]); 3 Generate the profile report and save the results to the file lotkaprof. profile report lotkaprof This suspends the profiler, displays the profile report in your systems default Web browser, and saves the results 4 Restart the profiler, without clearing the existing statistics. profile resume The profiler is now ready to continue gathering statistics for any more M-files you run. It will add these new statistics to those generated in the previous steps. 5 Stop the profiler when you are finished gathering statistics. profile off

14

MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of Technology Preferences for the Command Window
Using preferences, you can specify the format for how numeric values are displayed, set echoing on automatically for each session, specify the font type, style, and size, and set the colors used for syntax highlighting for contents of the Command Window. To set preferences for the Command Window, select Preferences from the File menu in the Command Window. The Preferences dialog box opens showing Command Window Preferences. Text Display and Display Preferences for the Command Window Text display. Specify how output appears in the Command Window: Numeric format - Output format of numeric values displayed in the Command Window. This affects only how numbers are displayed, not how MATLAB computes or saves them. The format reference page includes the list of available formats. Numeric display - Spacing of output in the Command Window. To suppress blank lines, use compact. To display blank lines, use loose. For more information, see the reference page for format. Spaces per tab - Number of spaces assigned to a tab stop when displaying output. The default is 4. Display. Specify echoing option and buffer size: Echo on - Check the box if you want commands running in M-files to display in the Command Window during the M-file execution. For more information, see the reference page for echo. Wrap lines - Input and output in the Command Window uses multiple lines to fit within the current width of the Command Window. Note that the horizontal scroll bar does not appear in this mode because it is not needed.

Limit matrix display width to eighty columns - Check the box if you want MATLAB to display only 80 columns of matrix output, regardless of the width of the Command Window. Uncheck the box if you make the Command Window wider than 80 columns and want matrix output to fill the width of the Command Window. See also the display reference page.

15 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Enable up to n tab completions - Check the box if you want to use tab completion when typing functions in the Command Window. Then enter a limit in the edit box. For example, if you enter 10, when you use the tab completion feature, MATLAB displays the list of possible completions if there are 10 or less. If there are more than 10, MATLAB displays a message stating there are more than 10 completions. Uncheck the box if you do not want to use the tab completion feature. MATLAB moves the cursor to the next tab stop when you press the Tab key, rather than completing a function. Command session scroll buffer size - Set the size of the buffer that maintains a list of previously run commands to be used for command recall.

Font & Colors Preferences for the Command Window


Font. Command Window font preferences specify the characteristics of the font used in the Command Window. Select Use desktop font if you want the font in the Command Window to be the same as that specified for General Font & Colors preferences. If you want the Command Window font to be different, select Use custom font and specify the font characteristics for the Command Window: Type, for example, SansSerif Style, for example, bold Size in points, for example, 12 points After you make a selection, the Sample area shows how the font will look. Colors. Specify the colors used in the Command Window: Text color - The color of nonspecial text; special text uses colors specified for Syntax highlighting Background color - The color of background in the window Syntax highlighting - The colors to use to highlight syntax. If checked, click Set Colors to specify them. For a description of syntax highlighting,

16 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Command History
Lines you enter in the Command Window are logged in the Command History window. In the Command History, you can view previously used functions, and copy and execute selected lines. To save the input and output from a MATLAB session to a file, use the diary function.

Viewing Functions in the Command History Window


The log in the Command History window includes functions from the current session, as well as from previous sessions. The time and date for each session appear at the top of the history of functions for that session. Use the scroll bar or the up and down arrow keys to move through the Command History window. The Command History file is history .m. Type pref dir in the Command Window to see the location of the file. The history. m file is loaded when MATLAB starts and overwritten when you close MATLAB.

Deleting Entries in the Command History Window


Delete entries in the Command History window when you feel there are too many and it's inconvenient finding the ones you want. All entries remain until you delete them. To delete entries in the Command History window select an entry, or Shift+click or Ctrl+click to select multiple entries, or use Ctrl+A to select all entries. Then right-click and select one of the delete options from the context menu: Delete Selection - Deletes the selection Delete to Selection - Deletes all functions previous to (above) the selected function Delete Entire History - Deletes all functions in the Command History window Another way to clear the entire history is by selecting Clear Command History from the Edit menu.

17 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Running Functions from the Command History Window


Double-click on any function entry (entries) in the Command History window to execute that function(s). For example, double-click edit myfile to open my file . m in the Editor. You can also run a function entry by right-clicking on it and selecting Evaluate Selection from the context menu, or by copying the function entry to the Command Window.

Launch Pad
MATLABs Launch Pad provides easy access to tools, demos, and documentation. To open it, select Launch Pad from the View menu in the MATLAB desktop. All the products installed on your system are listed To display the listings for a product, click the + to the left of the product. To collapse the listings, click the to the left of the product. To open one of the listings, double-click it, or right-click and select Open from the context menu. The action depends on the listing you selected, as described in the following table. You can add your own entries to the Launch Pad by creating an info.xml file. To see an example, select one of the existing entries in the Launch Pad right-click, and select Edit Source from the context menu. The info.xml file for that product appears. The line for the tool you selected appears highlighted. Create a similar info.xml file for your own application and put it in a folder that is on the search path. Right-click in the Launch Pad and select Refresh from the context menu to update the Launch Pad so it includes your entries.

18 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Help Browser
Use the Help browser to search and view documentation for all your MathWorks products. The Help browser is a Web browser integrated into the MATLAB desktop that displays HTML documents. To open the Help browser, click the help button in the toolbar, or type help browser in the Command Window. . The Help browser consists of two panes, the Help Navigator, which you use to find information, and the display pane, where you view the information. Syntax Help For help about the general syntax of MATLAB functions and commands, type help syntax

Current Directory Browser


MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path. A quick way to view or change the current directory is by using the Current Directory field in the desktop toolbar as shown below. To search for, view, open, and make changes to MATLAB-related directories and files, use the MATLAB Current Directory browser. Alternatively, you can use the functions dir, cd, and delete.

19 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Search Path
To determine how to execute functions you call, MATLAB uses a search path to find M-files and other MATLAB-related files, which are organized in directories on your file system. Any file you want to run in MATLAB must reside in the current directory or in a directory that is on the search path. By default, the files supplied with MATLAB and MathWorks toolboxes are included in the search path. To see which directories are on the search path or to change the search path, select Set Path from the File menu in the desktop, and use the Set Path dialog box. Alternatively, you can use the path function to view the search path, addpath to add directories to the path, and rmpath to remove directories from the path.

Workspace Browser
The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces. For example, if you type t = 0:pi/4:2*pi; y = sin(t); the workspace includes two variables, y and t, each having nine values.

20 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

To view the workspace and information about each variable, use the Workspace browser, or use the functions who and whos. To delete variables from the workspace, select the variable and select Delete from the Edit menu. Alternatively, use the clear function. The workspace is not maintained after you end the MATLAB session. To save the workspace to a file that can be read during a later MATLAB session, select Save Workspace As from the File menu, or use the save function. This saves the workspace to a binary file called a MAT-file, which has a .mat extension. There are options for saving to different formats. To read in a MAT-file, select Import Data from the File menu, or use the load function.

Workspace operations
you can perform from the Workspace browser or with functions are: Viewing the Current Workspace Saving the Current Workspace Loading a Saved Workspace Clearing Workspace Variables Viewing Base and Function Workspaces Using the Stack Creating Graphics from the Workspace Browser Viewing and Editing Workspace Variables Using the Array Editor i) Viewing the Current Workspace The Workspace browser shows the name of each variable, its array size, its size in bytes, and the class. The icon for each variable denotes its class. To resize the columns of information, drag the column header borders. To show or hide any of the columns, or to specify the sort order, select Workspace View Options from the View menu.

21

MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of Technology You can select the column on which to sort in the Workspace
browser as well as reverse the sort order of any column. Click on a column heading to sort on that column. Click on the column heading again to reverse the sort order in that column. For example, to sort on Size, click the column heading once. To change from ascending to descending, click on the heading again. Function Alternative. Use who to list the current workspace variables. Use whos to list the variables and information about their size and class. For example: ii) Saving the Current Workspace The workspace is not maintained across MATLAB sessions. When you quit MATLAB, the workspace is cleared. You can save any or all of the variables in the current workspace to a MAT-file, which is a MATLAB specific binary file. You can then load the MAT-file at a later time during the current or another session to reuse the workspace variables. MAT-files use a . mat extension. Note that the .mat extension is also used by Microsoft Access. Saving All Variables To save all of the workspace variables using the Workspace browser: 1.From the File or context menu, select Save Workspace As, or click the save button in the Workspace browser toolbar.

The Save dialog box opens. 2 Specify the location and File name. MATLAB automatically supplies the .mat extension. 3 Click Save. The workspace variables are saved under the MAT-file name you specified. You can also save the workspace variables from the Command Window by selecting Save Workspace As from the File menu. Saving Selected Variables To save some but not all of the current workspace variables: 1 Select the variable in the Workspace browser. To select multiple variables Shift+click or Ctrl+click. 2 Right-click and from the context menu, select Save Selection As. The Save to MAT-File dialog box opens.

22 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

3 Specify the location and File name. MATLAB automatically supplies the .mat extension. 4 Click Save. The workspace variables are saved under the MAT-file name you specified. Function Alternative. To save workspace variables, use the save function followed by the filename you want to save to. For example, save('june10') saves all current workspace variables to the file june10. mat. If you don't specify a filename, the workspace is saved to matlab. mat in the current working directory. You can specify which variables to save, as well as control the format in which the data is stored, such as ascii. For these and otherforms of the function, see the reference page for save. iii) Loading a Saved Workspace To load a workspace that you previously saved: 1.Click the load data button on the toolbar in the Workspace browser, or right-click in the Workspace browser and select Import Data from the context menu. The Open dialog box opens. 2. Select the MAT-file you want to load and click Open. The variables and their values, as stored in the MAT-file, are loaded into the workspace.You can also load a saved workspace using the Import Wizard. Select Import Data from the File menu and then select a file from the Import dialog box. The Import Wizard opens. Use the Import Wizard to open the saved workspace. Function Alternative. Use load to open a saved workspace. For example, load('june10') loads all workspace variables from the file june10.mat. Note If the saved MAT-file june10 contains the variables A, B, and C, then loading j une10 places the variables A, B, and C back into the workspace. If the variables already exist in the workspace, they are overwritten with the variables from june10 iv) Clearing Workspace Variables You can delete a variable, which removes it from the workspace. To clear a variable using the Workspace browser: 1 In the Workspace browser, select the variable, or Shift+click or Ctrl+click to select multiple variables. To select all variables, choose Select All from the Edit or context menus. 2 Do one of the following to clear the selected variables: - Press the Delete key.

23 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

- From the Edit menu, select Delete. - Click the delete button ""D on the toolbar. - Right-click and select Delete Selection from the context menu. 3 A confirmation dialog box may appear. If it does, click Yes to clear the variables. The confirmation dialog box appears if you specify it as a preference. To delete all variables at once, select Clear Workspace from the Edit menu, or from the context menu in the orkspace browser. Function Alternative. Use the clear function. For example, clear A M clears the variables A and M from the workspace. v) Viewing Base and Function Workspaces Using the Stack When you run M-files, MATLAB assigns each function its own workspace, called the function workspace, which is separate from MATLAB's base workspace. You can access the base and function workspaces when debugging M-files by using the Stack field in the Workspace browser. The Stack field is only available in debug mode; otherwise it is grayed out. The Stack field is also accessible from the Editor/Debugger. vi) Creating Graphics from the Workspace Browser From the Workspace browser, you can generate a graph of a variable. Right-click on the variable you want to graph. From the context menu, select Graph Selection and then choose the type of graph you want to create. The graph appears in a figure window. For more information about creating graphs in MATLAB, see MATLAB graphics documentation. Preferences for the Workspace Browser You can specify as a preference the fonts to use in the Workspace browser and whether or not you want a confirmation dialog box to appear when you clear variables using the Workspace browser. From the Workspace browser File menu, select Preferences. The Preferences dialog box opens to the Workspace Preferences panel. Font. Workspace browser font preferences specify the characteristics of the font used in the Workspace browser. Select Use desktop font if you want the font in the Workspace browser to be the same as that specified for General Font & Colors preferences. If you want the Workspace browser font to be different, select Use custom font and specify the font characteristics for the Workspace browser: Type, for example, SansSerif Style, for example, bold Size in points, for example, 12 points After you make a selection, the Sample area shows how the font will look. Confirm Deletion of Variables. Check the box for Confirm deletion of variables if you want a confirmation dialog box to appear when you delete a variable.

24 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

vii) Viewing and Editing Workspace Variables Using the Array Editor Use the Array Editor to view and edit a visual representation of one or two-dimensional numeric arrays, strings, and cell arrays of strings.

Array Editor
Double-click on a variable in the Workspace browser to see it in the Array Editor. Use the Array Editor to view and edit a visual representation of one- or two-dimensional numeric arrays, strings, and cell arrays of strings that are in the workspace.

Editor/Debugger
Use the Editor/Debugger to create and debug M-files, which are programs you write to run MATLAB functions. The Editor/Debugger provides a graphical user interface for basic text editing, as well as for M-file debugging. You can use any text editor to create M-files, such as Emacs, and can use preferences (accessible from the desktop File menu) to specify that editor as the default. If you use another editor, you can still use the MATLAB Editor/ Debugger for debugging, or you can use debugging functions, such as dbstop, which sets a breakpoint. If you just need to view the contents of an M-file, you can display it in the Command Window by using the type function.

25 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Setting Preferences
Set preferences to modify the default behavior of some aspects of MATLAB such as the font used in the Command Window. Preferences remain persistent across MATLAB sessions. Note that some tools allow you to control these aspects from within the tool without setting a preference use that method if you only want the change to apply to the current session.To set preferences: 1 Select Preferences from the File menu. The Preferences dialog box opens. The page it opens to reflects the currently active window. 2 In the left pane, select the type of preferences you want to specify. In the above example, General preferences are selected. If a + appears to the left of an item, click the + to display more items, and then select the item you want to set preferences for.The right pane reflects the type of preference you selected. 3 In the right pane, specify the preference values and click OK. The preferences take effect immediately.

26 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute of Technology Examples of Formats
Here are a few examples of the various formats and the output produced from the following two-element vector x, with components of different magnitudes. x = [4/3 1.2345e-6] format short 1.3333 0.0000 format short e 1.3333e+000 1.2345e-006 format short g 1.3333 1.2345e-006 format long 1.33333333333333 0.00000123450000 format long e 1.333333333333333e+000 1.234500000000000e-006 format long g 1.33333333333333 1.2345e-006 format bank 1.33 0.00 format rat 4/3 1/810045 format hex 3ff5555555555555 3eb4b6231abfd271 format + ++ For a complete list and description of available formats, see the reference page for format. If you want more control over the output format, use the sprintf and fprintf functions.

28 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Controlling Spacing
Use the text display preference or format function to control spacing in the output. Use format compact to suppress blank lines, allowing you to view more information in the Command Window. To include the blank lines, which can help make output more readable, use format loose

Few General Purpose Commands


General information help - On-line help, display text at command line. helpwin - On-line help, displayed in the help browser. helpdesk - Comprehensive hypertext documentation and troubleshooting. support - Open MathWorks Technical Support Web Page. demo - Run demonstrations. ver - MATLAB, SIMULINK, and toolbox version information. whatsnew - Access Release Notes. Managing the workspace. who - List current variables. whos - List current variables, long form. workspace - Display Workspace Browser, a GUI for managing the workspace. clear - Clear variables and functions from memory. load - Load workspace variables from disk. save - Save workspace variables to disk. quit - Quit MATLAB session. Managing commands and functions. what - List MATLAB-specific files in directory. type - List M-file. edit - Edit M-file. open - Open files by extension. which - Locate functions and files.

29 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Managing the search path path - Get/set search path. addpath - Add directory to search path. rmpath - Remove directory from search path. pathtool - Modify search path. Controlling the command window. echo - Echo commands in M-files. more - Control paged output in command window. diary - Save text of MATLAB session. format - Set output format. beep - Produce beep sound. Operating system commands cd - Change current working directory. copyfile - Copy a file. pwd - Show (print) current working directory. dir - List directory. delete - Delete file. mkdir - Make directory. ! - Execute operating system command (see PUNCT). Debugging M-files. debug - List debugging commands. dbstop - Set breakpoint. dbclear - Remove breakpoint. Profiling M-files. profile - Profile function execution time. profreport - Generate profile report.

30 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Operators and special characters


Arithmetic operators. plus - Plus + uplus - Unary plus + minus - Minus uminus - Unary minus mtimes - Matrix multiply * times - Array multiply .* mpower - Matrix power ^ power - Array power .^ mldivide - Backslash or left matrix divide \ mrdivide - Slash or right matrix divide / ldivide - Left array divide .\ rdivide - Right array divide ./ Relational operators. eq - Equal == ne - Not equal ~= lt - Less than < gt - Greater than > le - Less than or equal <= ge - Greater than or equal >= Logical operators. and - Logical AND & or - Logical OR | not - Logical NOT ~ xor - Logical EXCLUSIVE OR all - True if all elements of vector are nonzero Special characters. colon - Colon : paren - Parentheses and subscripting ( ) paren - Brackets [ ] paren - Braces and subscripting { } punct - Function handle creation @ punct - Decimal point . punct - Structure field access . punct - Parent directory .. punct - Continuation ... punct - Separator , punct - Semicolon ; punct - Comment % punct - Invoke operating system command ! punct - Assignment = punct - Quote ' transpose - Transpose .' ctranspose - Complex conjugate transpose ' horzcat - Horizontal concatenation [,] vertcat - Vertical concatenation [;]

31 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

subsasgn - Subscripted assignment ( ),{ },. subsref - Subscripted reference ( ),{ },. subsindex - Subscript index Bitwise operators. bitand - Bit-wise AND. bitcmp - Complement bits. bitor - Bit-wise OR. bitmax - Maximum floating point integer. bitxor - Bit-wise XOR. bitset - Set bit. bitget - Get bit. bitshift - Bit-wise shift. Set operators. union - Set union. unique - Set unique. intersect - Set intersection. setdiff - Set difference. setxor - Set exclusive-or. ismember - True for set member.

Help on Functions from the Command Window


Several types of help on functions are available from the Command Window: To list all categories that you can request help on from the Command Window,

just type
help To see a list of functions for one of these categories, along with a brief description of each function, type help <category>. For example, help datafun To get help on a particular function, type help <functionname>. For example, help sortrows

32 MATLAB WORKSHOP Department of Electrical Engineering Haldia Institute

of Technology

Using Notebook
Notebook allows you to access MATLABs numeric computation and visualization software from within a word processing environment (Microsoft Word). Using Notebook, you can create a document, called an M-book, that contains text, MATLAB commands, and the output from MATLAB commands. annotated with text or as a document embedded with live MATLAB commands and output. Notebook is useful for creating electronic or printed records of MATLAB sessions, class notes, textbooks or technical reports. Creating an M-Book from MATLAB To create a new M-book from within MATLAB, type Notebook at the prompt. If you are running Notebook for the first time, you may need to configure it. Notebook starts Microsoft Word on your system and creates a new M-book, called Document1. Notebook adds the Notebook menu to the Word menu bar. You use this menu, illustrated below, to access Notebook commands. Configuring Notebook After you install Notebook, you must configure it. (Notebook is installed as part of the MATLAB installation process. See the MALAB Installation Guide for PC for more information.) To configure Notebook, type notebook setup in the MATLAB command window. Notebook prompts you to specify which version of Microsoft Word you are using. Choose your version of Microsoft Word: [1] Microsoft Word for Windows 95 (Version 7.0) [2] Microsoft Word 97 [3] Microsoft Word 2000 [4] Exit, making no changes Microsoft Word Version: 2 Next Notebook prompts you to specify the location of the Microsoft Word executable (winword.exe) on your system. You will be presented with a dialog box. Please use it to select your copy of the Microsoft Word 97 executable (winword.exe). Press any key to continue... Use this dialog box to specify the location of your Microsoft Word program. When you click Open, Notebook prompts you to specify the name of a Microsoft Word template file. You will be presented with a dialog box. Please use it to select a Microsoft Word template (.dot) file in one of your Microsoft Word template directories. We suggest that you specify your normal.dot file. Press any key to continue... Use this dialog box to specify the name of your Microsoft Word template file. When configuration is completed, Notebook outputs this message. 33

Running MATLAB Functions


The Command Window is the main way you communicate with MATLAB. It appears in the desktop when you first start MATLAB. Use the Command Window to run MATLAB functions (also referred to as commands) and perform MATLAB operations. The prompt (>>) in the Command Window indicates that MATLAB is ready to accept input from you. When you see the >> prompt, you can enter a variable or run a function. For example, to create A, a 3by-3 matrix, type A = [1 2 3; 4 5 6; 7 8 10] When you press the Enter or Return key after typing the line, MATLAB responds with A= 123 456 7 8 10 To run a function, type the function including all arguments and press Return or Enter. MATLAB displays the result. For example, type magic(2) and MATLAB returns ans = 13 42 The K>> prompt in the Command Window indicates that MATLAB is in debug mode.

Evaluating a Selection
To run a selection in the Command Window, make the selection, and then right-click and select Evaluate Selection from the context menu. You cannot evaluate a selection while MATLAB is busy, for example, running an M-file.

Opening a Selection
To open the M-file for a function, select that function in the Command Window, and then right-click and select Open Selection from the context window. The M-file for that function opens in the Editor/Debugger.

Running One Process


You can only run one process at a time. If MATLAB is busy running one function, any commands you issue will be stacked. The next command will run when the previous one finishes. For example, you cannot set breakpoints from the Editor/Debugger while MATLAB is running a function in the Command Window.

Function Equivalent
Use the format function to control the output format of the numeric values displayed in the Command Window. The format you specify applies only to the current session.

27

Das könnte Ihnen auch gefallen