MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。 : Z1 w" v7 L. Z T " B5 \, E% ?9 v- ~$ E, S# B如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。, i$ c3 y v% Q$ T
- Y$ I6 A: n4 w( ?例如, ' L7 m$ F, D) F% V 1 n+ U4 d$ ~* X* _8 bTotal = 42 5 P/ `# }" n6 k' {5 n7 D. ^9 w# b上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。0 Z/ \/ P1 ~6 N* P, b9 s) p
6 n1 `; m% z' q+ I7 U% j9 ^
MATLAB中可用的数据类型8 B! U) x: f5 @# D8 I
% S2 ?1 W7 z0 f
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。 4 o( m0 y/ q% F8 }) N5 Y% t) u' g {1 l4 t; f) h+ u" ?
下表显示了在 MATLAB 中最常用的数据类型: " J! l8 [. @' C- H# M; O9 U, ~- j . J* B) z1 v% H# _) l数据类型 描述9 o% P3 ^+ t0 v# j8 a
int8 8-bit signed integer: ^1 E: w% t+ @4 |1 D
uint8 8-bit unsigned integer 5 z$ @ U" A/ X, p) f4 S" zint16 16-bit signed integer * q) @. Z1 s, h9 ]uint16 16-bit unsigned integer - o. _7 q* J1 l* R% Z0 r3 uint32 32-bit signed integer% A* i& r, `$ f; K" ?1 A4 A
uint32 32-bit unsigned integer 6 K; \" B& B) K& h- }$ Lint64 64-bit signed integer . N6 w% K* E* X' C. F) T0 l$ Auint64 64-bit unsigned integer( J! ~1 S* |- R! p
single single precision numerical data `2 J; @( v- A9 ?4 K. G+ w
double double precision numerical data * }+ r% I; U, T T6 \8 ylogical logical values of 1 or 0, represent true and false respectively . M7 R' f/ e3 W2 F3 J) x! \char character data (strings are stored as vector of characters)" y! O5 { e" ?* J6 f9 X$ c
cell array array of indexed cells, each capable of storing an array of a different dimension and data type 9 o& t' y. H& u+ S0 Ostructure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type `0 U) C8 ~: Z5 H- C qfunction handle yiibaier to a function , Z+ i3 K* M9 m* R" o# fuser classes objects constructed from a user-defined class$ K5 X* J7 o- }/ u# a
java classes objects constructed from a Java class6 x- E* _& w; e+ ]) p8 b5 a
例子9 ^9 V$ y7 i% k" E Q' K
: s0 q/ |6 L1 i4 I( U1 T
创建一个脚本文件,用下面的代码:, ]2 i1 o2 k9 d/ L5 u# c m
, M+ R9 N! h, \+ Q% Z; z! T
str = 'Hello World!' & Z' _* e) r( k2 {# D$ on = 2345 - T u: ^; k: \* y/ ^$ Fd = double(n) * N& U: c. z" w- Run = uint32(789.50) - i2 s) L8 ^/ |; Krn = 5678.92347! G4 V. H4 Q- u n3 Z9 P, F1 S
c = int32(rn)" E6 S! o, [+ U& [% u' x' t
上面的代码编译和执行时,它会产生以下结果:' z F1 U0 @/ W7 _
& X+ o/ L4 m0 w( l6 [$ @5 [
str =# C* Z" M0 q% Q1 I8 ~0 K
Hello World!, S9 X& O, o8 q5 c* w' ]
n =4 l8 J# i3 _$ L
2345 2 ?2 y. }# @3 n. ed =! e6 G q7 z/ P
2345 ) V! W" f4 O; B4 N, i2 m, Run =& R% r* T8 V. f3 f, a/ ]; [
790 " _& W- ^5 b- _! N* Brn = A& g# }/ i- ]/ B/ v, m. P
5.6789e+03- r% Y* B. O8 h+ N7 p0 W+ F* f
c = 3 P7 D; L: V+ [ 5679' [$ y; G# m! Z6 G3 G, U8 d: N
数据类型转换+ X% r* f! u( L( }
# C- n0 T* w/ r6 g1 \; I8 L1 X+ UMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数: * s7 U* R# f0 d5 ]0 E; X- @9 w& @! @9 i
函数 目的/作用, p6 z' m8 q# P0 k: U% r# r( I
char Convert to character array (string)+ ]0 j8 A3 W" R! Z7 R( |
int2str Convert integer data to string + D& q6 Z \1 W3 fmat2str Convert matrix to string 4 E% o. W$ G) ^) W& h2 Gnum2str Convert number to string : g$ B3 b$ _5 @% ]. q8 c7 C) n$ P$ Ostr2double Convert string to double-precision value + J5 l; H& J2 s9 }: V# _( D) nstr2num Convert string to number( V4 B9 g/ z" M4 d: m/ r4 i
native2unicode Convert numeric bytes to Unicode characters: h7 m% }( e* h. j. ~
unicode2native Convert Unicode characters to numeric bytes . S6 O H- _0 l/ _) u% qbase2dec Convert base N number string to decimal number. S Z+ X: y% @
bin2dec Convert binary number string to decimal number + u( v* U) A* x4 idec2base Convert decimal to base N number in string , G0 K% P) \! _2 Y3 wdec2bin Convert decimal to binary number in string * {* P S1 S- g$ R, v/ \dec2hex Convert decimal to hexadecimal number in string4 Q) X: L* H5 Q9 F4 U1 s( B2 [
hex2dec Convert hexadecimal number string to decimal number7 y, g: ]/ p' I/ u% n/ Y* ? f9 E
hex2num Convert hexadecimal number string to double-precision number 5 \9 T# O+ C9 f& _num2hex Convert singles and doubles to IEEE hexadecimal strings" {4 ?6 z3 n; _, \7 L
cell2mat Convert cell array to numeric array 8 l6 z1 O- } E H! kcell2struct Convert cell array to structure array ' W! Q8 \- ^0 p, ]( K. Ucellstr Create cell array of strings from character array( h& |' t5 D9 O7 A6 y ^
mat2cell Convert array to cell array with potentially different sized cells$ A; P: U! Q y9 E6 c" K" ]+ L3 r
num2cell Convert array to cell array with consistently sized cells . m3 c$ H; f. k* Lstruct2cell Convert structure to cell array / L. l- _: Z* r, J2 C2 M* b$ s) F测定的数据类型 ' q b) b3 z3 r- k/ b- E0 X 2 P, u0 s2 B- D" {4 j. |" n6 dMATLAB 提供各种函数标识数据类型的变量。/ d8 X: G& H! B
, [8 b" B1 ^6 _
下表提供了确定一个变量的数据类型的函数: % ?- U8 s2 s% P; F& @; v; H, w4 ^0 a2 b9 a0 j8 L+ z
函数 目的/作用$ V& R* A2 c W% ?* u3 X+ z# N. Q
is Detect state& s7 b# @: I$ v
isa Determine if input is object of specified class" s- M" e6 {1 s) a2 `$ O
iscell Determine whether input is cell array ; |) s9 p7 D: R5 l# ~1 i6 J. discellstr Determine whether input is cell array of strings # ~5 H1 [/ l; Y: N5 Y) m8 p7 Dischar Determine whether item is character array / F& c5 v) c! k6 tisfield Determine whether input is structure array field+ N% O9 D9 I: q5 v
isfloat Determine if input is floating-yiibai array9 v* I' E' w% |' h6 Y
ishghandle True for Handle Graphics object handles. P7 Q: Q) ^9 X! n; H
isinteger Determine if input is integer array + ^% h2 E3 ?1 T4 gisjava Determine if input is Java object, R8 \+ s! L% n* T1 l1 M
islogical Determine if input is logical array $ E# w9 S6 D+ }; ]: Jisnumeric Determine if input is numeric array 1 V* S, C. f+ w/ ]0 }: J" Wisobject Determine if input is MATLAB object. h; B: ?) ? l0 l- }
isreal Check if input is real array 0 t2 {1 P. w# y: risscalar Determine whether input is scalar7 X% ?. z% s( ~' e
isstr Determine whether input is character array: ?8 M, \) i+ v
isstruct Determine whether input is structure array% T$ o3 |: ]) ?: Q0 v
isvector Determine whether input is vector( |1 E" x' g* r! {
class Determine class of object % H; h" Q0 D/ |; @0 Y6 Q3 | Y, Kvalidateattributes Check validity of array+ u! a7 b! |5 j7 ^
whos List variables in workspace, with sizes and types ( h( N6 A4 h }& \' p7 a9 R例子9 h* g" B% N* o/ x* j
; C: K6 C+ z/ H3 q创建一个脚本文件,用下面的代码:, `9 M0 U) Y# s
7 K+ m8 F \7 x1 e. {
x = 3! |. J# R$ k# C( \2 O* _% z
isinteger(x) # ~% x# |+ Z" y, s) g# s1 C% c& sisfloat(x)# t4 f* c* P, q. E+ E5 n7 G" g
isvector(x)1 K; M/ ~ x' D# a( s; ~& e
isscalar(x)9 Q1 n2 N! `8 k6 E7 J
isnumeric(x): M4 t7 s. k2 M5 {3 p
: l% I" s {0 c3 Hx = 23.54 0 e, [! v$ p8 |' j1 C" kisinteger(x)& }% {" ^1 L1 S8 o) ~
isfloat(x)8 f A- w6 A% w6 \, b0 ~+ e5 H/ @
isvector(x) ( f/ ]- u, H u+ w1 |$ |! @7 ^isscalar(x)6 c; x. i' R; {1 z# g
isnumeric(x)2 g$ l+ _* ?$ R" E: E9 d7 [
( l: c# P% @1 k: F
x = [1 2 3]) r9 G7 K7 R! U* D" L" R
isinteger(x)6 v' J' D; v% R
isfloat(x); Z+ J% B0 \6 x# X
isvector(x)1 j6 I m% n G/ W2 U" C" h
isscalar(x)% C! k" c1 |. P/ c E
' Y4 m& N" B M5 q% o& M
x = 'Hello' ! v; s7 ]; \3 D4 D. _6 A/ Disinteger(x)/ ^' M+ a/ k$ p! R0 ]
isfloat(x)( c! v w" H. }9 f; i
isvector(x); O& |* p' l8 x1 z. H$ Y
isscalar(x) 2 v. m6 s. j* misnumeric(x) & z# ]5 t) {2 \0 ?& Z当运行该文件,它会产生以下结果: ( W, L" b( D6 A7 J* a6 @ ( I& W) t H4 `, R9 h zx =! D# [: x' T( N
3! @" d: G6 D4 T: n, g' n
ans = & @, [; s) a& o& {. ^6 R3 _ 0 * f% }5 j" e: {5 E/ [ans = & O! S# {' C0 A- s! { 1 ' s# j3 m! G( b' m( Cans = 0 ]$ ?$ m6 a6 }5 d% G. D5 M f3 \ 1 % R# q9 E$ f( o4 w- Y7 Q7 z$ M) ]ans = 4 `1 I. A0 H2 Y$ H 1+ q6 c- p- S% p5 d( f+ l+ I. P1 f
ans = + k* [# K' ?. o 1! s. C8 K% w m% I/ ~+ h
x = $ ?7 i2 d7 h" @ 23.5400 : m# [/ S8 p8 p% @. }$ ~8 {ans = 5 h+ U2 B; N% T& J% Y; R6 J8 V 0 / |) ^6 n F- W' s! X1 ~) pans = 1 v5 _8 P: c9 v5 i# B1 s' S$ c 1+ j8 e0 s( B, l1 S( ~
ans =, k! o7 q! T) s3 `# g
1 0 B! x5 R! C# n' J; Fans =1 p, u( _" b' X7 v( `
1 ; p" Q; I" o/ j5 z6 Qans =# P4 V" b. C* y! N8 {6 W
1% h, h7 n& P, p" j+ v( u5 Z
x =+ o d$ O7 Y' h @* ` ^; j
1 2 3 4 s* \& \1 O7 _9 U: yans =7 b+ M. H7 f8 {+ T
0 , ^" |1 h2 k8 O8 L0 ~2 Fans = : D0 Y" L6 }6 D C* x" u 1, D+ t4 w1 V2 ^! S
ans =! k7 [3 y& S3 t" R- m
12 z; x" s7 h; x1 K. J
ans =* G& {, |0 S2 _3 I1 p
0% K, \4 B3 S8 J8 r" m
x =- x7 B0 D% q8 u( c
Hello ( d( ]9 C; V6 J6 c; kans = 6 O0 C" v, y$ }7 v, v8 b. E7 _ 0 * _$ T- E8 R* ~' K2 hans = , z0 [) G f0 } X N 0 ; g7 P- ^. }# P9 `" Jans = / q# a' X; |! b8 Y+ p, m 1" M2 j1 Q- n, b$ t" Q1 [# G$ n
ans =. e7 S3 m4 c$ W9 I4 y
0 4 f$ Q+ V# @; r2 p; E$ Zans =0 k( t* Y8 z. \
0 , N U, D e/ j+ H- \" J' R4 r [+ C6 E- m8 J, O4 o( c1 W————————————————* ?: S. Q) h) }4 I5 K& m
版权声明:本文为CSDN博主「Phil__ming」的原创文章。 8 a' n: p0 n7 e5 c: L9 [" ` g' O原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575 0 a# y/ S2 A W5 A: k$ B 4 u! E6 e3 m2 } ! n9 B: ?7 k- i