Sunteți pe pagina 1din 7

2.

1 Matlab Desktop Basics

MATLAB is a matrix-based computing environment. All of the data that you enter into MATLAB is stored
in the form of a matrix or a multidimensional array.

When you start MATLAB®, the desktop appears in its default layout.

The desktop includes these panels:

• Current Folder — Access your files.

• Command Window — Enter commands at the command line, indicated by the prompt (>>).

• Workspace — Explore data (variables) that you create or import from files.

• Command History — View or rerun commands that you entered at the command line.

As you work in MATLAB, you issue commands that create variables and call functions. For example,
create a variable named a by typing this statement at the command line:

a=1

MATLAB adds variable ‘a’ to the workspace and displays the result in the Command Window.

a=

Create a few more variables.

b=2

b=

c=a+b

c=

d = cos(a)

d=

0.5403

When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store
the results of your calculation.

sin(a)

ans =

0.8415
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display
of output in the Command Window.

e = a*b;

You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓. Press the arrow
keys either at an empty command line or after you type the first few characters of a command. For
example, to recall the command b = 2, type b, and then press the up-arrow key.

2.2 Vectors and Matrices


MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers
one at a time, MATLAB® is designed to operate primarily on whole matrices and arrays.

MATLAB is a matrix-based computing environment. All of the data that you enter into MATLAB is stored
in the form of a matrix or a multidimensional array. Even a single numeric value like 100 is stored as a
matrix (in this case, a matrix having dimensions 1-by-1):
A = 100;

whos A
Name Size Bytes Class

A 1x1 8 double array


Regardless of the class being used, whether it is numeric, character, or logical true or false data,
MATLAB stores this data in matrix (or array) form. For example, 'Hello World' is a 1-by-11 matrix of
individual character elements in MATLAB. You can also build matrices composed of more complex
classes, such as MATLAB structures and cell arrays.

Constructing a Simple Matrix


The simplest way to create a matrix in MATLAB is to use the matrix constructor operator, []. Create a
row in the matrix by entering elements (shown as E below) within the brackets. Separate each element
with a comma or space:
row = [E 1 , E 2 , ..., E m ] row = [E 1 E 2 ... E m ]
For example, to create a one row matrix, a vector, of five elements, type
A = [12 62 93 -8 22];
To start a new row, terminate the current row with a semicolon:
A = [row 1 ; row 2 ; ...; row n ]
This example constructs a 3 row, 5 column (or 3-by-5) matrix of numbers. Note that all rows must have
the same number of elements:
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
The square brackets operator constructs two-dimensional matrices only, (including 0-by-0, 1-by-1, and 1-
by-n matrices). To construct arrays of more than two dimensions, see Creating Multidimensional
Arrays (Links to an external site.).
For instructions on how to read or overwrite any matrix element, see Matrix Indexing (Links to an external
site.).
Entering Signed Numbers
When entering signed numbers into a matrix, make sure that the sign immediately precedes the numeric
value. Note that while the following two expressions are equivalent,
7 -2 +5 7 - 2 + 5
ans = ans =
10 10
the next two are not:
[7 -2 +5] [7 - 2 + 5]
ans = ans =
7 -2 5 10

Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1
column vector of zeros.

z = zeros(5,1)

z=

2.3 Create Character Vector


Characters like numeric data are also stored as arrays in MATLAB. A single character is stored
as a 1x1 char vector and a string of characters is stored as a 1xn row vector. Create a character
vector by enclosing a sequence of characters in single quotation marks.
chr = 'Hello, world';

Character vectors are 1-by-n arrays of type char. In computer programming, string is a frequently-used
term for a 1-by-n array of characters.
whos chr
Name Size Bytes Class Attributes

chr 1x12 24 char


If the text contains a single quotation mark, include two quotation marks when assigning the character
vector.
newChr = 'You''re right'
newChr =

You're right
Functions such as uint16 (Links to an external site.) convert characters to their numeric codes.
chrNumeric = uint16(chr)
chrNumeric =

72 101 108 108 111 44 32 119 111 114 108 100


The char (Links to an external site.) function converts the integer vector back to characters.
chrAlpha = char([72 101 108 108 111 44 32 119 111 114 108 100])
chrAlpha =

Hello, world

2.4 Create Rectangular Character Array


Character arrays are m-by-n arrays of characters, where m is not always 1. You can join two or more
character vectors together to create a character array. This is called concatenation and is explained for
numeric arrays in the section Concatenating Matrices (Links to an external site.). As with numeric
arrays, you can combine character arrays vertically or horizontally to create a new character array.

Alternatively, combine character vectors into a cell array (Links to an external site.). Cell arrays are
flexible containers that allow you to easily combine character vectors of varying length.
Combine Character Vectors Vertically
To combine character vectors into a two-dimensional character array, use square brackets or the
char (Links to an external site.) function.

• Apply the MATLAB® concatenation operator, []. Separate each row with a semicolon (;). Each row must
contain the same number of characters. For example, combine three character vectors of equal length:
• devTitle = ['Thomas R. Lee'; ...
• 'Sr. Developer'; ...
• 'SFTware Corp.'];
If the character vectors have different lengths, pad with space characters as needed. For example:
mgrTitle = ['Harold A. Jorgensen '; ...
'Assistant Project Manager'; ...
'SFTware Corp. '];

• Call the char function. If the character vectors have different lengths, char pads the shorter vectors with
trailing blanks so that each row has the same number of characters.
• mgrTitle = char('Harold A. Jorgensen', ...
'Assistant Project Manager', 'SFTware Corp.');
The char function creates a 3-by-25 character array mgrTitle.
Combining Character Vectors Horizontally
To combine character vectors into a single row vector, use square brackets or the strcat (Links to an
external site.) function.
• Apply the MATLAB concatenation operator, []. Separate the input character vectors with a comma or a
space. This method preserves any trailing spaces in the input arrays.
• name = 'Thomas R. Lee';
• title = 'Sr. Developer';
• company = 'SFTware Corp.';

fullName = [name ', ' title ', ' company]
MATLAB returns
fullName =

Thomas R. Lee, Sr. Developer, SFTware Corp.

• Call the concatenation function, strcat. This method removes trailing spaces in the inputs. For example,
combine character vectors to create a hypothetical email address.
• name = 'myname ';
• domain = 'mydomain ';
• ext = 'com ';

address = strcat(name, '@', domain, '.', ext)
MATLAB returns
address =

myname@mydomain.com

2.5 Identify Characters


Use any of the following functions to identify a character array, or certain characters in a character array.

Function Description
ischar (Links to an external site.) Determine whether the input is a character array
isletter (Links to an external site.) Find all alphabetic letters in the input character array
Function Description
isspace (Links to an external site.) Find all space characters in the input character array
isstrprop (Links to an external site.) Find all characters of a specific category

Find the spaces in a character vector.


chr = 'Find the space characters in this character vector';
% | | | | | | |
% 5 9 15 26 29 34 44

find(isspace(chr))
ans =

5 9 15 26 29 34 44

2.6 Working with Space Characters


The blanks (Links to an external site.) function creates a character vector of space characters. Create a
vector of 15 space characters.
chr = blanks(15)
chr =

To make the example more useful, append a '|' character to the beginning and end of the blank
character vector so that you can see the output.
['|' chr '|']
ans =

| |
Insert a few nonspace characters in the middle of the blank character vector.
chr(6:10) = 'AAAAA';
['|' chr '|']
ans =

| AAAAA |
You can justify the positioning of these characters to the left or right using the strjust (Links to an
external site.) function:
chrLeft = strjust(chr,'left');
['|' chrLeft '|']
ans =

|AAAAA |
chrRight = strjust(chr,'right');
['|' chrRight '|']
ans =

| AAAAA|
Remove all trailing space characters with deblank (Links to an external site.):
chrDeblank = deblank(chr);
['|' chrDeblank '|']
ans =

| AAAAA|
Remove all leading and trailing spaces with strtrim (Links to an external site.):
chrTrim = strtrim(chr);
['|' chrTrim '|']
ans =

|AAAAA|

S-ar putea să vă placă și