MATLAB Data Types
1.Numeric Types:
This section covers the following topics:
Integers, Floating-Point Numbers, Complex Numbers, Infinity and
Integers:
MATLAB has four signed and four unsigned integer data types.
Creating Integer Data:
Name Size Bytes Class
x 1x1 2 int16 array
Or you can use the class function if you want to assign the output as shown here: xType = class(x)
xType =
int16
Use the isinteger function if you just want to verify that x is an integer: isinteger(x)
ans =
1
: str = 'Hello World';
int8(str)
ans =
72 101 108 108 111 32 87 111 114 108 100
Floating-Point Numbers:
Double-Precision Floating Point
Single-Precision Floating Point
Creating Double-Precision Data:
Name Size Bytes Class
x 1x1 8 double array
Creating Single-Precision Data:
Name Size Bytes Class
x 1x1 4 single array
2.Logical Types:
The logical data type represents a logical true or false state using the numbers 1 and 0, respectively. Certain
For example, the statement (5 * 10) > 40 returns a logical true value.
Creating a Logical Array:
One way of creating an array of logicals is to just enter a true or false value for each element. The true function returns logical one; the false function returns logical zero:
x = [true, true, false, true, false];
3.Characters and Strings:
This section covers:
Creating Character Arrays:
name = 'Thomas R. Lee';
Name Size Bytes Class
name 1x13 26 char array
name = 'Thomas R. Lee';
title = ' Sr. Developer';
strcat(name,',',title)
ans =
Thomas R. Lee, Sr. Developer
Cell Arrays of Strings:
Converting to a Cell Array of Strings:
data = ['Allison Jones';'Development ';'
celldata = cellstr(data)
celldata =
'Allison Jones'
'Development'
'
length(celldata{3})
ans =
7
String Comparisons:
str1 = 'hello';
str2 = 'help';
C = strcmp(str1,str2)
C =
0
C = strncmp(str1, str2, 2)
C =
1
Converting from Numeric to String:
x = [77 65 84 76 65 66];
char(x)
ans =
MATLAB
Converting from String to Numeric:
name = 'Thomas R. Lee';
name = double(name)
name =
84 104 111 109 97 115 32 82 46 32 76 101 101
4.Dates and Times:
Current Date and Time:
date
ans =
02-Oct-1996
datestr(now)
ans =
02-Oct-1996 16:56:16
datestr(floor(now))
ans =
02-Oct-1996
5.Structures:
Building Structure Arrays:
patient.name = 'John Doe';
patient.billing = 127.00;
patient.test = [79 75 73; 180 178 177.5; 220 210 205];
Now entering patient
at the command line results in name: 'John Doe'
billing: 127
test: [3x3 double]
Accessing Data in Structure Arrays:
mypatients(1)
ans =
name: 'John Doe'
billing: 127
6.Cell Arrays:
Creating Cell Arrays:
A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};
A(1,2) = {'Anne Smith'};
A(2,1) = {3+7i};
A(2,2) = {-pi:pi/10:pi};
C = {[1 2], [3 4]; [5 6], [7 8]};
7.Function Handles:
Constructing and Invoking a Function Handle:
fhandle = @functionname
Calling a Function Using Its Handle
fhandle(arg1, arg2, ..., argn)
fhandle()
0 comments:
Post a Comment