Introduction to MATLAB


As the volume and complexity of data and results continues to grow with the increasing complexity of data sources and algorithms, the need for intuitive representations of that data and results becomes increasingly critical. The graphical representation of the results is often not only the most effective means of conveying the points of the study or work which has provided the data, but is in most cases an expectation of the audience of the work. Even as computing hardware continues to increase in capability, MATLAB® continues to be one of the best applications available for providing both the computational capabilities of generating data and displaying it in a variety of graphical representations. With the advent of version 6, MATLAB has taken on a new look, a new integrated development environment (IDE), new graphics development tools, and introduces some new functions. It is in that light that we offer the "upgraded" version of this topic.

Welcome to the third edition of Graphics and GUIs with MATLAB!But just as MATLAB version 6 has introduced major changes in several areas, so has this third edition. In addition to the new MATLAB specific commands and techniques, this edition offers sections on Visualization Considerations and Elements of GUI Design, which are general treatments applicable to any development software. Those familiar with the earlier editions will also be happy to find that there are now problem sets at the end of some topics that will (hopefully) motivate the new MATLAB programmer to exercise the techniques addressed earlier in the topic and make this topic more suitable to classroom settings.

MATLAB is not just a computation and plotting package; it is a versatile and flexible tool which allows users with even the most elementary programming capabilities to produce sophisticated graphics and graphical user interfaces (GUIs). The level of sophistication is only limited by one’s needs, curiosity, and imagination.

As in the previous editions, it is the goal of this topic to provide you with information, examples, and techniques which should give you the background you need to become a MATLAB graphics and GUI expert. If you are already conversant with the MATLAB programming language, this topic will provide you a ready reference with illustrative examples. If you are new to MATLAB, you will find this topic an excellent tutorial leading you to MATLAB proficiency. As in the previous editions, this topic will help take you from wherever you are in your MATLAB skills, to many steps closer to where you want to be.

Organization of This topic

This topic is organized into three general parts: Part 1: Information Visualization, Part 2: MATLAB Graphics Objects, and Part 3: Graphical User Interfaces. Each part is intended to provide the reader with a general introduction to the topic area before going into specific topics in MATLAB. For instance, if your main interest is in the visualization of data, the part on Information Visualization will give you a rudimentary introduction to the topic. Similarly, the part on Graphical User Interfaces will provide you with a good background useful in any programming language. Taken as a whole, the three parts will introduce you to the greater field of information visualization and GUI design in general, and with MATLAB specifically.

Part 1: Information Visualization will introduce you to visualization considerations such as when to use 2-D and 3-D techniques, the advantages and pitfalls of color, how motion can add another dimension of understanding, and how dynamic interaction with a visualization can enhance intuitive understanding. Contained in this part are the elementary aspects of plotting in two and three dimensions; MATLAB’s graphics commands are discussed and applied in illustrative examples. Plot manipulation and special plots are explored, including volumetric visualization for both scalar and vector volume data. Reading, writing, and manipulating bitmap graphics is covered in this section as well as printing, exporting, and saving your MATLAB visualizations.

Part 2: MATLAB Graphics Objects thoroughly explores the concept of graphics objects by introducing the fundamentals of MATLAB’s Handle Graphics™. If you consider yourself somewhat experienced with the basic plotting capabilities of MATLAB, you might well want to start with this topic. A basic understanding of Handle Graphics needs to be achieved before you can move on to more complex and sophisticated programming of graphics and GUI applications. The first topic in this section explores graphics objects, handles to them, properties and ways to change the values of properties. The next two topics explore the details of powerful dimensions that can enhance the understanding of your data, specifically the properties of color, light, transparency, and animation. Once you have grasped the concepts here you can then appreciate the power of MATLAB and will be fully equipped to comprehend the programming techniques to follow in Part 3.

Part 3: Graphical User Interfaces will bring together all you have learned by summarizing practical considerations for good GUI design. The three phases of interface design, user, and appearance considerations are covered first, followed by thorough coverage of the MATLAB Graphical User Interface Design Environment (GUIDE). Finally, user interface control elements, user actions, and the MATLAB event queue are covered so that you will be able to create GUIs that go beyond the boundaries of GUIDE.

The intent of the overall structure of this topic is to lead any MATLAB programmer through a wide variety of graphics related subjects. The information, examples, and tutorials are designed to illustrate different techniques of creating graphics. These techniques can be expanded and tailored to meet your individual needs and desires.

In addition to the topic descriptions, many of the topics contain icons in the margins to help quickly lead you to the information you need. The icons and their significance are as follows:

Speedy Solutions for those who are in a rush and don’t have the time for the details.

Power Tips will especially add to your MATLAB knowledge to make you a stronger programmer. Hopefully the whole topic falls under this icon, but there are some special tips that particularly enhance your abilities.

Tools describe what we especially feel are outstanding methods, techniques, and MATLAB programs that accomplish a specific job and make your life easier. The programs that get this icon are very useful and complement the standard set of programs that come with MATLAB.

M-file indicates that a nonstandard function is to be developed. The discussion that follows will teach you how the MATLAB code accomplishes a certain task. For your convenience, some of these M-files will be downloadable from the above website; however, we do recommend that you study the code – after all, it is there for your edification.

FAQ directs your attention to the answer to a frequently asked question about the current topic. Many of these questions come from newsgroups and classroom discussions.

Warning will call your attention to typical pitfalls.

Other visual cues will help you get around in the topic. MATLAB function names that appear in the discussions will be in bold. MATLAB code examples, fragments, and listings are throughout the topic and can be recognized readily by the distinctive courier font in which they are cast. For example, plotting the sine of a vector x that ranges from -2π to +2π in 0.1 increments with the sin function looks like this:


This same style is used for "general forms" of MATLAB functions and commands as well; however, the general forms will use all capital letters just like in the MATLAB command line help. For example, the general form of the view function when using it to return the current viewing perspective is given by:


Finally, even after you become quite familiar with MATLAB graphics and GUIs, there are always going to be "problems" or situations that require additional thought to determine how to best accomplish a task. For just such occasions, as in the earlier editions, we have compiled a "Quick Reference" in the topic . These sheets provide a summarized list of helpful hints that will help ease and hopefully speed up your development process. Many of these hints have come about through our own development, consulting, and teaching experience. Included with each hint is a reference to the applicable sections of the topic that provide further explanation on the topic or related topics.

Terminology and the MATLAB Programming Language

If you are new to MATLAB, it would be wise to familiarize yourself with some basic terminology and concepts. We recommend that you review the documentation included with MATLAB. A good place to start is with the "Getting Started" section of the MATLAB Help. If you have just upgraded to version 6, you will want to get familiar with the new MATLAB Desktop and the tools that make it up. When you start your MATLAB the Desktop is the first thing you see. In it you will see windows with names like "Workspace," "Command History," "Command Window," and "Launch Pad." This desktop can be configured in different ways, in essence customizing it to the way you like to work. Once you have installed and started your MATLAB, simply click on Help and then select "Full Product Family Help." From there, click on the folder entitled "MATLAB" then click on "Getting Started." The Mathworks has done a fine job of constructing a very extensive set of hyper-linked documents that allow you to get both fast answers and detailed discussions. Be sure to familiarize yourself with the MATLAB workspace, directory structure, and file types. You should understand what the MATLAB search path is and how you can add and remove directories from this search path. You should also know that in this topic you will be working primarily with the MATLAB file types M-files, FIG-files, and MAT-files. The final assumption we must make is that you know what we mean by the "Command Window" and the "Figure Window." The Command Window is where you can enter commands directly to MATLAB. The Figure Window is where you display graphics and GUIs by issuing the appropriate commands in the Command Window.

Getting Started

The MATLAB Desktop is what results when you invoke MATLAB on your computer and provides a convenient and easily configurable interface to the various tools that make up the development environment. Depending on how you have set preferences for your specific installation of MATLAB, it should look something like that shown in Figure1.1.

The MATLAB desktop.

Figure1.1 The MATLAB desktop.

In order to begin, we must assume that you have already gained some familiarity with the MATLAB development environment. Of course the portion of the MATLAB desktop with which you should be most familiar is the Command Window as this is where you will issue commands directly to MATLAB. Specifically, you type the MATLAB statements at the Command Window prompt which is denoted by >> . Generally we will refer to this as the "command prompt." A few other items with which you will want to become familiar are: the Command History where all the commands entered in the Command Window are recorded, the MATLAB Search Path and how you can add and remove folders from this search path, and the three MATLAB file types that we will be mainly working with M-files, FIG-files, and MAT-files. These file types derive their names from the file extensions. We will avoid other MATLAB file types such as MEX-Files and P-Files. You will also want to become familiar with the MATLAB Figure Window as this is where you display graphics and GUIs and the MATLAB Editor/Debugger where you will create scripts and functions.

Getting Help

This section is intended to get you pointed in the right direction in order to familiarize you with the MATLAB environment in terms of the directory structure, the file types, and the various windows that are available to you. If you are new to MATLAB it would be wise to familiarize yourself with some basic terminology and concepts. We recommend that you review the documentation that is included on the MATLAB Documentation CD. A good place to start is with the "Getting Started" section of the MATLAB Help. You can do this easily. After starting MATLAB, just select Help ^ MATLAB Help from the pull-down menu in the Desktop. When you do, you will see something like that shown in Figure1.2.

Getting started: Help

Figure1.2 Getting started: Help

From here you can dig as deeply as you wish into the many aspects of MATLAB.

The commands which are issued to MATLAB can be either from the original set of functions that came with your MATLAB package or the ones that you develop in the form of M-file scripts or functions. These are text files with a ".m" extension. Throughout this topic a MATLAB "program" or "function" refers to a function M-file, that is MATLAB code that has the keyword "function" in its first line. We will use the term "script" to refer to an M-file that is simply a stored list of commands. Although the differences between M-file functions and scripts are profound, we will assume that you already understand those differences. If you don’t, or would like to review M-file scripts and functions, we again refer you to the documentation that came with your MATLAB package.

One of the nice aspects about the MATLAB language is that it can be expanded by writing new functions and scripts. Moreover, any new M-file can be supplemented with on-line help. (By on-line we are not referring to the internet, but to the help available from the command prompt in the Command Window.) The on-line help feature and hypertext documentation are both useful as quick references to built-in features of MATLAB, but on-line help is something that you can provide and build into your own M-files. It practically becomes a necessity when M-files are shared among MATLAB users. A well-documented function relieves a user from the responsibility of understanding every minute detail of a function’s operation, and allows the programmer to obtain desired results by following the syntax or usage of the M-file.

The on-line help feature is activated by typing help filename, where filename is the name of the M-file whose help contents you wish to have listed (e.g. >> help plot). Here is an example you can try in the Command Window.


Notice that what happens when you invoke help for a function is that MATLAB returns in the Command Window the first block of contiguous commented lines starting from the second line of the M-file. (Try either the Editor/Debugger or the type command to see the contents of TIC.M.) When creating your own functions and help comments, keep in mind that the first comment line of the M-file should be as concise and descriptive as possible, since this is the line that will appear when one executes help directory-name, where directory-name is the name of the directory or folder containing M-files (as an example type: help graphics).

As you continue to expand your MATLAB vocabulary, the help command will be a very convenient alternative to the HTML help, the documentation CD, or other printed documentation that came with your software purchase. Sometimes all that is needed is a quick bit of information or reminder of the details, and the on-line help is perfect for that.

Several other commands that are convenient in helping you enrich your command vocabulary are more, type, and demo. The function more controls the number of lines which are displayed at a given time to the Command Window. You can turn it on by issuing the command more on, so that you can read the contents of the Command Window before the next page of output is displayed. Pressing the return key displays the next line of output while the space bar displays the next page. If you wish to stop paging through the output, just press the letter "q" on the keyboard. Issuing more off turns off the paging feature.

The contents of many MATLAB commands can be viewed by either using the type command or by opening up the file in MATLAB’s Editor/Debugger. If you want to open a file with the Editor/Debugger simply type edit followed by filename at the command prompt. (You can also use the pull-down menus in the desktop.) If you do this, the file will be opened in the Editor/Debugger and you can make and save changes. (Type edit factorial to see an example of a simple function in the Editor/Debugger.) However, if all you want is to quickly see the contents of an M-file, the command type filename allows you to list the contents of a file (if no extension is provided, MATLAB assumes and searches for an M-file) in the Command Window. We believe that viewing MATLAB programs is perhaps the quickest way to learn how to program your own MATLAB code. Using type is just the quick way of viewing the source code. However, in some cases the source code of the command is not available in a text file and cannot be typed out. For example, issuing type line will return "line is a built-in function." This means that this command has been built into MATLAB itself for computational efficiency and speed.

The Command History window keeps a record of all the commands you type at the command prompt. You can also select commands there and drag them into the Command Window. However, sometimes (especially when you are learning) you will want to save a log of your commands. The diary filename command can be used to keep a running record of what was typed and displayed in the Command Window. This can be useful in program development for several reasons. The first reason is that occasionally you may clear the Command History window before realizing that you have forgotten which commands you issued and what order they must be executed in to achieve specific results; but if diary is on you will have a file record of the commands you used. Another advantage of using the diary command is that you can create a script or function fairly rapidly by editing the resulting diary file and saving it as an M-file.

You can be selective as to what goes to the diary file by switching between diary states. The commands diary on and diary off, respectively allow or prevent your typed commands and MATLAB output to be sent to the file. In addition you can switch between different diary files by reissuing the diary filename command, where filename is the name of a different or new diary file. Diary output is always appended to the file that you specify.

Finally, we encourage you to check out the MATLAB demo packages. When you do, you will get a flavor of some of what can be accomplished with MATLAB. At the very least, you may get a jump on a solution to a problem by remembering that one of the demos did something similar to what you would like to be able to do. Once you find that demo, you can step through the code and use the ideas or techniques for your own code. The MathWorks expects and encourages you to examine their M-files so that you can learn the language quicker and with less frustration. To get a list of demos that are available to you, type help demos at the command prompt. Depending on which version MATLAB and which Toolboxes you have installed on your system, additional demonstrations may be provided to illustrate specific package capabilities.

Next post:

Previous post: