Chapter 1 data types of Matlab syntax

Posted by nitromaster on Wed, 05 Jan 2022 18:57:00 +0100

(1.1) common functions

clc; %Clear screen
clear; %Clear workspace variables
%Output display settings
format short; %Keep four decimal places(default)
format short e; %5 Significant digit plus 3-digit index
format long; %Display 16 significant digits
format long e; %16 Significant digit plus 3-digit index
format bank; %Keep 2 decimal places
format +; %Only positive and negative are given
format rational; %Expressed as a fraction
format hex; %Expressed as a hexadecimal number
%Predefined variables 
ans; %The default variable name that holds the result of the last operation
pi; %PI
NaN; %Non number
inf; %Greater than 2^1024 Number of
eps; %Less than 2.2204e-16 Number of
realmax; %Maximum positive real number
realmin; %Minimum positive real number
nargin; %Number of function input parameters
nargout; %Number of function output parameters
varargin; %Variable number of function input parameters
varargout; %Variable number of function output parameters
floor(x); %Round down
ceil(x); %Round up
fix(x); %Round to 0
round(x); %Rounding
rat(x); %take x Expressed in fractions
sign(x); %x<0 Time,Value is-1;x=0 Time,The value is 0; x>0 Time,The value is 1
rem(x,y); %seek x divide y Remainder of
gcd(x,y); %Find integer x and y Maximum common factor of
lcm(x,y); %Find integer x and y Least common multiple of
abs(x); %Find the absolute value of real number, the modulus of complex number and the modulus of string ASCII Code value
sqrt(x); %Open square
exp(x); %Exponent Based on natural logarithm
pow2(x); %Base 2 index
log(x); %Natural logarithm
log2(x); %Base 2 logarithm
log10(x); %Common logarithm
sin(x); %Sine function
csc(x); %Cosecant function
cos(x); %cosine function
sec(x); %Secant function
tan(x); %Tangent function
cot(x); %Cotangent function
asin(x); %Inverse sine function
acsc(x); %Inverse cosecant function
acos(x); %Inverse cosine function
asec(x); %Arctangent function
atan(x); %Arctangent function
acot(x); %Inverse cotangent function
sinh(x); %Hyperbolic sine function
csch(x); %Hyperbolic cosecant function
cosh(x); %Hyperbolic cosine function
sech(x); %Hyperbolic secant function
tanh(x); %Hyperbolic tangent function
coth(x); %hyperbolic cotangent 
asinh(x); %Inverse hyperbolic sine function
acsch(x); %Inverse hyperbolic cosecant function
acosh(x); %Inverse hyperbolic cosine function
asech(x); %Inverse hyperbolic secant function
atanh(x); %Inverse hyperbolic tangent function
acoth(x); %Inverse hyperbolic cotangent function

(1.2) numerical type

(1) Signed integer: int8, int16, int32, int64
(2) Unsigned integers: uint8, uint16, uint32, uint64
(3) Single precision floating point number: single
(4) double precision floating point number (system default): double

(1.3) character (string) type

(1) Characters and strings are not distinguished. They are represented by char and enclosed in single quotation marks.
(2) Starting with MATLAB R2017a, you can create strings in double quotation marks.
(3) A single character is stored according to Unicode encoding, and each character occupies two bytes.
(4) Matlab calculates the string according to the coded value of the character.
(5) Multiple strings can form a character matrix, and the number of characters in each line of the matrix must be equal.

strcat(str1,str2); %String horizontal connection
strvcat(str1,str2); %String vertical connection
strcmp(str1,str2); %string comparison
strncmp(str1,str2,n); %Before string n Character comparison
strncmpi(str1,str2,n); %Compares the first two strings case insensitive n Characters
strfind(text,pattern); %stay text Find in pattern Character or string that returns the index of the first identical character
strrep(s,s1,s2); %String s Substring in s1 Replace all with s2
deblank(s); %Remove Spaces 
disp(s); %Display string
size(s); %Gets the length of the string
double(s); %String as ASCII Code display
char(a); %take ASCII The code is displayed as a string
isspace(s); %Judge each character in the string. If it is a space, it returns 1, otherwise it returns 0
isletter(s); %Judge each character in the string. If it is a letter, it returns 1, otherwise it returns 0
upper(s); %Converts a string to uppercase letters
lower(s); %Converts a string to lowercase letters
eval('expression'); %Execute in string Matlab instructions

(1.4) plural

(1) Matlab uses i or j to represent imaginary units.
(2) It is customary to define imaginary units i = sqrt(-1)

complex(a,b); %with a Is the real part, b Create complex numbers for imaginary parts
real(z); %Take complex number z Real part of
imag(z); %Take complex number z Imaginary part of
abs(z); %Take complex number z Module of
angle(z); %Take complex number z Radial angle of
conj(z); %Take complex number z Conjugate complex of

(1.5) logic type

(1) In Matlab, 1 represents logical truth and is represented by function true; 0 represents logical false, represented by function false.
(2) The function logical() can be used to convert numerical type into logical type, any non-zero value into logical true, and the value 0 into logical false.

(1.6) function handle

h = @cos;

(1) The data type of the function handle is function_handle, created by the @ symbol.
(2) After creating the cos handle, you can use h to call cos indirectly.

(1.7) cell array

%The first creation method
c1 = {'Aiden Lee',19;[100 137 140 279],'XJTU'};
%The second creation method
c2 = cell(2,2);
c2{1,1} = 'Aiden Lee';
c2{1,2} = 19;
c2{2,1} = [100 137 140 279];
c2{2,2} = 'XJTU';
num2cell(m); %Convert matrix to cell array
cell2struct(c); %Convert a cell array to a structure

(1.8) structure

s = struct('name',{'Aiden Lee'},'age',{18},'score',{[100 137 140 279]},'school',{'XJTU'});
s1 = rmfield(s,'name'); %delete name member
s2 = rmfield(s,{'name','school'}); %delete name and school member
isstruct(s); %Judge whether it is a structure
isfield(f); %Judge whether it is a member variable of the structure
clc;clear;
s = struct('one',1,'two',2,'three',3,'four',4);
fieldnames(s) %Gets the member variable name of the structure
orderfields(s) %Sort member variables alphabetically
>>
ans =
    4×1 cell array
        {'one'  }
        {'two'  }
        {'three'}
        {'four' }
ans = 
    With the following fields struct:
        four: 4
        one: 1
        three: 3
        two: 2

ginseng Test Endowment material come source Reference sources Reference sources

  1. Scientific computing and MATLAB language Liu Weiguo Cai Xuhui Lugley He Xiaoxian China University MOOC
  2. MATLAB software and basic mathematics experiment Li Changqin Zhu Xu Wang Yongmao Ji Wanxin Xi'an Jiaotong University Press
  3. MATLAB R2018a complete self-study all-in-one Liu Hao Han Jing Electronic Industry Press
  4. MATLAB engineering and scientific drawing Zhou Bo Xue Shifeng Tsinghua University Press
  5. Matlab tutorial - image processing (Lesson 1) The first moon lights lanterns https://www.bilibili.com.
  6. Matlab tutorial - image processing (lesson 2: where is meow) The first moon lights lanterns https://www.bilibili.com.
  7. MATLAB from entry to baldness Goodall https://www.bilibili.com.
  8. Experimental course of automatic control principle Giant forest warehouse Xi'an Jiaotong University Press

Bo passenger create do : A i d e n   L e e Blog creation: Aiden\ Lee Blog creation: Aiden Lee
Special statement: the article is for learning reference only. Please indicate the source for reprint. Embezzlement is strictly prohibited!

Topics: MATLAB