数学建模社区-数学中国

标题: MATLAB数据类型 [打印本页]

作者: zhangtt123    时间: 2020-1-20 16:59
标题: MATLAB数据类型
MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
* Y3 r7 P! g  k5 \. X4 G8 ~! q8 Z2 u+ {+ L
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。0 y" H9 h& V; ^' [7 p1 P, F
2 Y' G+ v. V) U1 w1 p; a( L
例如,2 ^+ B8 Z: t9 P( J) b1 g* {
; B7 _4 j, v% b  ^: Q5 T
Total = 42! u$ P) X& c" B5 s
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。/ q& o3 C2 f1 W/ X
4 t0 B& \* ]/ E/ K/ c
MATLAB中可用的数据类型* G( }* d" m6 i2 Y5 ?5 q
) O6 Z% c1 i0 L9 Z- |% U4 |1 K+ x
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
0 A. @8 f1 q/ L. M4 A
9 `/ s; V$ @% f: G% p. T下表显示了在 MATLAB 中最常用的数据类型:7 d+ B" [4 Q. p( n

2 c( ?/ z/ z' v; K数据类型        描述
6 [% s% B, _6 X$ M4 _9 @/ X' B3 uint8        8-bit signed integer
5 F8 J4 N6 E. i/ t8 `uint8        8-bit unsigned integer. i" Y' n& c$ j5 L& r( m. A+ g5 v
int16        16-bit signed integer, G  U5 v7 T# o+ r8 j
uint16        16-bit unsigned integer
9 t$ d) N$ B3 `' j  E( k; ^  b; kint32        32-bit signed integer8 h2 S2 U, K4 ~  n7 J; v9 U
uint32        32-bit unsigned integer
: x; D% ~! k! R. S) Y# M% ^* gint64        64-bit signed integer: f' F" U7 Q# r7 H. T
uint64        64-bit unsigned integer4 y' ]0 |) @4 @
single        single precision numerical data
; z$ U! E, V+ A  w. ]0 idouble        double precision numerical data
) H; e& g! _1 F2 M' Vlogical        logical values of 1 or 0, represent true and false respectively: S4 _4 R7 S4 _2 O
char        character data (strings are stored as vector of characters)( N7 c! U6 s  Z; L# b6 O6 x
cell array        array of indexed cells, each capable of storing an array of a different dimension and data type
" s1 o3 N- q" Y; s- Ustructure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
3 r3 y# {  O: p- g0 c( p. sfunction handle        yiibaier to a function% b: J0 m$ p6 K
user classes        objects constructed from a user-defined class- |, k3 N" `& Y5 k0 ^# C/ [% j
java classes        objects constructed from a Java class) z; t8 H( [( w- m' p: i. s4 H
例子7 U6 G! |4 N, \  T# I, b+ p7 {

5 W+ f3 r$ }8 [$ {# G. u  d  e) ^创建一个脚本文件,用下面的代码:' {2 y6 r& Q% y% a* V

/ Q: ~$ A( p! u$ G8 G( p( g str = 'Hello World!'1 C6 d% S3 u  C! u/ s3 X
n = 2345; l, ?- u4 z+ M0 h' u
d = double(n)
5 D7 D) W# L! mun = uint32(789.50)/ b2 o* J  s6 y
rn = 5678.92347
, G  {5 K  k& D% d: y( qc = int32(rn)4 H7 H) v- i3 o, m: x3 E8 r
上面的代码编译和执行时,它会产生以下结果:% g, L8 I+ Q! |) e2 d9 B1 z

, L3 y& C, z: L3 u0 r7 m9 S' c: ]4 gstr =6 A( M' U3 \% F& w2 `( A! F$ g3 g+ s
Hello World!7 G" P( E0 k( ^: B% C8 _3 ]9 J9 z
n =
6 t; o9 x+ k2 e   23459 F% I5 m# ~( q; d" L
d =
/ c. k0 D( B* P0 J4 A   2345
: g- z. \6 H4 F/ h: N8 S: `/ nun =6 _2 ]1 s1 \. x' h. p4 b2 c; q
   790+ C& t* B2 f! N" ~- y$ y
rn =
" N8 i, M5 \6 c   5.6789e+03
8 o6 R! G4 c# n1 u2 q: z& l# Zc =& e% S- s$ E, z  A3 c2 g
   56790 _( m9 A% e& S
数据类型转换8 [1 C3 I+ U5 A1 d. c" @& c
8 u2 {9 i! ^* [" f/ Q% b- D' M# M/ |
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:6 n, L$ v" b' o' @, A% _# Q
( e( w+ C; \0 L4 ^- X. K
函数        目的/作用
1 Y# B# {2 `& o( A& {9 M+ zchar        Convert to character array (string)
9 |5 D) ?+ G2 W# r1 Zint2str        Convert integer data to string& ]" @& w' Q4 ?4 ]+ x
mat2str        Convert matrix to string
' }9 G; X7 @# b* Lnum2str        Convert number to string
2 y& ^& n9 e& J0 t) E, ?str2double        Convert string to double-precision value: P, K3 ~- c% f( D7 S
str2num        Convert string to number* p3 o( {( u2 `+ `' V6 K
native2unicode        Convert numeric bytes to Unicode characters
# Y1 f0 w8 a, X3 v; s8 Gunicode2native        Convert Unicode characters to numeric bytes& \& x5 i+ r2 `' n! X
base2dec        Convert base N number string to decimal number% [, w  x: d9 `, b
bin2dec        Convert binary number string to decimal number7 O$ }; C1 D6 V
dec2base        Convert decimal to base N number in string
/ Z: a. r7 }: {  ]dec2bin        Convert decimal to binary number in string
1 {- n* q  z% P# M1 b* U3 cdec2hex        Convert decimal to hexadecimal number in string- m0 l$ i- v% H, v/ b3 e; R0 {3 f
hex2dec        Convert hexadecimal number string to decimal number
4 L1 r0 W4 S( @7 s5 \hex2num        Convert hexadecimal number string to double-precision number
4 H6 t3 q# Q* w. bnum2hex        Convert singles and doubles to IEEE hexadecimal strings
( X$ J4 z9 g: F8 j: u* ]( ocell2mat        Convert cell array to numeric array! E+ V7 ]* z. [* {; U) I
cell2struct        Convert cell array to structure array
, C  j3 N) C; Ucellstr        Create cell array of strings from character array
: r# O8 e& ?3 M* Dmat2cell        Convert array to cell array with potentially different sized cells
$ N8 [1 {# t/ V# v/ r$ h/ mnum2cell        Convert array to cell array with consistently sized cells
* a8 Q: m* f: A' s5 g  G$ g- kstruct2cell        Convert structure to cell array
( R, T5 J. ^9 s4 v2 G8 T5 Y测定的数据类型
( c, v1 S2 V2 M( }3 d% U8 h6 A% c4 U. S
MATLAB 提供各种函数标识数据类型的变量。0 C6 C& }2 G; k1 P; @  M4 ]. a
, q/ N# {# V4 _1 I4 r* y( G/ A8 c
下表提供了确定一个变量的数据类型的函数:+ m! V* d9 q% w- s0 L
+ N- U8 Z& d, @- S7 Y6 Q; m
函数        目的/作用: W% b  ^# S# ^8 ?
is        Detect state: \" a9 p) O3 Y8 c+ |9 w$ Q& d8 f, b
isa        Determine if input is object of specified class, q9 c! l+ e) H! d) |( c
iscell        Determine whether input is cell array- T+ ]" ]6 b+ @  a
iscellstr        Determine whether input is cell array of strings
, Z9 x7 w% l! M6 Wischar        Determine whether item is character array" o4 ?# o- u/ a
isfield        Determine whether input is structure array field
& A( m% S! S8 A# y8 n' m0 fisfloat        Determine if input is floating-yiibai array1 [$ H1 G; _; b/ B2 f/ x; E
ishghandle        True for Handle Graphics object handles% l7 B  U: n! `: p
isinteger        Determine if input is integer array5 S  W! ^% C( B( Q
isjava        Determine if input is Java object8 n! _2 O. `; n2 N  n6 U
islogical        Determine if input is logical array
- x( Y# X( p8 h0 ~- ^8 a9 visnumeric        Determine if input is numeric array$ q( G* A  r: v  u4 H6 r5 D
isobject        Determine if input is MATLAB object
9 ]1 `! y* {1 N5 _  Tisreal        Check if input is real array8 W: k6 y( M% f4 I- T" S
isscalar        Determine whether input is scalar
2 C) g- j) h: Sisstr        Determine whether input is character array
# I- h4 b( S5 W- h! risstruct        Determine whether input is structure array
; E2 y) q9 {5 g; uisvector        Determine whether input is vector/ L) ~- Z5 O; b/ S6 K
class        Determine class of object
$ F# G* s5 k: Z" q9 `3 b8 evalidateattributes        Check validity of array, t/ ~$ U+ l, I3 j) t
whos        List variables in workspace, with sizes and types& F* R7 A" J! \
例子% o/ u( @4 e& j1 e, f. G5 V
+ [7 J& A2 [5 K' H6 A1 I
创建一个脚本文件,用下面的代码:
9 B3 n$ |( A+ D6 s! ?2 }% h  C+ T' Q% Z8 d! h
x = 3
& ]+ |6 i$ g: l  n8 u7 W! nisinteger(x)" d% E. e7 D1 K8 B- `2 X/ E( a
isfloat(x)1 A+ j: e5 ~. H7 R& F) V* U/ N5 ~. ]
isvector(x)
, ^8 y3 V; e/ Tisscalar(x)/ e8 [. w  L  D! R5 E- I# M6 O
isnumeric(x)& o1 w& `5 C. l4 Q: K2 W. Y

# K# A4 S  Y% B/ m0 ^x = 23.544 h, q5 h; g0 Q& F
isinteger(x). {( H7 S- [; ?, Z
isfloat(x)
) o2 g& T) w+ kisvector(x)
8 m9 Q& N2 P% ?; v. r8 }/ jisscalar(x)( p  Y& D4 t; n2 {& v: {. Y
isnumeric(x)  T; L. k9 p0 G( Q

) }( [/ f# |& lx = [1 2 3]; D2 O6 h- ?# h/ e0 a8 a- a
isinteger(x)
$ t: s" O0 }# f' k3 |$ I! z$ t4 Eisfloat(x)
1 {# _; ]+ p/ pisvector(x)
: z8 I! U# B0 s4 ^) ?isscalar(x)
* }! Q( O- T0 f2 g5 ^
  m& h3 h; m: J/ D) Tx = 'Hello'; E" u7 K; f2 e/ F" n% I
isinteger(x)) t$ W2 H& z* t  t/ b6 E
isfloat(x): [" k* k1 e' u8 X+ F1 H
isvector(x)
& ~! L" D* ~2 Z$ L0 |$ d; V9 qisscalar(x)1 S6 A/ y) U$ A& p& H0 k# M
isnumeric(x)+ y. f0 C/ S1 ^, L
当运行该文件,它会产生以下结果:
* D) Z4 v6 q4 x" |9 |# C6 A" c! L) c9 |2 U4 f$ S. C9 \9 ]. L
x =2 C5 L' q6 X0 @
     3* [3 q5 P9 U3 k( P' B8 Q
ans =- [3 G) c$ O' \& \
     0
2 q0 B1 M  s  J- [) {8 M( [ans =
9 J; x1 F& f9 B# k     1( P) [& z0 I! k7 m( c' r
ans =7 U* ]& C, z! {  [/ L- ~( O
     1
* p$ s) ]5 O0 z3 _+ u  zans =
8 G" I7 L% y. f% i, G5 v$ k& L! G& _     1% x& Y) t3 G0 q+ G9 y) c
ans =1 }7 s2 P; Y: N  |6 g, E* l
     1! Y/ {# i. H6 ^
x =
& {( T5 f% x8 f% n   23.5400  T. s+ s3 H: ?2 F. A( G
ans =; [$ N0 q5 s) s% O) ]
     0
' i6 Q4 `" g0 {ans =
( {- c' Y! z, c3 V$ L     1
% ~: z( I8 C  s' f6 S' Y2 Z& kans =/ W' v7 W$ o: f) a
     1
) l1 W7 W3 a  f0 ^ans =
# t  p. g# f7 Z: e9 Z) S     1
# p! l4 X+ j* `0 n5 y/ Sans =- B+ w: L" O- |4 V% t
     16 t/ c. {; T# l: z  ]$ f
x =2 a! k4 O. [7 r/ T( j
     1     2     3
1 ^7 U3 G! T" b1 _/ A4 x: h9 a4 vans =
- J0 o0 r; t; T/ A+ g2 [     0; L9 |, d' k' e3 `4 b
ans =
$ V4 w( E0 x5 ?( p, \     1! U  l/ k7 Q9 M1 u
ans =
7 O# ]+ S4 f' E- q- }     1
$ E. {  V6 K! t# F# jans =
6 g. n& ]  x% {7 E8 J# _( Q     0" J5 s+ l7 F7 ?9 E; ?0 \( o2 Y; b
x =+ [6 j- V1 m% b% j
Hello
; S$ e9 T2 ~5 v; R# Rans =6 h8 N0 [+ e( o# `4 f
     00 F9 S4 N* P4 ~2 P2 `; g" s* B
ans =
! j5 p7 R. p3 F8 B% F  f     0- g7 N& s% v* o7 F2 ~
ans =
# z: M6 ^6 v! a) @     1
/ j* B* E6 y* r6 s: Sans =7 A6 s3 ?. U+ X( J# `
     0
9 Q8 |7 s& O- H7 e: Tans =
2 {0 v; `  l, N9 s( V9 G/ _, @     0
" h* X4 }+ Y+ Y1 r, s- V( [  _  \% Y$ g, s7 ~# _1 o2 j1 @  f& S* _) ]
————————————————# O; t& f9 @9 Q! F$ |2 Y  M
版权声明:本文为CSDN博主「Phil__ming」的原创文章。
2 E" G! X  h7 [  u+ t/ ~0 K原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575
5 g% p" H! U/ F" Y/ g$ n+ r! k8 ^7 H2 y6 J
& ?4 |- V1 w: o/ f0 P' r: D

作者: 18019574874    时间: 2020-2-4 15:58
发表回复赞) A5 L4 ^( G- v/ f3 @% x, F# e% A





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5