MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。 9 u$ X5 t' ~; T; Z' Y; V# w ; u, _" \% _2 e4 ?. |如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。5 c! `- ]* a8 }
; Z# H; y% R" z! F* |5 u0 J
例如, . C- O; [$ U9 i$ M+ K, l " L* x* ]. L' e: L( {2 wTotal = 422 Q' B. Z/ I5 U" X0 I
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 3 `" h6 l' r' G4 H! S7 x4 v0 P$ y2 b% X0 h& z1 _
MATLAB中可用的数据类型 6 r3 e H! y* ^3 m& [; z2 Q( x! X' x# b' n0 N9 s
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。9 Y4 G+ a, Z) O3 d p" P1 H: R
8 l# e; B/ a0 F8 g7 _* X2 B0 U+ y下表显示了在 MATLAB 中最常用的数据类型: 3 a4 s$ |" s# y* y) m0 |+ T 6 ^ E1 b; r7 L1 J数据类型 描述 & ~" m. X/ b, |6 Yint8 8-bit signed integer) j8 ]/ L U( G% d2 s8 ^! P
uint8 8-bit unsigned integer/ b3 m5 f2 d; o1 S; k7 S8 {! @) Q
int16 16-bit signed integer4 ^7 y7 h: d6 y* U
uint16 16-bit unsigned integer0 D: R! n9 Y' [) f
int32 32-bit signed integer ) H5 E- v# `; U4 D4 G; Ruint32 32-bit unsigned integer W4 j6 E* h; u8 J c2 ~- x
int64 64-bit signed integer3 `/ x$ Q; }1 A$ A N& i
uint64 64-bit unsigned integer 8 j6 R5 v$ R5 M" P" C0 \3 J8 k( |single single precision numerical data 8 O- ?7 a$ M' `* _double double precision numerical data 0 I( A# C* |, L _7 Q2 Ylogical logical values of 1 or 0, represent true and false respectively . @6 T, v' f5 N: cchar character data (strings are stored as vector of characters)) U3 l8 s% z5 n" N
cell array array of indexed cells, each capable of storing an array of a different dimension and data type ; d7 c7 P5 g0 ^# Y0 t. d0 tstructure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type0 t7 L% b( y8 J2 q9 L: Y1 y
function handle yiibaier to a function. e8 R* q4 N6 {& Q, X: L
user classes objects constructed from a user-defined class 9 g+ L% w' C3 Q& b' s2 n6 }: }java classes objects constructed from a Java class 5 f3 F" M/ {% J" o例子 * a2 H$ U/ ]2 F# d : z, n) t9 |; V8 t3 c$ V/ N) @创建一个脚本文件,用下面的代码: 3 T0 r& x* U* r% J0 N9 Z$ p # Y8 B }. \% ]+ \. g* x str = 'Hello World!'7 C( E8 I! x4 H- x1 A- o
n = 2345 $ y/ @% q+ i$ R" j+ n# vd = double(n) - ]" U, \3 |1 a' g, run = uint32(789.50)' P, j3 e! ?1 X! H" a/ l2 h& V
rn = 5678.92347 ( _- l @$ w- H! f1 k# Gc = int32(rn); W1 s7 T( c0 i
上面的代码编译和执行时,它会产生以下结果:. d4 p5 k0 d' N4 m6 C) `6 n
# s/ c+ g' D* M& n
str = + A6 u; V* ?. F$ B0 P" ]Hello World!( V' |# {4 z; p5 b9 a- _
n = 3 U* }3 s2 `3 ]( u9 ? 2345$ U0 B6 D j2 @& y4 \4 o$ B( x
d = ( e' y" c1 ]) s/ } c' r 2345 Q+ y) h$ E% W$ e
un = : E# H2 V6 H9 x9 s 790 8 k' N& P/ Q6 q- irn =& a6 G) X+ G- s) Z: l% F
5.6789e+03 r9 \1 ~4 U4 I6 G. ?! Gc =, d& \1 E; Q1 p* F
5679 0 ^4 A. B) _! ?& {数据类型转换 2 h8 d9 i* V6 J h/ K ) F7 M* j- e" \9 e" l1 A5 u$ G) tMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:& E- T' j' u% h# N8 N
3 u- }6 ]: K, B函数 目的/作用 ; g, j5 y0 v7 G6 }+ T9 ?& kchar Convert to character array (string) 1 W1 q: O% x' ~ h8 U v5 b/ M: N# `int2str Convert integer data to string" P7 _/ k% f; F
mat2str Convert matrix to string. ~' O% M" e7 u3 z8 _
num2str Convert number to string 3 N( J, Q0 j0 R( x- U. zstr2double Convert string to double-precision value 9 h7 Q% L/ F, A0 }2 V& wstr2num Convert string to number $ d$ H/ d4 g% l6 znative2unicode Convert numeric bytes to Unicode characters- B9 U% T9 a/ `# w
unicode2native Convert Unicode characters to numeric bytes" k# C! y9 C N7 ]0 B
base2dec Convert base N number string to decimal number 1 ~; c6 p |( L! w; {5 u, jbin2dec Convert binary number string to decimal number 9 {, o+ ~3 [" U3 p. J2 _- Kdec2base Convert decimal to base N number in string7 t E: ?: a* |: c
dec2bin Convert decimal to binary number in string. `9 b) |9 `+ c i
dec2hex Convert decimal to hexadecimal number in string9 s9 h( V% r- g( C; A, q
hex2dec Convert hexadecimal number string to decimal number. }- h/ \* c2 y m/ B6 P- r
hex2num Convert hexadecimal number string to double-precision number0 r. x/ ?; I7 V
num2hex Convert singles and doubles to IEEE hexadecimal strings H* z! {6 ~) m; X5 C+ J# O8 {
cell2mat Convert cell array to numeric array6 w) u" [% v- o
cell2struct Convert cell array to structure array; g% K9 i- i; `7 m1 I. _% f
cellstr Create cell array of strings from character array5 m2 T# d+ ~3 i, Z/ q0 f" ^2 K
mat2cell Convert array to cell array with potentially different sized cells; w$ G1 K" ^# M; z8 s
num2cell Convert array to cell array with consistently sized cells ( |$ F$ }( t" p: \' k4 Dstruct2cell Convert structure to cell array 2 y; z: G/ T" I/ w) b# I测定的数据类型 & Y6 d1 B7 G2 e7 p" c% y) F! i# ?+ t" n$ U5 h& w
MATLAB 提供各种函数标识数据类型的变量。 I% y. n) e. p
5 J4 v( V0 R) W5 }2 X! v下表提供了确定一个变量的数据类型的函数:+ B. I E) h, \! f3 ]
& y% f0 e4 _1 ~1 o: M" P5 |+ v函数 目的/作用/ a% n" ^0 ]. I9 B3 {
is Detect state! U+ p1 \( x- p- g1 X
isa Determine if input is object of specified class& W* G ~0 G" U$ w. P0 b
iscell Determine whether input is cell array ) H9 b a: @, A$ n. eiscellstr Determine whether input is cell array of strings 0 Y3 l+ t" U9 F8 Y" Q4 d6 u& n' e8 }ischar Determine whether item is character array) i1 T" v9 V! L$ c! A& c
isfield Determine whether input is structure array field ' @9 [. ?7 p! [8 o" j8 ^. Oisfloat Determine if input is floating-yiibai array( }6 Z/ ]# x+ _) Y$ A# D/ x% @, W- {
ishghandle True for Handle Graphics object handles0 J4 P( W+ N) J' X
isinteger Determine if input is integer array8 r7 V; d) W( c. b8 P8 j, P7 P
isjava Determine if input is Java object( H* T. {: t3 O- G0 J3 {
islogical Determine if input is logical array3 [ @1 V' j- x# R. z, L8 O: K
isnumeric Determine if input is numeric array ; ?7 y4 a x! j0 \. V% Tisobject Determine if input is MATLAB object* n6 h3 O/ _$ v( i$ f* q
isreal Check if input is real array$ `* R* ^4 \0 u1 d% Q& p
isscalar Determine whether input is scalar, n: E( c9 w, p0 J8 d
isstr Determine whether input is character array& D8 |1 G/ H5 B. Y
isstruct Determine whether input is structure array / e8 X2 V7 t. U7 {/ N* Tisvector Determine whether input is vector 7 P" A# B# k" }2 C! h6 Sclass Determine class of object ) w: v( s4 V5 ^4 p" M) D9 Mvalidateattributes Check validity of array4 p7 o" q# G; B2 ?8 D, I7 K/ W u) O
whos List variables in workspace, with sizes and types3 N9 W4 }0 @! b# B# {
例子 ' i: v. S5 R* F; A0 K X9 e$ `3 e& L
创建一个脚本文件,用下面的代码:8 o" S$ F* p; g; K* {; h
; x- q' h+ {* }$ l; B0 Nx = 3" ^5 Z T& ^0 M$ M+ _
isinteger(x)* w: Q3 w2 p" m- V- M/ Q1 v
isfloat(x) $ m% I8 N" _# |. w1 ~isvector(x); E+ [/ r( ~2 V: l: G! I
isscalar(x); \4 n z [+ }- o- e
isnumeric(x) 0 G% K0 b! X- U6 h9 ^2 N: }2 V: @/ V+ T# [5 z& U8 Z
x = 23.54 $ H) v( o: W' l4 _: t: jisinteger(x)9 [7 ~5 \$ }5 E0 o0 B
isfloat(x)9 q* l, l3 u: K' e6 h9 h( w
isvector(x)# K! O# `$ O9 d$ n& @
isscalar(x) 8 j& e; g, H0 Y! ~4 s+ Aisnumeric(x)- z E8 L I6 F* p
: N5 _7 y- N& i( S6 h% t
x = [1 2 3] ) j9 j o3 s# w" k' wisinteger(x)' u- {) x5 a+ b, \0 I. n# T% H4 ~( X. ?
isfloat(x) # A/ {% b, O9 h, w; t) T) b8 Cisvector(x)5 ]0 z1 p$ a- d" P2 V) v
isscalar(x) . F6 o1 @3 x2 N( T' n2 g! R0 C6 _1 M" U% U6 S1 C
x = 'Hello'4 v7 M( g2 ]* M9 K1 x
isinteger(x), d+ U6 J# E6 S; {; f, N0 X
isfloat(x) 0 a2 p$ M: [( e" C2 X) Misvector(x) ?. L$ [3 z) \' |- H
isscalar(x) * f* M; m) _& ]6 S$ g0 nisnumeric(x)- j7 l3 j# ]1 S9 c; N7 x
当运行该文件,它会产生以下结果:: J4 a) n9 k# B( w
9 B4 i$ R3 l# H$ z" i1 v1 l
x =% W) ?9 m1 A+ _; H5 `/ o3 n
38 G( \& b& X! K) T: Q/ y% K# L
ans =! P+ g6 Z1 R1 U% _7 w/ S; f; B
07 ^0 I7 V7 q) ]* D" z
ans =% \) l9 S) Z0 c& g+ o1 ] x; ?
1 " S9 _, z6 p0 u5 {) Y7 D6 V6 `ans =6 [- Q, w8 S" |3 s5 q" a" h( G) {. N+ ]
1$ D+ P9 c& B1 l! j+ z
ans = # {9 {# ]( f/ F( c5 S 1 - {. o, j- W* [+ I; Jans =4 g2 O; g5 o5 a1 h
14 h# d7 k9 B& @& c
x = : M; g) O$ V0 X! G1 ?5 y. g% M 23.54000 u& Q% l# x$ x. p, q$ x
ans = - H' Q5 Q, {7 [1 s R, Q$ C# x. S 0 $ c" T5 V. i$ }ans =+ N9 G( A" }/ h: H7 m
14 D e$ X5 @/ I" `
ans = 3 D# ?8 O; s0 _( G' \ 1 s; \# z5 @9 G+ q' }" _. n/ k
ans = 1 F9 T( l1 h! |0 J9 k1 n6 T; D% p/ X 1( c8 |/ o& s! r" P r6 x
ans = : m! Z3 G, M' Y* n, h9 B 1 8 N7 x6 z7 v% ?2 f0 e% Y# V7 Tx = 3 ?4 v0 X! t& X/ t. P0 y& o# U% v1 O; ? 1 2 3 ! h; r+ p2 w9 R- c" pans = ! e. j( p/ _2 S Z: \# L. M 0( c7 ]/ \1 c! U
ans = 7 j% d: H% m% `% ? 19 M. e9 ^3 u3 u0 b
ans =0 e# a. ]# d3 z7 `8 T
1" n5 S! t3 V5 |
ans =- k4 c' l1 [% j! e: l
0 / U0 S9 m/ a9 X; i, q- C- J. k% h* ax = 4 L) T- I1 T5 }3 N# IHello0 o" l2 C6 K# n5 n# v. B9 ^+ G" L2 J
ans = ! l; I9 `% c1 A6 @2 ?; ?' X 07 u, Z, K* _( _( G: ?3 u/ c
ans =- T% k! l+ b* l
0! q: o5 k, L# e& V1 h; l
ans = ; Q7 J4 `% S7 e+ A8 Q2 S- A 1 " d, {7 u5 Q2 i# b# \ans =$ [( l7 C% P8 I& n; v
06 c* v0 i! V# f6 K2 v1 W1 Z$ l
ans =- J' A$ c; y* m0 @, @
0, V( E# J: l& [5 Q: p