Documente Academic
Documente Profesional
Documente Cultură
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.
• 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=
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.
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
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=
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
You're right
Functions such as uint16 (Links to an external site.) convert characters to their numeric codes.
chrNumeric = uint16(chr)
chrNumeric =
Hello, world
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 =
• 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
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(isspace(chr))
ans =
5 9 15 26 29 34 44
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|