MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。 : Q' k) Y4 S% z" p, w9 A7 m5 R 5 [# d( W. n7 Z* E0 y! H如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。; f# M7 ]7 @/ j. @4 p- g
9 Z: A: j- s# ]. K% P1 d3 q5 y; F例如, 7 R( e" H9 J& |+ B& i ! X( p& S( e% @5 {* o/ P% kTotal = 42 2 @8 R" T R, ^& M; ^上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 Y( Y3 L+ l# h
j) r0 b- y6 c% f& Y; s
MATLAB中可用的数据类型' c, X+ @+ g+ T
' x% s5 i2 o, i! M9 z8 D( q
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。9 P1 Z4 d6 Y8 J% D2 D, x
7 W( \1 G) [3 _4 O- q/ P9 d
下表显示了在 MATLAB 中最常用的数据类型:/ z* d% R: `- w
; Z7 w$ g$ T- G" D
数据类型 描述( X3 v0 P8 {4 d; Q) t4 d+ _
int8 8-bit signed integer' v' Q. E9 ?4 v
uint8 8-bit unsigned integer) y" ^7 I6 k6 c4 e
int16 16-bit signed integer 7 H2 y; P( b J+ fuint16 16-bit unsigned integer 5 t( {7 j* g! x+ Tint32 32-bit signed integer, n8 r/ M3 D: q0 f7 y
uint32 32-bit unsigned integer2 F% R3 `) l! g. Y9 L) w: _1 l* e
int64 64-bit signed integer ; M8 e. u, ]! M. B y iuint64 64-bit unsigned integer # d6 D0 m# r# F6 ssingle single precision numerical data, ?5 G4 z; w8 p% |; d
double double precision numerical data+ ^4 a3 A( j: O$ f& t( K
logical logical values of 1 or 0, represent true and false respectively; U, V& k# h$ M8 s* t7 y' b& [
char character data (strings are stored as vector of characters), V9 L \! G# R% f
cell array array of indexed cells, each capable of storing an array of a different dimension and data type 7 q' h0 m4 W" t5 j% X1 N! [2 d, Rstructure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type 1 Y0 E! q0 l0 R' o' `function handle yiibaier to a function( b: v! M$ S' M7 O5 u6 D
user classes objects constructed from a user-defined class ' _4 v' e$ ]& F$ u- ejava classes objects constructed from a Java class, P$ N) v# S$ }. I7 t6 X6 H
例子' z8 c7 S, X& s' m r
% f8 U- R) ?3 {/ c. l8 Y创建一个脚本文件,用下面的代码: K* f1 A5 z0 y; T
4 m2 D8 K+ t! }$ i
str = 'Hello World!'( ^5 ~! v+ G# E9 H; V* ]' y, J6 e$ G: I) _
n = 2345 % z, s( g9 S9 N% D4 A6 Fd = double(n)- x% x" H9 J! L' X$ `/ ]3 `1 H/ q
un = uint32(789.50) 1 O4 c+ Z4 G% s0 n. O2 drn = 5678.92347 " {0 D3 s5 e/ \+ B/ `$ n, d2 {c = int32(rn)$ h0 f+ }+ S7 |7 M" v
上面的代码编译和执行时,它会产生以下结果: ) [* D; ~' R7 m0 A$ a4 r- b p/ f, y& R& \- t) _
str =) \' C! a3 z- G: Y) n9 o4 M
Hello World! " F. c% D: ~- z& J! `n = 1 v5 b& a/ V9 _; f8 F; T8 ? 23457 c3 I) u& \. E+ t+ W5 b
d = % T5 ~7 T' H- j$ S, Y. w 2345 ( v& A/ u6 ~( }un =* Q0 O8 d. J) y/ b' ] |7 ]
7906 a0 d& G8 C* L6 c; @0 Q
rn =6 V* V2 O! H# \0 \' {% s
5.6789e+03 ! d5 |/ \2 h1 d, F4 c% d" S! w9 E# H0 yc =6 \3 E4 h3 ~" y+ U
5679- `/ h( m( p8 S1 W. H
数据类型转换! ~% U( s( E7 o& V" j
: Q" k3 @5 Q# ^3 |2 ?8 V: ?
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:; C6 p6 h3 X5 u6 c; K1 ?
- D/ t2 m" L. {$ l! }
函数 目的/作用9 ?% v9 ?( L# }
char Convert to character array (string) 2 I9 _2 h( F5 l! g, q# Oint2str Convert integer data to string( [6 r! ~+ {& m" |+ V- x1 [
mat2str Convert matrix to string) |1 i" ^" {: W; q) n* N, d6 V
num2str Convert number to string 0 U+ B I, x5 i9 Ustr2double Convert string to double-precision value7 a" ^2 p w4 O0 d6 Z
str2num Convert string to number3 C# o: V! D- M" k
native2unicode Convert numeric bytes to Unicode characters + B; q- P6 S- Uunicode2native Convert Unicode characters to numeric bytes + o% J. z3 p/ K6 \5 }& h1 j3 hbase2dec Convert base N number string to decimal number ' p1 \* A) B2 \7 Zbin2dec Convert binary number string to decimal number & R! Y! s3 [* r! C udec2base Convert decimal to base N number in string1 \; L+ n# e9 l( W) T6 ]( f/ o
dec2bin Convert decimal to binary number in string ) H+ N! L a5 w/ p; Fdec2hex Convert decimal to hexadecimal number in string $ c+ M4 }& `# b3 z* Ahex2dec Convert hexadecimal number string to decimal number" @2 [, x7 b0 L8 _
hex2num Convert hexadecimal number string to double-precision number 8 \4 j9 R: h$ O8 u# g; Lnum2hex Convert singles and doubles to IEEE hexadecimal strings' G! c1 a1 Y$ ?( m! d: L
cell2mat Convert cell array to numeric array" r( D4 \ `9 ?# J9 d1 | U. Q" C
cell2struct Convert cell array to structure array% ~6 G6 i/ s' g3 c) N) _3 S
cellstr Create cell array of strings from character array $ M; {4 j Z) [4 s$ r% cmat2cell Convert array to cell array with potentially different sized cells1 J7 M# p S X5 U! d5 o2 `, e
num2cell Convert array to cell array with consistently sized cells' L) \: W/ i- v. ^
struct2cell Convert structure to cell array* {( i* k& r I' y
测定的数据类型$ u U( y1 S" v) Z9 W0 f& {
( m% v4 C! W# ^4 k) E/ ~, a
MATLAB 提供各种函数标识数据类型的变量。 3 }0 {: L1 Z) w4 {$ P6 b7 Y1 B: h; j# P; r4 W2 d4 W b( Q$ C
下表提供了确定一个变量的数据类型的函数: % G- q6 t0 u+ U/ Y) e) |, j$ G : X7 ~! @6 I% j2 T/ s函数 目的/作用$ s G X- u: P" X+ t
is Detect state # R% }$ f+ u1 M L* Z) a, w. eisa Determine if input is object of specified class a/ R" Z- Z' |, c8 q7 a, ^
iscell Determine whether input is cell array 0 T. K" H, y; L, s. viscellstr Determine whether input is cell array of strings4 d- v/ ^5 I9 K+ u
ischar Determine whether item is character array: }- q" u. x+ `' @8 W
isfield Determine whether input is structure array field : _- |9 j7 Q, F& k2 J7 a$ v# |- Lisfloat Determine if input is floating-yiibai array 9 w% v( |3 R2 B& d1 o$ ~5 hishghandle True for Handle Graphics object handles , ~/ O& y" T" m7 k) G5 ^& iisinteger Determine if input is integer array: N4 ^: Q$ A N ]
isjava Determine if input is Java object ; z- D( f- l4 H$ `0 b/ w3 z& sislogical Determine if input is logical array) P, W! @9 A Y2 H1 E; Q
isnumeric Determine if input is numeric array + y8 y; a( N; O8 \9 Bisobject Determine if input is MATLAB object0 ~+ W4 U u3 O4 w+ y. Z
isreal Check if input is real array 5 ^8 C% D1 t" d9 _' y1 ?. fisscalar Determine whether input is scalar ) i- |1 d4 V0 Q( Xisstr Determine whether input is character array $ l: T- z& d5 V: nisstruct Determine whether input is structure array; I0 n5 Z: U: j& h; t( `( n
isvector Determine whether input is vector % q6 I8 S# Q- v ?; M3 A7 n, \class Determine class of object 3 {1 i6 I4 t5 M% B% yvalidateattributes Check validity of array6 B3 F' ?5 \0 M/ }6 S$ u
whos List variables in workspace, with sizes and types 2 E2 @ \# g1 z; g# w% B例子 : @" Q& e; [2 d& n 6 o7 t2 ]2 J4 Y2 [4 ^创建一个脚本文件,用下面的代码: 2 V! D% m6 Y7 ~# a/ J + s$ {# ]# A# y$ c9 J% Gx = 3" T! J$ K- f/ S: q
isinteger(x) : R3 {1 s& G! z+ |3 D( @7 uisfloat(x)0 }! m& N Z5 r! m
isvector(x)* X# p8 j& r! a) h% D+ U9 i6 R6 U8 a- g
isscalar(x) ; V ?# Z+ ^) H1 [+ Risnumeric(x)8 T2 ]* }/ J5 \# j
4 L3 n; M% s% |& Z
x = 23.54 p2 q/ ~0 g) L+ U9 h6 |( Gisinteger(x)2 p, B7 X H! [
isfloat(x) 8 J3 H7 ?% y8 g* H, [8 @isvector(x)$ H& \+ D0 i. _+ P/ _/ k! c0 u
isscalar(x)1 a' @. t% R6 n+ _
isnumeric(x) 1 U: P: p4 q* o" z8 m5 j, G6 ]4 h) Y& ^9 B- C
x = [1 2 3], A; H' J9 w8 P( k4 d$ B1 e5 m
isinteger(x)- x" P/ }, q% o. p9 V4 h( l( a; O
isfloat(x) , N% ?2 b5 r5 }0 d5 n, Uisvector(x) 0 g7 z" E+ N( C1 Q4 z- W- ^isscalar(x)0 X4 n7 P% ]7 Q+ z z+ F
( C* l! l" t, A0 [0 C8 v
x = 'Hello'7 Y9 a0 A3 i, H9 ] y$ C
isinteger(x)- h- J8 d! L" t- H
isfloat(x)3 x5 I! }& ~7 h( Z6 j
isvector(x) 8 v/ d! n3 o, W, S! ^% ~6 Tisscalar(x) ; b# H8 ~! A$ z9 N3 f. Eisnumeric(x)$ J+ f5 c" Q$ P, f; p8 @1 \
当运行该文件,它会产生以下结果: 4 I) O; @- @$ q' O8 S5 h4 Z$ K3 w* W1 e
x = ( Y4 }+ x4 }7 O' j7 d0 k( B, K 3" H8 Y( J) d a. F! i" c3 Z
ans = ; |% Q$ L& N4 ]- Y 0 - I5 |6 P1 p+ H+ R/ L( R7 Bans = # K! j& {0 b& @, u1 r5 u5 A 1 \- \; B& W+ `& ~- g8 P) h. wans =4 D1 \4 }) o, [3 P2 P3 i) \7 d
1 8 k _/ ^: I9 m0 j9 l2 c/ ~" X4 \ans =: g6 a$ c- F4 U1 u
1 : D+ W& @7 R: u/ v- Lans =0 C3 W0 j1 X# \/ G
1 ; l% m0 e$ J! e' o( P. Lx =4 K6 ~6 E2 h- Y& Z& G ~
23.54001 C3 |% ~2 @8 I( }; B& M
ans = 7 B3 l9 d& H8 G 0 3 Z4 ?% z: j5 Q- X ~ans =/ T; Y' h) D; _
1 , \5 U6 G9 q3 t8 B, \0 l; gans =% S+ P2 a( D: P" e* U; w3 \
1 , w5 V) y0 Z3 j' T" D e2 v B/ E9 Zans = " s0 D& _# q6 p1 }1 L+ j( g( G0 Q 1) u' A; b! O. u+ m2 h2 s; J
ans = ! r& f6 e- H' x4 B2 u 19 g0 H8 _7 v( q B. U0 b
x = $ ~9 F+ o' A- d* I ~* ^) ` 1 2 3 6 G. e5 v( u. B3 ^. L6 rans =, h, X5 c: j0 ^" M
0/ n9 u) o! Y6 t+ E+ ]" {
ans =" K: c& E8 i9 K+ C6 g( |7 E8 P/ s
1 : T2 s8 C. Z6 Y% A7 Fans = : g- l2 S; i; E 1 ! k9 ~ G- M& d* |ans = 5 B5 {/ y/ k! \& A, c( F 0 ' x; i# N( q- u; i3 rx =% y! M) i% J# _
Hello1 W/ }+ L" Z: X2 A
ans =, \( N; z: @+ I: ~2 n( |
0+ H; J3 E) }- h v$ b
ans = & _# x+ J/ g& x# S9 b" } 0 ! J9 }% ~8 U) I( S5 M* \/ U* Ians = 4 }$ c. k m; |8 R' n5 p) \ 1 ; v; z9 o1 L0 D# ]4 d) d5 k, Xans =7 X+ s1 v0 U; L2 Z2 a, |
02 }" l$ W* L$ q5 p1 W
ans =& X5 D$ @& g: r6 G+ a
0 - Y) m3 R7 N& g! D: g 1 ?4 V0 z$ F q* }———————————————— ! p' X' X! G B* R2 f版权声明:本文为CSDN博主「Phil__ming」的原创文章。; z- D- S3 m0 Y+ ~
原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575 + K% c( G O0 N3 Q4 K: n % D; ~' J8 n. L- a @2 h# X, Q8 J+ Z9 c* z