0% found this document useful (0 votes)
60 views

1.2 The Desktop

The document discusses the MATLAB desktop interface and provides examples of writing MATLAB programs. It describes the various tools available in the desktop, including the Command Window and Current Directory browser. It then gives examples of writing simple MATLAB scripts to plot a function and calculate bank interest, and explains how to save, run and edit script files.

Uploaded by

Lucien Yemah
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

1.2 The Desktop

The document discusses the MATLAB desktop interface and provides examples of writing MATLAB programs. It describes the various tools available in the desktop, including the Command Window and Current Directory browser. It then gives examples of writing simple MATLAB scripts to plot a function and calculate bank interest, and explains how to save, run and edit script files.

Uploaded by

Lucien Yemah
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

2 The Desktop

The default Matlab desktop presented below has already been seen. There are 6
configurations that are predefined. Each configuration has many tools. While the menus
will not be examined here, a word will be presented on getting help.

To get into the Help browser, either click on the help button (?) in the desktop toolbar, or
select the Help menu in any tool. The Help Navigator pane appears on the left. This is
where you can get the content, Getting started or Development Environment. MATLAB
Desktop is listed under the latter.

The desktop contains a number of tools. This includes the Command Window. On the left
is the Current Directory, which shares a docking position with the Workspace browser.
Use the tabs to switch between the Current Directory and the Workspace browser. Below
the Current Directory is the Command History.

Any of these windows can be resized in the usual way. A window can be moved out of
the MATLAB desktop by undocking it. Do this either by clicking on the arrow in the
windows title bar, or by making the window active (click anywhere inside it) and then

1
selecting Undock from the View menu. To dock a tool window that is outside the
MATLAB desktop select Dock from its View menu.

You can group desktop windows so that they occupy the same space in the MATLAB
desktop. Access to the individual windows is then by means of their tabs. To group
windows in this way drag the title bar of one window on top of the title bar of the other
window. The outline of the window youre dragging overlays the target window, and the
bottom of the outline includes a tab. The Status bar informs you to Release the mouse
button to tab-dock these windows. If you release the mouse the windows are duly tab-
docked in the outlined position.

1.3 A Program Development Tool

Sometimes a single statement might not be enough to solve a problem. The solution
might involve several statements. A collection of statements to solve such a problem is
called a program.

To write a program, select File -> New -> M-le, or click the new le button on the
desktop toolbar (you could also type edit in the Command Window followed by Enter).
This action opens an Untitled window in the Editor/Debugger. This is a scratch pad in
which to write programs.

For example, we want to plot a graph of e-0.2x sin (x) over the domain 0 to 6. We can
proceed with this short program in the editor window (exactly as it is)

x= 0:pi/20:6*pi;
plot (x, exp(-0.2*x) .* sin(x), r), grid

The argument r for plot will draw a red graph.

Now select and copy the codes in the usual way (highlight codes go to edit menu and
copy). This process is called dragging. Then go to the command window and paste the
codes as prompted. Execute the codes with the <Enter> button. The graph should appear
in a gure window.

This process, from highlighting (selecting) text in the Editor, to copying it into the
Command Window, is called cut and paste (more correctly copy and paste).

You can correct the program by going back to the Editor, then click at the position of the
error, make the correction, then and cut and paste again. Alternatively, you can use
command-line editing to correct mistakes. Yet another alternative is to paste from the
Command History window. To select multiple lines in the Command History window
keep Ctrl down while you click.
2
As another example, calculate your balance for one year if you deposited $1000 in a bank
at an interest rate of 9%. Even with a relatively simple problem like this, it often helps
rst to write down a rough plan of the solution. The plan of the solution is as below:

1. Get the data (initial balance and interest rate) into MATLAB.
2. Calculate the interest (9 percent of $1000, i.e. $90).
3. Add the interest to the balance ($90 + $1000, i.e. $1090).
4. Display the new balance.

Now go back to the Editor. To clear out any previous text, select it as usual by dragging
(or use Ctrl+A), and press the Del key. Enter the following program, and then cut and
paste it to the Command Window:

>> balance = 1000;


rate = 0.09;
interest = rate * balance;
balance = balance + interest;
disp( New balance: );
disp( balance ); <Enter>

New balance:
1090

(Note how information in the single quotes is present in the output)

To save the contents of the Editor, select File -> Save from the Editor menubar. A Save
le as: dialogue box appears. Select a directory and enter a le name, which must have
the extension .m, in the File name box, e.g. junk.m. Click on Save. The Editor window
now has the title junk.m. If you make subsequent changes to junk.m in the Editor, an
asterisk appears next to its name at the top of the Editor until you save the changes.

A MATLAB program saved from the Editor (or any ASCII text editor) with the extension
.m is called a script le, or simply a script. (MATLAB function les also have the
extension .m. MATLAB therefore refers to both script and function les generally as M-
les.) The special signicance of a script le is that, if you enter its name at the
command-line prompt, MATLAB carries out each statement in the script le as if it were
entered at the prompt.

As an example, save the compound interest program above in a script le under the name
compint.m. Then simply enter the name

Compint
3
at the prompt in the Command Window. As soon as you hit Enter the statements in
compint.m will be carried out exactly as if you had pasted them into the Command
Window. You have effectively created a new MATLAB command, viz., compint.
To list the script le in the Command Window, use the command type,

Ex.
type compint

The extension .m may be omitted.

Script les provide a useful way of managing large programs which you do not
necessarily want to paste into the Command Window every time you run them. When you
run a script file, set the Matlab current directory to the directory to which the file is
saved. The current directory can always be changed by typing a new path in the Current
Directory field, selecting from the drop-down list or browsing the directories. This could
also be done on the command line with the cd command.

Ex
cd\mydirectory

A handy way to run a script is as follows. Select the le in the Current Directory browser.
Right-click it. The context menu appears. Select Run from the context menu. The results
appear in the Command Window. If you want to edit the script, select Open from the
context menu. Many other features can be viewed in the current directory.

The MATLAB system is technically called an interpreter (as opposed to a compiler). This
means that each statement presented to the command line is translated (interpreted) into
language the computer understands better, and then immediately carried out. It should be
noted that the interpreted statements are carried out in order from the top down.

4
Chapter 2

Matlab Fundamentals

2.1 Variable

We have already seen that a variable name must comply with the following two rules:

1. It may consist only of the letters az, the digits 09 and the underscore (_).
2. It must start with a letter.

A variable name may be as long as you like, but MATLAB only remembers the rst 31
characters. A variable is created simply by assigning a value to it at the command line or
in a program, If you attempt to refer to a non-existent variable you will get the error
message

??? Undefined function or variable...

The official MATLAB documentation refers to all variables as arrays, whether they are
single-valued (scalars) or multi-valued (vectors or matrices). In other words, a scalar is a
1x1 array, i.e. an array with a single row and a single column which, of course, is an array
of one item.

MATLAB is case sensitive. Many programmers write variable names in lowercase except
for the rst letter of the second and subsequent words, if the name consists of more than
one word run together. This style is known as camel caps.
Ex
millenniumBug, dayOfTheWeek

Others prefer to separate words with underscores.

There are some workspace commands that will be mentioned here.

clear: clears the workspace of all user-defined variables


Syntax
>> clear <Enter>

who: Displays all user-defined variables


Syntax
>> clear <Enter>
Your variables are:
balance interest rate
5
whos: Lists the size of each variable
Syntax
>> whos <Enter>

Name Size Bytes Class

balance 1x1 8 double array


interest 1x1 8 double array
rate 1x1 8 double array

The double means the variable is given in double precision floating point.

One or more variables can be cleared from the workspace

>> clear rate <Enter>


>> clear rate interest <Enter> (separated by spaces and not commas)

The function ans returns the value of the last expression evaluated but not assigned to a
variable.
Ex:
>> 2 + 3/4*5 <ENTER>
ans =
5.7500

The percentage sign (%) indicates that the part of a statement after it is a comment

Some commonly used constants can be grouped together in an M-file. The file could be
run at the beginning of each session to have their values available in the workspace of
that session.
Ex: Save the following statements as myconstants.m.

g = 9.8; % acceleration due to gravity


avo = 6.023e23; % Avogadros number
e = 2.718281828459045; % base of natural log
pi_4=pi/4;
log10e = log10( e );
bar_to_kP = 101.325; % atmospheres to kilopascals

If you run myconstants at the start of a session these six variables will be part of the
workspace and will be available for the rest of the session, or until you clear them.

6
7

You might also like