This article was reproduced from: http://hi.baidu.com/xmf6227/blog/item/97ca2ddf98f1b61f495403cb.html
There are 15 basic data types in MATLAB, mainly integer, floating point, logic, character, date and time, structure array, cell array and function handle, etc.
1, Integral type : (Int8;uint8;int16;uint16;int32;uint32;int64;uint64)
The Intmax (Class) and Intmin (class) functions return the maximum and minimum values for the type integer, such as Intmax (' int8 ') = 127;
2, floating point : (single;double)
Floating-point numbers: Realmax (' Double ') and Realmax (' single ') return the maximum value of double-precision floating-point and one-precision floating point respectively, realmin (' Double ') and Realmin (' a ') Returns the minimum value for double-precision floating-point and single-precision floats, respectively.
3. Logic: (Logical)
Logical: The following example is the application of a logical index in a matrix operation, which sets an element greater than 0.5 in the 5*5 matrix to 0:
A = rand (5);
A (a>0.5) = 0;
4, Character: (char)
The input characters in MATLAB need to use single quotation marks. The string is stored as a character array, with each element occupying an ASCII character. such as date character: datestring= ' 9/16/2001 ' is actually a 1 row 9-column vector. The length of the line string that makes up the matrix or vector must be the same. You can use the Char function to build a character array, using the Strcat function to concatenate characters.
For example, the command name = [' abc '; ' ABCD '] will trigger an error warning, because the length of the two strings is not equal, at this time can be obtained by means of NULL characters such as: name = [' abc '; ' ABCD '), the simpler approach is to use the Char function: char (' abc ', ' ABCD '), Matlab automatically fills the null character to make the length equal, so the column latitude of the string matrix is always equal to the number of characters of the longest string.
For example, size (char (' abc ', ' ABCD ')) returns the result [2,4], where the string ' abc ' actually exists ' abc ', at which point a character element in the matrix needs to be extracted, using the Deblank function to remove spaces such as name =char (' abc ', ' ABCD '); Deblank (Name (1,:)).
In addition, MATLAB provides a more flexible method of cell array, using the function Cellstr to convert a string array into a cell array:
Data= char (' abc ', ' ABCD ')
Length (data (1,:))->? 4
CDATA=CELLSTR (data)
Length (Cdata{1})->?3
Commonly used character manipulation functions
Blanks (n) returns n NULL characters
Deblank (s) removes null characters that are contained in the tail of a string
(string) to execute a string as a command
findstr (S1,S2) search string
Ischar (s) determines whether the string
Isletter (s) determine if the letter
Lower (s) Convert lowercase
Upper (s) Convert uppercase
strcmp (S1,S2) compare strings are the same
STRNCMP (S1,s2,n) compares the first n characters in a string
Strrep (S1,S2,S3) replaces character S2 in S1 with S3
5. Date and Time
MATLAB offers three date formats:
A date string such as ' 1996-10-02 ', the number of date series such as 729300 (January 1, 00 is 1), and a date vector such as 1996 10 2 0 0 0, followed by the day of the month and seconds.
Commonly used date manipulation functions
DATESTR (D,F) converts a date number to a string
Datenum (Str,f) converts a string to a date number
Datevec (str) Date string conversion vector
Weekday (d) calculating the number of weeks
Eomday (yr,mth) calculates the last day of the specified month
Calendar (str) returns the calendars matrix
Date vector for clock current date and time
Date Current-day string
Now the sequence number of the current date and time
6. Structure
A structure is an array that contains a named data container or field. A field in a structure can contain any data.
7. Building an array of structures
(1) Assignment method
The following assignment command produces an array of structures named patient that contains three fields:
Patient.name = ' John Doe ';
patient.billing = 127.00;
Patient.test = [79 75 73; 180 178 177.5; 220 210 205];
Enter patient in the command area to view structure information: Name: ' John Doe ' billing:127 test: [3x3 double]
Continue assignment to extend the array of structures: patient (2). Name = ' Ann Lane '; Patient (2). billing = 28.50; Patient (2). test = [68 70 68; 118 118 119; 172 170 169];
The structure array becomes [1 2] after the assignment.
(2) Construct the structure array: the basic form of struct function function is: Strarray = struct (' field1 ', Val1, ' Field2 ', val2, ...)
For example:
Weather (1) = struct (' temp ',, ' rainfall ', 0.0);
Weather (2) = struct (' temp ', ' rainfall ', ' 0.1 ');
Weather = Repmat (struct (' temp ',, ' rainfall ', 0.0), 1, 3);
Weather = struct (' temp ', {n-N, (), ' rainfall ', {0.2, 0.4, 0.0});
(3) Accessing structural data
The following are the legal structure array access commands:
Mypatients = Patient (1:2) Gets the substructure data mypatients (1) Accesses the structure data patient (2). Name accesses a specific field in the Structure data patient (3). Test (2,2) Accesses a specific field in the structure data (the field is an array) bills = [patient.billing] accesses multiple structures tests = {Patient (1:2). Test} Extract structure data into a cell array
Use the dynamic name of a structure field
Through Structname. (Expression_r_r_r) can give the structure field name and access the data. For example, the field named Expression_r_r_r, the struct named Structname, access the 7th row 1 to 25 column data can use the command: Structname. (Expression_r_r_r) (7,1:25).
For example, there is an array of student data structures for each week, and the information is set up in the following ways:
Testscores.wang.week (1:25) = ... [95 89 76 82 79 92 94 92 89 81 75 93 ... 85 84 83 86 85 90 82 82 84 79 96 88 98];
Testscores.chen.week (1:25) = ... [87 80 91 84 99 87 93 87 97 87 82 89 ... 86 82 90 98 75 79 92 84 90 93 84 78 81];
That is, the structure is named Testscores, and the fields are named after each student's name, Wang and Chen, each with an array of grade structures named week.
Calculates the average score for the given structure name, student name, and start-and-end number of weeks.
Enter edit avgscore.m in the command window and save the file after entering the following code:
function avg = Avgscore (struct,student, First, last) avg = SUM (struct. Student). Week (First:last))/(Last-first + 1);
Enter in the naming window: Avgscore (testscores, ' Chen ', 7, 22) calculates the average score of the 7th and 22nd week of the student's Chen Cong.
(4) Adding and removing structure fields
command [struct] (index). (field) To add or modify fields. such as patient (2). SSN = ' 000-00-0000 ' adds a field called SSN in the structure patient.
The Delete field uses the Rmfield function, such as Patient2 = Rmfield (patient, ' name ') to remove the name field and produce a new structure.
8. Cell array: (cell)
The cell array provides a storage mechanism for different types of data that can store arrays of any type and latitude.
The rules for accessing a cell array are the same as other arrays, except that you need to use curly braces {} to access, for example, a{2,5} to access the 2nd row and 5th column cells in array a of cells.
(1) Building a cell array: Assigning a value method
You can create a cell array directly using the curly brace identifier, such as:
A () = {[1 4 3; 0 5 8; 7 2 9]}; A (+) = {' ABCD '}; A (2,1) = {3+7i}; A (2,2) = {-PI:PI/10:PI};
The above command creates a 2*2 cell array. Continue adding cell elements directly using assignments such as a (2,3) ={5}, note that you need to use curly braces to identify them. The simplified method is to combine the use of curly braces (cell arrays) and square brackets () to create, such as C = {[1 2], [3 4]; [5 6], [7 8]};
(2) Building cell arrays: function methods
The cell function. such as: B = cell (2, 3); B (1,3) = {1:3};
(3) Access to data
The data elements in the cell array can be accessed directly through the index, for example: n{1,1} = [1 2; 4 5]; n{1,2} = ' Name '; n{2,1} = 2-4i; n{2,2} = 7; c = n{1,2} D = n{1,1} (2,2)
9. Function handle
A function handle is a MATLAB value or data type that is used to indirectly invoke a function. You can pass a function handle when calling other functions, or you can save a function handle in the data structure. by command Form fhandle = @functionname can create a function handle, such as [email protected], or an anonymous function Sqr = @ (x) x.^2;.
The function called with the handle is in the form Fhandle (arg1, arg2, ..., argn) or fhandle (no arguments). such as: Trigfun (1). Cases:
function x = Plotfhandle (fhandle, data) plot (data, Fhandle (data))
Plotfhandle (@sin,-PI:0.01:PI)
Data type conversions such as e.g in the C language are similar to forced type conversions.:
y=9;
Z=double (y);
Conversion of image data types in MATLABThe data type that is read into the image in MATLAB is uint8, and the data type used in the matrix is double so I2=im2double (I1): Converts the I1 of the image array to the double precision type, and if not converted, overflows when the uint8 is added to the The error that may be prompted is: Function ' * ' isn't defined for values of class ' uint8 '.
Image data type conversion function
by default, MATLAB stores the data in the image as double, which is a 64-bit floating-point number, and Matlab also supports unsigned integers (uint8 and uint16), while the UINT type has the advantage of saving space and converting it to a double type when it comes to operations. Im2double (): Converts an array of images to a double precision type im2uint8 (): Converts an array of images to UNIT8 type im2uint16 (): Converts an array of images into unit16 type.
MATLAB Basic data types