MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。# ~1 ?( t6 I: M" G$ y! m w( I
5 Q5 K# s. E6 }0 G' D
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。/ v9 p7 E7 x8 Y5 b q
( `! ? p% I$ P& Q% t
例如, 5 d4 R, [! {& X {. z7 R) I" D j; h, Y+ U j) t" c8 R4 R6 ]
Total = 42; z+ Q7 Q3 Q3 ^7 v2 a
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 . L- O3 D3 b$ q( g' }& R9 L: }" `1 L. [8 P5 a
MATLAB中可用的数据类型! x9 }( h/ L0 m0 e; U, `
2 o; `6 V! F/ C3 t( S! s
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。 ( r$ A2 s2 ]1 ?7 c+ N. S O) y: [. R0 O! K9 T+ P下表显示了在 MATLAB 中最常用的数据类型: / @$ k! `8 q: n$ b- b7 y4 H 0 u) m+ c5 j* `* E/ s数据类型 描述 G4 e4 }7 M. H, N j9 l! n, M2 \int8 8-bit signed integer 5 c; T2 q/ G& q& p2 E1 kuint8 8-bit unsigned integer2 e5 m. E- G2 c
int16 16-bit signed integer 7 { r2 J& N7 H8 z4 w V9 u" zuint16 16-bit unsigned integer & a) _2 A, P) p4 @* P/ fint32 32-bit signed integer( {0 Q* r0 i* {
uint32 32-bit unsigned integer9 l/ K; N! p4 `+ ]/ h
int64 64-bit signed integer8 D/ k. k: {1 U( Y; S& c- g u
uint64 64-bit unsigned integer+ f' ^' {1 L0 G0 B J z4 V# u. m% z% P
single single precision numerical data 9 T/ L J2 N3 h) @9 ?1 e* cdouble double precision numerical data 5 H0 I6 d" d, u% zlogical logical values of 1 or 0, represent true and false respectively ) R; U4 O! H; j8 t5 E$ Pchar character data (strings are stored as vector of characters) * m5 l0 _3 G- |$ o% tcell array array of indexed cells, each capable of storing an array of a different dimension and data type# h9 L# H/ Y& h& V: W; E
structure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type * [4 I7 O6 ~9 rfunction handle yiibaier to a function) I# Q; H9 [! g7 ]
user classes objects constructed from a user-defined class + W6 ^- l0 W/ F9 \java classes objects constructed from a Java class 2 S9 w4 j r$ d( ?6 M. @例子 6 D2 m- u( `1 y) }5 Y6 c0 b + s2 N5 W1 ~$ B9 H3 y( S. J" N创建一个脚本文件,用下面的代码:3 d2 d4 j5 T' b7 @$ u9 `& k0 Y B
3 w; I/ L% j" f; | str = 'Hello World!'6 C; R" s! B$ y& k
n = 2345, h8 O9 O+ E6 I( J& z1 }
d = double(n)% }; |- p& Z$ C/ k& h3 q
un = uint32(789.50)+ U& Q5 e: F1 q N* O! i3 B; n/ a1 T
rn = 5678.92347 ( v( s2 g* r2 u( b jc = int32(rn) ! d3 ]2 O% j7 Y5 f" _, k8 O2 D* H4 m上面的代码编译和执行时,它会产生以下结果: + s6 i& W7 \( p! \# Q' n/ p- H& i" H( v w- w
str = + V) d7 L, _: UHello World! : h- w' S% Q3 U0 B8 s) F/ on = 7 y* F d* g$ m* R* O 23457 w Z1 d; {8 {7 E' t
d = : O9 ^5 R( L, @ 2345) l) s+ A1 H- ]: f8 a6 d: k7 _3 [# X
un = - D* L( @' @" P/ u. m1 A) l% ~ 790/ G* r% b/ z5 J+ p0 J& E
rn =9 y1 ~* H4 V `1 l9 m# D# U
5.6789e+03 9 F ], q) _0 H) Y0 O2 \/ z$ Yc =8 W0 T! s' @3 B8 r- v7 T: E3 n4 f
5679: ^$ J8 Z: V2 }6 b. ~ `
数据类型转换 4 O" V2 v5 B+ m& P* P5 d" Q8 j& x% j5 w( M7 p% }
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:- V$ H0 r# Q3 U5 w
. \# d2 W- K+ a! c# n) U函数 目的/作用 ' y7 @; j: L# ^char Convert to character array (string) ! N0 t/ O$ T; i& P" Mint2str Convert integer data to string 3 d1 F' V0 c. Y$ x) Amat2str Convert matrix to string 2 p2 D8 e' E4 Ynum2str Convert number to string 7 x4 p* _1 t0 g( |: [6 ]9 hstr2double Convert string to double-precision value + Y) c) W) U L; D6 S* Z# C) X8 q9 jstr2num Convert string to number " M9 B5 f' K2 x+ T. |& \' o$ znative2unicode Convert numeric bytes to Unicode characters 8 A& J$ f# e# x; eunicode2native Convert Unicode characters to numeric bytes# y9 J7 W& w0 ~! V. C
base2dec Convert base N number string to decimal number$ s4 q6 @, H( ^
bin2dec Convert binary number string to decimal number, R) f8 N5 @1 Q4 d! [7 s. c
dec2base Convert decimal to base N number in string ; z" {8 {, O$ fdec2bin Convert decimal to binary number in string 3 n, D6 Q2 _+ h8 F5 F7 _dec2hex Convert decimal to hexadecimal number in string 9 d) z9 z# b: ^- ? F3 ahex2dec Convert hexadecimal number string to decimal number& t% l9 [2 v [; u8 h
hex2num Convert hexadecimal number string to double-precision number9 a! Z1 X% }# @# z% b' n
num2hex Convert singles and doubles to IEEE hexadecimal strings ( L5 x# b3 u# k$ @4 t1 [) x, Mcell2mat Convert cell array to numeric array ) D% J# C$ j. ]6 C3 K U' E; m( ycell2struct Convert cell array to structure array 3 Q) z1 x% r& K% G5 ecellstr Create cell array of strings from character array 6 K+ U6 ]! ^) E8 L! emat2cell Convert array to cell array with potentially different sized cells5 ~# R3 }6 \# T
num2cell Convert array to cell array with consistently sized cells , u( z# v3 T" {2 y& p8 V, Zstruct2cell Convert structure to cell array : e" B1 L6 t, Q" Y a测定的数据类型 ) k: j* d7 J) m; x" Q% p1 H. U4 k. r7 h ?! |2 n
MATLAB 提供各种函数标识数据类型的变量。4 ^- x; c. ?; u, @
+ V1 E# F J# H j9 K* l
下表提供了确定一个变量的数据类型的函数: ) r# E4 l D _1 x5 w l. b* j# `5 b 6 k \7 x& ^$ ~2 A9 g函数 目的/作用' X7 d+ y' P- W% F" H
is Detect state 2 X+ R6 ?& I# _5 F- G C% Cisa Determine if input is object of specified class, K: n \8 c3 l8 d
iscell Determine whether input is cell array- Q+ j6 W! C: i0 Y
iscellstr Determine whether input is cell array of strings 2 K+ j3 b5 T c' r8 k$ F0 I' ?ischar Determine whether item is character array ) o3 V: B8 l- L& y. ^& n! Z* L4 Uisfield Determine whether input is structure array field 4 o% V* U h7 sisfloat Determine if input is floating-yiibai array 0 [8 H0 M) V: K# f% yishghandle True for Handle Graphics object handles " @# a* t8 @* ~isinteger Determine if input is integer array1 ^% m5 g: |. ~7 @/ z+ N
isjava Determine if input is Java object' U- M: K5 i- U$ \" w2 h6 l' O- Z
islogical Determine if input is logical array 8 p) H, F6 a1 d9 O" f( g7 f0 Nisnumeric Determine if input is numeric array 7 P8 I1 t) O/ eisobject Determine if input is MATLAB object 5 S8 w. @9 U: ?5 d4 ~! Lisreal Check if input is real array # J, V4 Z$ J2 m* q" Disscalar Determine whether input is scalar) x3 \4 W0 w0 B& s
isstr Determine whether input is character array& H/ k2 w1 Z" J& D' x5 z
isstruct Determine whether input is structure array ; U+ K3 X7 U9 gisvector Determine whether input is vector7 a& C H) q' E* @
class Determine class of object ( W% Z: X2 t' x$ y! `/ svalidateattributes Check validity of array & z2 ]. {. ~4 U0 s1 Z; nwhos List variables in workspace, with sizes and types8 j$ \$ T! H" \; t5 v
例子7 U! ~" k# j' t5 J& [ C, r8 U