MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。: T+ Z& M, E- F% b
b# c8 ^, ]3 e6 G如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。/ j& E$ F9 U( f, {
) Q3 d5 f- v! Z6 @" R
例如,3 l" j& f1 b f; I7 i$ u
1 e. F- j3 e3 U) e6 g( y. t
Total = 42* ]% C' L; t! I( P8 L6 C
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 % r+ M! a ?/ [3 D# u( x( c' w7 r- x/ U9 Q( g! ^
MATLAB中可用的数据类型 $ k$ m. n9 ~, G6 S$ X0 ~ h1 U' O/ _
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。, y: m+ z- F; w
: H. u/ C0 i' v0 y$ b3 k下表显示了在 MATLAB 中最常用的数据类型: 1 }# |0 H) I- P* {; X3 Y 1 D# ?% Q' M$ i数据类型 描述 . z8 A" U& {+ b: w/ yint8 8-bit signed integer& ^3 `6 ~+ G- E' p
uint8 8-bit unsigned integer ! H. |) z$ X( S' g8 {( ] V2 bint16 16-bit signed integer8 A) \% ~2 o* T* n3 Y, H3 p/ d7 _7 k$ f
uint16 16-bit unsigned integer. Q6 x- c& x) ]$ [9 C, C. _/ g
int32 32-bit signed integer ' @6 K2 @- b4 o; N# guint32 32-bit unsigned integer. O+ w/ r! n) w# G! T' {
int64 64-bit signed integer w& U- {4 q+ ]) H+ @6 |
uint64 64-bit unsigned integer# b" S0 g; k ^( F* B
single single precision numerical data 3 Z* Z# j- T- m! h" n' W% [% bdouble double precision numerical data& z$ H/ Q% a7 r; \
logical logical values of 1 or 0, represent true and false respectively ! ^$ }" ^- Q( \2 ]. o Echar character data (strings are stored as vector of characters) l9 ~2 G. A( R7 ~
cell array array of indexed cells, each capable of storing an array of a different dimension and data type ' j! a3 ]. E, F$ F7 D# q8 istructure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type l8 r8 X7 M& g0 Afunction handle yiibaier to a function 2 t/ F' z! y+ muser classes objects constructed from a user-defined class : ~6 T8 j! o8 Njava classes objects constructed from a Java class! ?$ \8 f; x/ f- U. D. _5 @
例子5 ~. G: E3 f- }5 a, g
# H& ?4 M) Y+ c' h- \! X9 m
创建一个脚本文件,用下面的代码:1 J3 j! p; [1 b& F3 T
! j/ a' e& u' v. L8 l str = 'Hello World!'1 d! k8 z8 p N7 k" \/ V
n = 23456 y7 X- R/ U7 K: t/ Y s0 S/ r
d = double(n); w, C9 Q% p# v1 L) i
un = uint32(789.50)7 J9 k* A% c ~$ l; F6 d+ }* [4 J% v
rn = 5678.92347 7 t" o' g% j: ?2 Ic = int32(rn) # D0 T w% ~3 I4 {8 G! P* B上面的代码编译和执行时,它会产生以下结果: ( y# L9 z8 H" U3 u$ U - }. R3 n+ T2 F; ]& ]str =* j3 l( Z& t$ i* e7 m( \
Hello World! ' t. U6 L( |! ]* m& O, Rn =* L0 J; j; |$ [- N) V8 c1 w
2345- f& l( p! |3 W/ z3 V: Q% Y
d = F8 _" @" }) c9 v# e
2345 & T5 h8 x# a9 k( c/ m) f7 j. h Lun =& K( v; N: M8 s! V3 ]9 @# Z( i
7906 a, z. `# l# D7 L
rn =+ H( P3 I; g/ r) p
5.6789e+03* h0 n7 S; s* [/ f. j
c = - C' \3 `2 Q- I5 ? 5679 # D! |: a! |. E @; |# q" k数据类型转换8 h& K8 W+ e |. P+ h
& w7 q" o0 D7 `, l I4 {$ ]MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数: , R% y: j; o6 Q% e# }3 M2 n2 r2 j7 R3 D
函数 目的/作用. r, _* k6 i+ g6 P4 d% I3 a
char Convert to character array (string), X: u1 B' V2 ^7 k1 O
int2str Convert integer data to string" U/ p# K3 N( Y, z- k; T' d
mat2str Convert matrix to string3 v' V1 ^# p; u$ [
num2str Convert number to string1 _/ K0 x+ E; B" t( R/ L
str2double Convert string to double-precision value & d. G7 l% L- A0 W! Lstr2num Convert string to number# K8 L; E8 c0 S1 M' Q
native2unicode Convert numeric bytes to Unicode characters 1 L, A8 A2 x! V+ Tunicode2native Convert Unicode characters to numeric bytes ; W1 G' D) ]: P# }base2dec Convert base N number string to decimal number( {$ z4 y2 T |+ b) W/ ~
bin2dec Convert binary number string to decimal number + Z" m2 p2 I6 i5 `) Y# U9 v5 F h* {dec2base Convert decimal to base N number in string ' d0 V8 z% k: Xdec2bin Convert decimal to binary number in string) O0 u; T& P2 `; x
dec2hex Convert decimal to hexadecimal number in string; b2 r3 t3 L" P% v, D* v; [# F
hex2dec Convert hexadecimal number string to decimal number, \! Q, j" C% n8 `5 r- O
hex2num Convert hexadecimal number string to double-precision number ( N/ z+ c% y1 b1 N) unum2hex Convert singles and doubles to IEEE hexadecimal strings 0 G8 U( s& | Zcell2mat Convert cell array to numeric array, z4 p# z% S- ?# M* r
cell2struct Convert cell array to structure array. y$ X1 c- Z# E u1 T8 @; `7 H
cellstr Create cell array of strings from character array! N. G4 z" j8 M+ p9 f9 T: b2 ?
mat2cell Convert array to cell array with potentially different sized cells8 w2 m, e8 w" `/ Y% Y
num2cell Convert array to cell array with consistently sized cells . ?8 F9 k, z3 m! s7 b/ ]struct2cell Convert structure to cell array7 X; H& ]. P0 I) f0 u
测定的数据类型 ; S6 }9 a/ ] ~7 f _' K: w1 x4 @ R0 E 9 H* d7 _0 g- }/ ^6 xMATLAB 提供各种函数标识数据类型的变量。, K# h- _4 t: Y& s
. J1 j/ |/ \% Y0 V" E0 E4 p% u
下表提供了确定一个变量的数据类型的函数:. l# f& i# n, X& X! z( w' q6 F5 j
2 c2 ^* e h6 k& z7 @
函数 目的/作用 . [/ N! |" R U; m8 B% Nis Detect state* D$ d- H+ f' _+ {
isa Determine if input is object of specified class 9 f! @$ S6 H0 j& a2 o6 {iscell Determine whether input is cell array- T7 J7 p+ C8 \% ~! J7 [" h# U6 o
iscellstr Determine whether input is cell array of strings7 |0 O. j: m8 u& K$ x
ischar Determine whether item is character array " x" S& p- H6 B% W& [9 `isfield Determine whether input is structure array field . c. U: R! j( C) ^3 L- C. risfloat Determine if input is floating-yiibai array9 U; F/ d! h ]- ~$ G3 K; Y- J0 I
ishghandle True for Handle Graphics object handles- R: X; ~! ~. ^
isinteger Determine if input is integer array " G" }3 b. W2 |. Wisjava Determine if input is Java object6 F O N/ n, c! g& Q# H$ ?2 v
islogical Determine if input is logical array6 ?1 I, i" Y8 ~% h
isnumeric Determine if input is numeric array- O. w0 }) J( r& y4 H2 U
isobject Determine if input is MATLAB object ) ]8 X" e3 V& ?2 ^; x% T! ~isreal Check if input is real array % v6 n& J1 s) w w5 ]" x& kisscalar Determine whether input is scalar 3 {2 d6 B6 s5 S' Y9 d% Nisstr Determine whether input is character array0 C1 I: d9 _' s/ |4 w6 _8 U
isstruct Determine whether input is structure array% v+ J1 E. d7 U& Y" f
isvector Determine whether input is vector& V2 N8 e/ o; ?( G) b
class Determine class of object ! M" v5 B2 M/ D7 e- q7 `1 nvalidateattributes Check validity of array % i0 m; ?) w! M- f( Q) fwhos List variables in workspace, with sizes and types . G: t6 Z" ^$ R' G) @例子' V; k$ v; t3 c