MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。 5 K3 N; J! l `# e, j $ p2 f* T+ P+ x+ n如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。# j5 y9 N7 H& Y8 J5 Y
- _3 v9 N4 P f- j4 E
例如, % b; G2 N2 g- ^7 ^9 x, p' ?0 _2 z- s; ?- E4 s3 U! q
Total = 42 , J5 P/ c% o4 X8 I上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 : E" P* S! \2 H$ @6 c5 s % |( n) [5 k5 @% K# sMATLAB中可用的数据类型( v- q8 I! C* r9 p2 A% w
z, z6 A. i! | f5 U' K
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。 5 ~, ]. X6 p" z" u& [: V8 H , [7 i8 W; y8 Y7 ]4 C下表显示了在 MATLAB 中最常用的数据类型:% ], W/ L8 {9 l* ~& B* @
4 q# Z8 b, y: ]: D/ F$ s& t3 N5 D
数据类型 描述 2 ~ _5 ~' B1 Y$ ?/ D9 Eint8 8-bit signed integer " A4 m: z& R1 r+ i# Zuint8 8-bit unsigned integer 5 j+ u1 T5 D$ X1 M3 Iint16 16-bit signed integer + V8 |) `5 n6 D5 R8 E6 q& V3 Guint16 16-bit unsigned integer0 X: M% D3 r% R4 F
int32 32-bit signed integer9 |* N" m. k' z8 Z
uint32 32-bit unsigned integer& D7 C) o, X1 C* p" P* Q8 P" A, M
int64 64-bit signed integer ; F: r& a1 a1 B+ @8 F7 X2 huint64 64-bit unsigned integer " W* o9 J* z# Rsingle single precision numerical data; t! k: b5 ?: L+ i% F
double double precision numerical data! g* k. z; M5 W' v: F
logical logical values of 1 or 0, represent true and false respectively* [5 C( y& u) m+ Y$ G- c1 A
char character data (strings are stored as vector of characters)! ~/ |* o$ B7 M
cell array array of indexed cells, each capable of storing an array of a different dimension and data type 9 u* C/ c4 p$ m$ ]3 Tstructure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type : m5 r) R, v) P* `9 Efunction handle yiibaier to a function6 Z# o! q" o+ m# X2 H8 ^
user classes objects constructed from a user-defined class/ C9 F6 E, p# ~ G' m) l& f
java classes objects constructed from a Java class0 ^4 K4 Z" a( H& j. d) t# j
例子% a `, M+ n/ M! K* [1 G; I
3 A7 F2 z: V" l. H创建一个脚本文件,用下面的代码: ' t) Y" R5 f- G/ z* Z& h' _2 \' z1 i2 h3 C+ M, [/ `
str = 'Hello World!' L3 ~# |! z) H( E2 f5 K; O+ g% zn = 2345/ Z- c: a- z& A) k1 Z: c+ ]) j
d = double(n) X# s# P& X! ^; @' _un = uint32(789.50) , j0 X0 @. p: n1 Crn = 5678.92347 @0 p* i: D0 B' V
c = int32(rn)1 ~( U6 L3 ~* T5 ~3 p
上面的代码编译和执行时,它会产生以下结果:: F1 F/ `/ \ X) g1 j. r5 `3 ^1 P0 X
& J- P5 C/ J# w1 L l. ~
str = p4 L) ~0 v, h3 {
Hello World!; y( D9 D0 N2 X" ?+ ^
n =4 n2 h: H) L: W6 h8 Y S: V: Y
2345 % t" w7 h: K2 X* o. jd = : Q9 C$ S2 H1 Q6 P 2345 . O% X; J6 `. F! D. ] Oun =7 |4 E0 }# f. w
790! p% G& C% }' F5 O
rn =0 [! d0 u! f4 X0 v4 |+ L& S/ q' K
5.6789e+03. m4 m2 m8 F/ v# g
c = 6 M) D0 z* e" q7 p 56790 E2 v4 @" F' K" m, |4 b6 a2 n
数据类型转换 I8 r$ z3 J* I2 u( W4 @4 E$ Y& m6 G
. i6 |! @8 e4 L6 D( MMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数: , J5 b3 g& _* J2 W3 ~+ n0 _( ?* H3 i% @2 m6 b! |" P: M
函数 目的/作用 7 J% t$ p& @) P& ~9 G* ?char Convert to character array (string) . M4 ^; v1 }; @int2str Convert integer data to string ' A$ E7 q1 u. a0 K7 `+ Dmat2str Convert matrix to string4 X3 i/ a* O5 ?* U% h5 F
num2str Convert number to string / D5 _" E$ n. A8 t- B! H; Cstr2double Convert string to double-precision value . _- {. w. G+ U2 u6 [' ?str2num Convert string to number4 h4 E' x$ X5 N) {& _0 z+ T
native2unicode Convert numeric bytes to Unicode characters3 `" o% O. O0 _# }0 Z6 w. D' V
unicode2native Convert Unicode characters to numeric bytes " ?2 A3 Y; y5 Rbase2dec Convert base N number string to decimal number% Y9 |& Y& n6 I9 {7 a
bin2dec Convert binary number string to decimal number# v. u: {5 o6 i7 d# a
dec2base Convert decimal to base N number in string & W/ a( U {; C6 \, i7 f. w1 bdec2bin Convert decimal to binary number in string 6 I, v" g* A$ ]8 k% n) ndec2hex Convert decimal to hexadecimal number in string9 N- e' Q7 Z1 }/ f9 C9 V
hex2dec Convert hexadecimal number string to decimal number) u; V. n8 d0 Y% g& U- h5 x# C4 x
hex2num Convert hexadecimal number string to double-precision number) N v# v3 S8 c+ o5 O( U
num2hex Convert singles and doubles to IEEE hexadecimal strings0 \7 e: \- {* ~3 h7 Z9 |/ J
cell2mat Convert cell array to numeric array- t7 L" V. q ]" v8 K0 q
cell2struct Convert cell array to structure array4 e3 T5 Z( d5 d2 ?8 V4 v) P7 w
cellstr Create cell array of strings from character array% @; |6 g2 X% o
mat2cell Convert array to cell array with potentially different sized cells3 \" I( e. t& R. P
num2cell Convert array to cell array with consistently sized cells $ X+ h. m! o# a9 E0 X, ]+ t+ lstruct2cell Convert structure to cell array + K" ~3 n, d7 P测定的数据类型* `% [9 ]9 ]& V/ o5 `
# G; W2 J- V z. G- O+ ^3 K8 K. tMATLAB 提供各种函数标识数据类型的变量。 : o# b6 m5 x5 P0 u " x8 d* G d: b0 D2 b下表提供了确定一个变量的数据类型的函数:0 R1 V7 [1 x3 g& N. b
- U9 C9 @3 j4 a& m( F, h
函数 目的/作用 & T9 {' M" }5 H) J- ~8 Tis Detect state # k. {5 U/ F% ]$ X7 q! s/ fisa Determine if input is object of specified class! b, q. o# t6 R* w
iscell Determine whether input is cell array7 \3 h/ c4 A3 y$ H( ^
iscellstr Determine whether input is cell array of strings5 ^ \, X7 o c* W5 }
ischar Determine whether item is character array9 c$ h+ v3 h- q# j0 m
isfield Determine whether input is structure array field n$ U5 A7 u% W# {) }( Q
isfloat Determine if input is floating-yiibai array 6 L$ A& @4 F, M7 K# zishghandle True for Handle Graphics object handles) b, `2 A- z" h# {- }$ E6 h* G+ ^( r
isinteger Determine if input is integer array. }- Y: z/ `! R
isjava Determine if input is Java object" B" n, U$ |( C' v ]- @" ~
islogical Determine if input is logical array8 R! K' g, n, Z8 k
isnumeric Determine if input is numeric array 8 ?; F2 j' t7 [isobject Determine if input is MATLAB object : T7 W# s5 J# \. risreal Check if input is real array/ x! w4 X9 q1 W* A
isscalar Determine whether input is scalar8 o7 a5 R+ t& h' b, J
isstr Determine whether input is character array8 P; n5 O. I+ E4 Z$ r
isstruct Determine whether input is structure array " C; C% i1 Q3 m/ U# @9 T7 wisvector Determine whether input is vector ; ]3 C G) L! k) z0 fclass Determine class of object e/ r/ k1 _0 D. j
validateattributes Check validity of array# z% ~6 C0 m& B; B9 N
whos List variables in workspace, with sizes and types . I$ C; u5 L# p* J例子$ m* f2 g3 B9 L! Y: G
$ c) `( ^, E8 Y6 f2 J/ {+ D
创建一个脚本文件,用下面的代码: ' M" L% }: n; @: ?/ }/ [9 I ) O7 B' q+ `2 \2 Cx = 3* N$ m8 _1 g$ f+ _- T8 r$ T( s' J0 ]
isinteger(x)+ A) X/ F/ `! D! I. I
isfloat(x)4 N% J; {/ \, Q
isvector(x) " k, s5 n0 G' g) X0 `3 ] Xisscalar(x) 4 W; n0 M6 K7 ]4 |* F; R9 pisnumeric(x) / u. o- |7 Y; ]) d' S) Q6 ^( C; W7 n
x = 23.54 ! T& n8 P1 h% \isinteger(x) ) i5 D) T! @3 d, `isfloat(x)" U6 q) }$ G0 |# q
isvector(x) 7 l$ O/ W. j) N) cisscalar(x) # b6 _% K. c. F; x0 Wisnumeric(x)3 u; t. }. o; t$ ?
9 d6 n, D- d4 M! Jx = [1 2 3]7 v3 G) a% L9 N6 Y. d: _, u
isinteger(x) 6 e) z3 v3 I) S1 |% N* p+ Y- C4 Tisfloat(x) : ]+ B2 q% D0 I7 X8 S0 Risvector(x)2 ?$ k5 I1 p" J
isscalar(x) " Q9 M6 h' g7 T4 f : ^3 C4 B+ Z/ v/ o( W, Y& vx = 'Hello' - l8 W: c6 J+ p' D$ Z3 Xisinteger(x)1 N: Y) u% C" @$ B# E) e
isfloat(x) # z* v7 j' b( s# U2 [$ Tisvector(x)# ~* `' l9 h0 G- a+ f! J
isscalar(x) : j9 C% ^) t# Z# N1 N, wisnumeric(x) / y% _7 T$ m$ ?% ?: `当运行该文件,它会产生以下结果:* V* L! m: a- C9 c
7 H; K G% Q" @& T+ e" H
x = # y: i7 _$ T9 N; q/ l 3 5 J8 c- A* E& ~, C6 y1 ?, mans =8 n ?% U1 H* h7 R6 d' [ [: q
06 N& N% H# E( i5 L6 `
ans = q2 y$ O: Y9 ?7 G' Y
1( ?$ V$ b0 P; m! @, x
ans = 3 S3 ~+ u. C& T 10 ]7 w6 Q: \: g. ~1 m" p
ans = a/ u8 ~ B6 G, c
12 L: B5 S- G2 E9 }! W- q
ans =. e- ?* H% ]" a/ z/ X% d
14 G( h0 u+ z* h4 f8 A5 M- N
x = 7 w/ {$ F4 B. @5 i 23.5400 ' o9 c3 u. o; @9 q# Fans =1 {: |! [1 }5 u W1 K# L1 b
0! b! V2 `9 M" s
ans =- z5 t! k) B" N; w/ p
13 A" y/ `4 M+ _: Q
ans = 3 w( b! O/ ]* R/ M3 a 1$ P: G7 B) I: ` M2 M9 v9 R7 {
ans = " G! I# o9 @* N9 j 1 - i+ C$ v, l7 k' t8 R2 ?$ Ians = , ~7 K7 k2 x6 {& C 1 & J* ]( \9 f* z6 w1 ~' Gx =8 i a; a8 Z9 @# {6 V& W v! L8 Q$ G
1 2 3 9 Z% ]$ X9 o. k6 X' eans =5 M {8 J$ H, ~( }
0: Z7 ^: y' C- z/ I' f; ~* N
ans =9 X1 N. x' E4 j/ ^ N6 b
18 e, |0 o$ b5 N) H. ?
ans =: I; N0 G; V) b. D
1. d& ?8 M; ^( o2 A( j* x
ans = & y* P2 v3 C* O: W 0- {/ ~1 o- _$ }$ u
x = / Q/ ]) O& ~- u( A$ f. J( bHello & `# r6 M3 o: w8 p# h3 T! m: d# k* gans = : l6 x" H% K' w& z* o1 [5 o) S 03 j9 A5 |9 t4 r+ k. o
ans =% b! v% `& K. t. Q, P
0 i l, o- t9 _! W- bans = 3 D$ _5 C, \3 L' p 13 r/ y5 M$ ?3 r: O2 n9 L9 Z" v
ans = 7 a) A7 h1 J; u" l 0 Y. U" I' ]( R& h3 g
ans = 4 ` A6 S: R# [' }1 P 0/ E( K/ z9 h' p4 {# U4 [7 H
0 V7 c& ~ w# V" x/ S" a2 k3 S
————————————————! z# ~. @' R$ m! m" l* U
版权声明:本文为CSDN博主「Phil__ming」的原创文章。 / g; T8 T1 U5 b/ b原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575 / F1 n1 S( M. g. D* m( k0 U) m4 O! }- ]# m) Q- H( i