Sei sulla pagina 1di 4

Helpful hints:

The Command Window


The Command Window is the main window in which you communicate with the MATLAB interpreter.
The MATLAB interpreter displays a prompt (>>) indicating that it is ready to accept commands from
you. For example, to enter a 3-by-3 matrix, you can type
A = [1 2 3; 4 5 6; 7 8 10]
When you press the Enter or Return key, MATLAB responds with
A=
123
456
7 8 10
To invert this matrix, enter
B = inv(A)
MATLAB responds with the result.

Command Line Editing


Various arrow and control keys on your keyboard allow you to recall, edit, and reuse commands you
have typed earlier. For example, suppose you mistakenly enter
rho = (1+ sqt(5))/2
You have misspelled sqrt. MATLAB responds with
Undefined function or variable 'sqt'.
Instead of retyping the entire line, simply press the key ⇑. The misspelled command is redisplayed.
Use the key ⇐ to move the cursor over and insert the missing r. Repeated use of the key ⇑ recalls
earlier lines.
The commands you enter during a MATLAB session are stored in a buffer. You can use smart recall
to recall a previous command whose first few characters you specify. For example, typing the letters
plo and pressing the key ⇑ recalls the last command that started with plo, as in the most recent plot
command.

Help and Online Documentation


There are several different ways to access online information about MATLAB
functions.
• The help command
• The help window
• The lookfor command
• The MATLAB Help Desk
• Printing online reference pages
• The MathWorks Web Site

The help Command


The help command is the most basic way to determine the syntax and behavior of a particular
function. Information is displayed directly in the command window. For example
help magic
displays
MAGIC Magic square.
MAGIC(N) is an N–by–N matrix constructed from
the integers 1 through N^2 with equal row,
column, and diagonal sums.
Produces valid magic squares for N = 1,3,4,5....
NOTE MATLAB online help entries use uppercase characters for the function and variable names to
make them stand out from the rest of the text. When typing function names, however, always use the
corresponding lowercase characters since MATLAB is case sensitive and all function names are
actually in lowercase.
All the MATLAB functions are organized into logical groups, and MATLAB’s directory structure is
based on this grouping. For instance, all the linear algebra functions reside in the matfun directory. To
list the names of all the functions in that directory, with a brief description of each:
help matfun
Matrix functions – numerical linear algebra.
Matrix analysis.
norm – Matrix or vector norm.
normest – Estimate the matrix 2–norm
...
The command
help
by itself, lists all the directories, with a description of the function category each represents:
matlab/general
matlab/ops
...

The Help Window


The MATLAB help window is available on PCs and Macintoshes by selecting the Help Window option
under the Help menu, or by clicking the question mark on the menu bar. It is also available on all
computers by typing
helpwin
To use the help window on a particular topic, type
helpwin topic
The help window gives you access to the same information as the help command, but the window
interface provides convenient links to other topics.

The lookfor Command


The lookfor command allows you to search for functions based on a keyword. It searches through the
first line of help text, which is known as the H1 line, for each MATLAB function, and returns the H1
lines containing a specified keyword. For example, MATLAB does not have a function named inverse.
So the response from
help inverse
is
inverse.m not found.
But
lookfor inverse
finds over a dozen matches. Depending on which toolboxes you have installed, you will find entries
like
INVHILB Inverse Hilbert matrix.
ACOSH Inverse hyperbolic cosine.
ERFINV Inverse of the error function.
INV Matrix inverse.
Adding –all to the lookfor command searches the entire help entry, not just the H1 line.

Matrices in MATLAB
The statements
u = [3; 1; 4]
v = [2 0 —1]
s=7
A = [1 2 3; 4 5 6; 7 8 10]
produce a column vector, a row vector, a scalar and a matrix.
u=
3
1
4
v=
2 0 —1
s=
7
A=
123
456
7 8 10
Colon :
The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays, and
specify for iterations. The colon operator uses the following rules to create regularly spaced vectors:
where i,j, and k are all scalars.
Below are the definitions that govern the use of the colon to pick out selected rows, columns, and
elements of vectors, matrices, and higher-dimensional arrays:
j:k is the same as [j,j+1,...,k]
j:k is empty if j > k
j:i:k is the same as [j,j+i,j+2i, ...,k]
j:i:k is empty if i > 0 and j > k or if i < 0 and j < k
A(:,j) is the j-th column of A
A(i,:) is the i-th row of A
A(:,:) is the equivalent two-dimensional array.
For matrices this is the same as A.
A(j:k) is A(j), A(j+1),...,A(k)
A(:,j:k) is A(:,j), A(:,j+1),...,A(:,k)
A(:,:,k) is the kth page of three-dimensional array A.
A(i,j,k,:) is a vector in four-dimensional array A. The vector includes
A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:) is all the elements of A, regarded as a single column. On the left side of an assignment
statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the
same number of elements as A.

Plots, Graphic
On the PC, you can import a MATLAB graphic into another application by copying the Figure to the
clipboard in Windows Bitmap or Windows Enhanced Metafile format, and then pasting the graphic
into the other application.
There are two ways to copy a Figure to the clipboard:
• Select the Copy Figure command from the Edit menu of the Figure window. The format of the
output is determined by preferences you can set.
• At the command line, use the print command with the –dbitmap or –dmeta option. Do not provide
a filename. The Figure will be copied to the clipboard as a Windows Bitmap or Metafile, depending
on which switch you use. You then import the graphic into another application by using the Paste
command.
Choosing the Format. The Windows Bitmap and Enhanced Metafile formats are fundamentally
different in the way they represent the Figure. The Bitmap format creates a bitmapped copy of the
Figure window, while the Metafile format uses a vectorized approach. In general, the bitmap format is
of lower resolution than the Metafile format.
The Windows Enhanced Metafile format is a device-independent format for sharing graphics between
Windows applications. This format is capable of producing high-quality graphics, and is the preferred
graphics format to use on Windows systems.

Application-Specific Issues
• Microsoft Word. When you import a graphic into a Microsoft Word document, first create a frame
in the document and import the graphic into it. Importing into a frame will enable you to reposition
the graphic by dragging it.
• Corel Draw. You can import Windows Enhanced Metafiles and PCX files into Corel Draw. Note
that the graphic appears to be black and white until you make the picture full screen.
• LaTeX. You can import a MATLAB Figure into LaTeX by creating an Encapsulated PostScript file.
The general syntax for including a PostScript figure in LaTeX is:
\begin{figure}[h]
\centerline{\psfig{figure=file.ps,height=height,angle=angle}
\caption{caption}
\end{figure}
You can specify the height in any LaTeX compatible dimension. To set the height to 3.5 inches, use
the command:
height=3.5in
You can use the angle command to rotate the graph. For instance, to rotate the graph 90 degrees,
use the command:
angle=90

Potrebbero piacerti anche