MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。4 x' \) S+ [( R. |# {4 s: X
$ z( v4 }: b& \$ k* c# z" ^2 J
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。6 s4 ~; |; b) B: f( W: P
; ]- y* M7 l" K' G! i例如, 5 {/ R" G, x; } ]4 ]5 R' m$ Q' ~. X8 p x9 ]
Total = 42, |: @9 G7 Y4 l! q/ P
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 ! C& t& w, J( |3 F( Z- N9 R , J' V; G8 d- X9 `MATLAB中可用的数据类型! Q, S- i6 I, P' b& |
" x! M) u9 ]6 u2 u7 LMATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。; U6 F: }5 D, J7 L! N
$ f5 U1 [2 d+ G/ i5 R: K% Z下表显示了在 MATLAB 中最常用的数据类型: * ?# ~6 H2 e5 B5 j7 K) X( o/ V! | ' t: J _9 c$ ?0 z' x" ~数据类型 描述9 n+ a H! `2 V( Y6 T6 s! J
int8 8-bit signed integer ( D2 y5 B3 M: R, f/ U9 a6 ?. luint8 8-bit unsigned integer + M: O: Y8 M: Xint16 16-bit signed integer, d J( K' k8 G; n
uint16 16-bit unsigned integer. z2 N8 D6 A# ]# W5 O x7 s% k
int32 32-bit signed integer \. O3 f/ T; e1 ?6 Z) R3 E# ^
uint32 32-bit unsigned integer; W! q; S1 t; E5 \5 t
int64 64-bit signed integer- q& T1 W, R) U8 k% P& v8 [
uint64 64-bit unsigned integer - E- k5 q, F8 R# h$ O' [( nsingle single precision numerical data. S2 L4 X w! r8 O1 @ i e: }2 \) t
double double precision numerical data : l9 [" p8 F% t5 B4 w+ k, llogical logical values of 1 or 0, represent true and false respectively 8 H6 |6 _$ k7 k$ P. Ichar character data (strings are stored as vector of characters): h' R U' @5 A7 M# w6 U
cell array array of indexed cells, each capable of storing an array of a different dimension and data type1 k( Z7 ~# c# s" v
structure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type4 e! Y' X! R' u# g7 @6 Q( r6 k
function handle yiibaier to a function * Q/ d& r) {1 P) buser classes objects constructed from a user-defined class 1 ~" h N5 f y* J- Kjava classes objects constructed from a Java class3 Z! @/ d9 S$ v& }/ u
例子1 z+ A9 n h! Z
+ l8 g/ L: A$ n; t创建一个脚本文件,用下面的代码:8 Z6 B7 @8 `; X& ~! I q2 i
5 } A. ^8 F7 E7 _$ I! ] str = 'Hello World!') T8 h7 P7 w" {) G: q: v
n = 2345 % i9 s# Q, M/ G6 pd = double(n) 6 ^# V/ z4 ~+ |4 P! pun = uint32(789.50) ) D" Z( P' m: U, f8 F4 Q0 ?) J8 }) H7 }rn = 5678.92347 0 ~+ R( R( U! p' d6 Q- }c = int32(rn), N5 x- [4 ^/ S) i9 C; p
上面的代码编译和执行时,它会产生以下结果:& h% w6 w. d; }. M, t
1 l4 o( t9 I. Q5 u
str = / E2 w( @) `- F! s. d6 d! OHello World! 3 [: T! o( G( ?/ [9 }) x/ A5 vn = ! x* z! Y! J7 g8 c 2345- T. ^! O# O; ?8 ]0 M! o& Q0 u! v
d = 0 Q, g! r) X% f 2345$ K8 j* i; G! O9 K0 T
un = ! R* d" p0 S4 x5 w 790 1 }5 K4 R! L- q- Irn = 4 ~$ l1 Y8 |- B7 F( O7 X 5.6789e+03* j6 p. e' b; e- J1 y( D: F
c =' Q$ g8 {! }, C- H2 M
5679 ; V( }) N+ A/ w/ l5 O数据类型转换( S7 @5 B, o! {0 ^2 h0 V
W( `% h: r$ o9 e4 P0 Y% N, y
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:# Y' e9 @1 O3 h+ o1 d; }$ w1 y
( |2 ?5 ^6 S7 V; J- X; \' W
函数 目的/作用 6 w' g1 r6 {, |3 W' S: [char Convert to character array (string) 1 }& c# X4 e% N D: f- O hint2str Convert integer data to string - s2 v- c0 N4 z! x4 R# D! ?) jmat2str Convert matrix to string, J5 ]7 n1 p' d; R P3 k1 w. H% x
num2str Convert number to string " c x2 ^! g0 _1 pstr2double Convert string to double-precision value4 f+ `* t; X: C1 f. d
str2num Convert string to number: B) ]$ L" s. k+ T0 Q* t% ?
native2unicode Convert numeric bytes to Unicode characters * F! m; A) \ u$ Uunicode2native Convert Unicode characters to numeric bytes : B- a2 b$ U0 i; c& rbase2dec Convert base N number string to decimal number1 ? x" S. \' `$ L( o: L9 L* e
bin2dec Convert binary number string to decimal number4 b3 I! _+ c! \% U
dec2base Convert decimal to base N number in string, K2 P% Y5 i4 Q
dec2bin Convert decimal to binary number in string 0 z; h6 Z) L W( S' \dec2hex Convert decimal to hexadecimal number in string 4 `. q, x( U, o6 S( a- W! y" {hex2dec Convert hexadecimal number string to decimal number) c1 _/ I. {' U
hex2num Convert hexadecimal number string to double-precision number ' z S$ g$ Q* o; x3 y- G; knum2hex Convert singles and doubles to IEEE hexadecimal strings: _4 k: J' Q0 Q3 i2 `
cell2mat Convert cell array to numeric array5 ?' j$ _5 m+ Z* |' m
cell2struct Convert cell array to structure array, e( H: {- s2 m7 ^0 I( ~
cellstr Create cell array of strings from character array" E9 V( j) D, F6 Y' u6 f
mat2cell Convert array to cell array with potentially different sized cells3 n2 \, s: I7 l! C3 u+ _! @# v; b
num2cell Convert array to cell array with consistently sized cells+ k2 r% R$ d& {0 Z% v. z% H4 N3 b
struct2cell Convert structure to cell array. P! U/ B+ C. j- g
测定的数据类型 # @! l$ I* b% I8 L8 w0 ]5 b) }0 u+ q/ ]% @
MATLAB 提供各种函数标识数据类型的变量。* r! M* L& {' {" m8 o" m
+ F+ k# b5 \( e
下表提供了确定一个变量的数据类型的函数: $ m' K3 j9 ?* q: |5 \8 ]0 y& e- L . Y" r; i. P( s8 N; n函数 目的/作用, m% G0 ^4 t O
is Detect state9 o# i/ X3 |3 V' F. J+ m
isa Determine if input is object of specified class " e7 A' W7 ^# ~2 L1 ciscell Determine whether input is cell array / c: B7 X2 B3 \1 G: B0 [) j3 liscellstr Determine whether input is cell array of strings * V/ Z3 S8 z! _% eischar Determine whether item is character array0 K% z' i# x1 |
isfield Determine whether input is structure array field 6 L; {- r" l# S5 [: v6 Fisfloat Determine if input is floating-yiibai array) I2 S- N& e7 J
ishghandle True for Handle Graphics object handles * O# R i( L* i& ]) _7 C9 _isinteger Determine if input is integer array - a$ r( H# T5 R U. q. v! n4 U, b2 wisjava Determine if input is Java object ; L5 v" p! S fislogical Determine if input is logical array0 d& ?1 Y3 h: f3 ?3 {
isnumeric Determine if input is numeric array / z' M' L) w# h, ~! `isobject Determine if input is MATLAB object; _; s7 W: ~5 c, S& x- B6 S
isreal Check if input is real array # e& N! z9 f/ R5 R: Z9 f visscalar Determine whether input is scalar + R+ M3 d+ {% k s- r7 kisstr Determine whether input is character array( @. x% k% w7 |( h0 W3 Z
isstruct Determine whether input is structure array ' s9 Z0 q/ \) }0 n/ k2 K# Lisvector Determine whether input is vector " ?, @+ T) @. N- t( D+ pclass Determine class of object 0 i8 [4 v3 Q" Y: |1 Wvalidateattributes Check validity of array ; {5 J0 t3 J7 T# ~whos List variables in workspace, with sizes and types! ?* {) Q1 |6 D& {! c
例子 * q1 S. T& K% X9 P/ H Z. m, b0 g' J4 X8 C% R h# z3 m
创建一个脚本文件,用下面的代码:0 J- j. r- w+ n/ j" c. x2 U
! ~9 e9 o$ {# \# h
x = 39 n4 @ F# s" Y2 @) W) o& g
isinteger(x) 5 {9 v5 V0 E0 B: O' w8 r2 Kisfloat(x)& k0 A8 U" u- A* h
isvector(x) 9 h5 `- a7 ~5 h# q, p7 yisscalar(x). X, R8 z) N/ b. I$ u9 v' u
isnumeric(x)1 b( R- b7 f: ^
( |. V k! L% a% _4 S! Gx = 23.54 $ w2 v y8 \& |9 yisinteger(x) ' _7 {& o( T* _4 l. f1 q$ ^8 Aisfloat(x)/ g, c& c) y: q5 o1 J: t
isvector(x). _: h+ Q# p, [/ Z4 l, b, a3 K
isscalar(x)+ }2 |, N! B) _, s3 l- L
isnumeric(x)0 B5 P5 J z& ^: S6 @; H5 A
) A3 Y4 ^! o, j) ~* e
x = [1 2 3] ! p+ e0 b, B0 ] y* n4 T, aisinteger(x)( B/ ]+ F0 q7 A+ ^* y! S% n C
isfloat(x) ( s; p! F% t! u( ~( Y! gisvector(x) 7 c: G& k/ s2 ^5 R/ H/ o7 \, [2 sisscalar(x) ) B" `" k6 }/ s: R* K" t% T: F, ^' G, V* g4 W8 D
x = 'Hello'6 y8 V4 n( l1 y* p5 @- {* X k
isinteger(x) 4 F V$ y. P5 ~# @isfloat(x)+ j" L& \' M9 b9 Y- G' @
isvector(x)2 Q r' x- M+ u- C8 \2 ~5 ]
isscalar(x)% n. c1 t9 Y; X" y
isnumeric(x) / h# K/ \7 I I& d当运行该文件,它会产生以下结果: * O) X' n8 v3 N2 a) ^) F3 K1 D1 N; p, ~9 i- N! J9 L- R4 p! k
x = # K5 `7 |/ M0 F& m 3 . U! ^ @8 }5 `2 _ans = p3 j$ A4 ~2 i 0. b! Z$ i1 W: Y9 Y8 }* r) f
ans = ( ]- c& K' K/ h' t; `+ u; \/ x 1 s& P; S0 f) Z' |ans =: J8 P: L7 j6 \! s, z( d2 S0 ~9 ^, x
1 ; ]' ~# h& {6 ^ ]; sans = 8 e% a' v7 v+ N) K. | 1 7 W3 b' E8 g9 ^6 gans = - o0 b& ~2 ]3 z 1 " n& b- ]9 c$ w& [; u e# tx =* `1 ~: t$ r7 C: I) X* N% b
23.54001 l2 Q* n/ C9 M& j" r
ans =$ l4 g( P" a( K4 v4 j, p2 v
0+ C [/ ]/ V2 Z2 K k! e6 q
ans =# m0 E: X: [2 Z$ t- ]. b9 ?- e3 \& ~
1 - F; F: f u: A' b+ s9 xans =' t4 w/ w! e, }. R; I3 a7 c
1: C, b5 X+ [3 w" o: t
ans = ( N% s* w1 o; |2 T# A 1 " z9 s9 G/ P) |; y ^9 mans = 9 Q. m6 i5 v7 U; q- u% H- d4 @# i 1" u% P! r- E# \$ C9 U" R8 ?5 S. K
x = 6 k8 t2 }) |( U# f 1 2 35 G3 _5 J( s8 U4 E) u
ans = 3 @; |4 f: b8 h2 i2 Z' m 0* O( w% Y7 H/ I0 [4 O
ans = . r2 u9 {, t0 m+ r$ V3 Q 1$ b2 w: y' ]5 {5 J9 s- @. ]
ans = q6 h: E; u! Q9 p 1 . k3 |# M& l6 K, b2 }- _& pans = : U) G7 y' p& K# X 0 : C) `' v) d+ ?0 h2 f/ hx = 6 x, V) a- ~0 d) P% jHello6 L9 ^1 Y/ I0 j$ |6 g9 P
ans = 2 Z$ h3 \# O7 T* H$ q: P 0: u; h' V0 R" M2 C
ans = 3 L' ? S& y$ A7 e5 s1 _ 0 , ], t* C& a" Z$ v( J3 qans = ; _0 A4 P1 k3 [; K: d* e2 I4 e8 K$ k# L 1 7 w! K+ \( _ a9 }1 q8 [' Cans =2 D1 t8 {: [8 V/ r' u0 G" S: P
06 K$ k2 f6 y1 r
ans =5 M8 p& p g: [7 I; z. U# L
0 ) F6 l6 b G7 M j ( F8 I7 _, D/ H% D————————————————5 H% Z9 M0 J. e& A5 Z
版权声明:本文为CSDN博主「Phil__ming」的原创文章。" \" Y5 \+ o+ a, H7 J3 A0 U
原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575 * D* K" r% }: l6 ]) P- a6 p+ h; y7 ~! w% X4 h6 v5 N
: K# R- Z1 x: g# {