数学建模社区-数学中国

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

作者: zhangtt123    时间: 2020-1-20 16:59
标题: MATLAB数据类型
MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。( ]; Q% V/ {2 R$ @. }1 h" f6 Q7 w
& ~: @/ }; I8 E
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。8 a0 l8 M& g: p' _# A2 B' r6 `

7 m* ]" v+ ~$ ^. K) o5 A+ g6 N例如,
$ w' N5 L% w) T, _" b8 L2 |+ N
2 F( ]& u& X6 Z. z" Q% w4 k% nTotal = 42
& I/ E3 f0 B$ S  X上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。+ h0 G3 r: r  A" B0 U5 ]
1 g& i- Z: y' K2 j8 L& G' v8 \
MATLAB中可用的数据类型5 ?% [* ~- `8 W" \/ e. g

$ ^7 u+ h1 j. c9 nMATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
0 ]6 F$ Z- H' \) J; B
  i; k3 ]! M- D2 {2 s9 U# {& E下表显示了在 MATLAB 中最常用的数据类型:
7 |' ^2 s5 G. b5 y/ o) c0 C! n2 w' `  q
数据类型        描述1 I; X, }7 r4 w5 k2 K9 V: U
int8        8-bit signed integer
+ A3 i1 W$ a- _. {: ]& ~8 kuint8        8-bit unsigned integer. G7 T, R" t. Q
int16        16-bit signed integer4 C# x& V- n% g
uint16        16-bit unsigned integer4 d& O7 ^  u( e# g+ Y/ E
int32        32-bit signed integer
" i8 X; e% D! _9 Ouint32        32-bit unsigned integer
6 V: m$ l; H6 ^, K" xint64        64-bit signed integer
6 J- h" z3 ~; l5 Kuint64        64-bit unsigned integer
/ w1 L- q& h7 o- lsingle        single precision numerical data' c* J) u- J4 {! c
double        double precision numerical data  V  C) X& D- D' r
logical        logical values of 1 or 0, represent true and false respectively6 K. v# N& ?% ~" F: x/ P
char        character data (strings are stored as vector of characters)
; V  s1 h5 M  Pcell array        array of indexed cells, each capable of storing an array of a different dimension and data type
% h" r) N* V. ], i4 ?0 sstructure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
9 w: [* s1 T8 Lfunction handle        yiibaier to a function$ V( N2 ~( E& o
user classes        objects constructed from a user-defined class7 Y+ C. R+ F! ]2 _
java classes        objects constructed from a Java class
) R9 ?* B+ E; k) |# Z9 F& W9 ~例子0 W4 [" a$ q) |) h

; P9 ~" I5 B4 F4 N4 q4 a创建一个脚本文件,用下面的代码:
8 R' C: t- i8 R
& N$ q2 D, B! O/ y str = 'Hello World!'9 m7 |7 w  Y  {: @9 n& h
n = 2345
4 e( h2 A; i( @: _d = double(n)
/ X+ `0 h) w5 X2 ?# E. v9 run = uint32(789.50)
( m8 F, y2 r* D# h# B1 brn = 5678.92347# f  x4 p" f# U! \/ h
c = int32(rn)
' i* |+ p2 C6 w, W上面的代码编译和执行时,它会产生以下结果:
' ~3 t0 O1 ^& b6 I& m* u
/ u7 c3 O8 H" mstr =
/ F8 }% `1 B0 Q5 |; u5 ^: q( k; aHello World!4 s. Z' `7 B2 A
n =  n0 J3 L  ~6 x7 M! @
   2345  p7 a  ?$ Y8 Y
d =+ I3 |1 T4 {7 t7 C6 }
   2345
1 |4 {  ?+ r2 X" e7 `$ bun =
, _' O1 |) w+ u   790! w- J6 y, O8 O" A! u" |, }
rn =
2 \2 d" K7 E( `   5.6789e+033 `* h5 W2 E& v# d$ }; a, J
c =
7 A8 u. n) A$ J; q: o. c5 D1 W# ]   56791 ]/ }% R+ r2 C
数据类型转换
8 e6 N+ }) F9 p( @! X& ]% l
: y, d+ ~# R- o/ _  z4 m- kMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:
: I) y( i; m1 D& ^
/ o. ^, S; r: g$ o* X函数        目的/作用5 G" T. ]$ ]! c3 x, e0 c1 m+ J( ~
char        Convert to character array (string)
+ \* z3 _  V# V. o3 nint2str        Convert integer data to string* y7 b9 g$ B* u% l
mat2str        Convert matrix to string5 h- c: I1 `( L2 U" `! G7 q- E1 v
num2str        Convert number to string3 i) G/ ?+ J' s0 l: B
str2double        Convert string to double-precision value' n5 v9 }: I( G5 y4 Y! f
str2num        Convert string to number
6 w8 X, [; b, ?; ]" k5 q: L, qnative2unicode        Convert numeric bytes to Unicode characters+ D1 y$ {/ X# N6 `# O
unicode2native        Convert Unicode characters to numeric bytes
' C. S( k& b. sbase2dec        Convert base N number string to decimal number+ g) M6 n( V8 Q
bin2dec        Convert binary number string to decimal number
  P) i9 l6 S5 edec2base        Convert decimal to base N number in string3 H% c: b8 O$ \5 f$ t( w3 ~6 K
dec2bin        Convert decimal to binary number in string- z1 O, ?) w6 G1 D2 l3 T
dec2hex        Convert decimal to hexadecimal number in string
) x6 ~% _/ J& @) J7 R" D5 phex2dec        Convert hexadecimal number string to decimal number( |4 T9 M  \3 K) j* [( ?: T3 ]
hex2num        Convert hexadecimal number string to double-precision number
, n1 Y. B. U$ e; |5 P7 Dnum2hex        Convert singles and doubles to IEEE hexadecimal strings
0 [8 M3 f& P6 r8 R+ i6 p; Y$ a' I6 kcell2mat        Convert cell array to numeric array
" d8 j& S" m3 }+ xcell2struct        Convert cell array to structure array
% w/ `7 m, z  m0 l/ w8 H1 M0 f# Vcellstr        Create cell array of strings from character array
6 C- Q1 P, T; ~mat2cell        Convert array to cell array with potentially different sized cells
8 b# [( M( \7 {* C4 ~+ Enum2cell        Convert array to cell array with consistently sized cells
! A+ U6 g: J$ y5 e" m0 t6 r! vstruct2cell        Convert structure to cell array8 x2 J1 W* q7 \% F& j% B0 M
测定的数据类型
: p* Y) x. }$ N' L, a' s! Y6 |: e
- F, Y1 _3 n# I3 E: x/ @3 i9 tMATLAB 提供各种函数标识数据类型的变量。
" [6 V  m( Y8 g1 l0 p1 [; ~5 e8 b( |7 Q! j+ I1 @
下表提供了确定一个变量的数据类型的函数:
) l( A4 v# S3 }8 _$ M$ a# W2 y4 p% D$ p6 m# ^5 {: Y8 m
函数        目的/作用; [% s! S8 P( n7 E# u9 |& _5 v
is        Detect state5 j3 z% q" y; h- R
isa        Determine if input is object of specified class
9 D9 M% `. Z8 W, p! J& Riscell        Determine whether input is cell array5 C- V( d+ |4 K8 j8 w( K) l
iscellstr        Determine whether input is cell array of strings3 C3 X! a, H9 i6 i
ischar        Determine whether item is character array; u0 ]6 C9 R! |2 h7 r
isfield        Determine whether input is structure array field
, w- S" G& Y/ x. D0 t( x: tisfloat        Determine if input is floating-yiibai array
$ I- Z2 z4 ~( ?) `: oishghandle        True for Handle Graphics object handles. z  b6 K; {9 f
isinteger        Determine if input is integer array
* {, F, S1 J$ Z. Z# `isjava        Determine if input is Java object7 Q: q3 @5 T  }
islogical        Determine if input is logical array
6 K  C1 w: j1 \; U/ W: hisnumeric        Determine if input is numeric array
( Y/ E3 M6 y6 D+ Y& ?isobject        Determine if input is MATLAB object8 w; g! j2 R$ s6 Y$ V2 l: _
isreal        Check if input is real array3 D- m4 {9 B3 @
isscalar        Determine whether input is scalar, Z0 Z% ?/ `1 T5 Q* l
isstr        Determine whether input is character array
0 _1 U( j1 a2 r/ P% S# s0 Visstruct        Determine whether input is structure array
  U/ ]: C1 C; y6 p& p7 x5 Lisvector        Determine whether input is vector
# O5 ~* J$ r- v- P+ C1 N% [. g, }class        Determine class of object" S4 b  Z- l0 r
validateattributes        Check validity of array
! E4 B4 y2 f' u1 C7 Lwhos        List variables in workspace, with sizes and types( m: o; B( p6 k: j$ Y
例子: u9 t  h! x" X( T

$ S0 Q# D4 P5 G/ w, ^& W创建一个脚本文件,用下面的代码:
# P6 D5 Q# h( V: ^. l
& f- @& `6 f9 U" ^# B3 N8 Dx = 3
8 q1 u, O; L2 E0 s" t! `7 iisinteger(x)
; A3 K! Y6 R) Risfloat(x)1 b0 b( o* y9 _6 Y3 e7 _* @
isvector(x)
, s$ i$ \2 t" bisscalar(x): {0 Y3 D/ R' h1 C
isnumeric(x)
; {$ |& ?9 W7 h3 B9 @8 ^* t% `6 U+ {
x = 23.542 g* o; e. H& v% @
isinteger(x)& R) h. m7 G+ g3 x- o8 G, A  d
isfloat(x)
3 V* Y1 S" P% o3 v6 ~% W" x5 c8 Cisvector(x)/ g3 m( \- e% [4 U. }- ~
isscalar(x)& }8 G0 r8 j5 G6 l. Z6 i6 j" N
isnumeric(x). Z" j$ }" L) z5 }' t  A
! D+ ]4 X, n; f2 x& C( f
x = [1 2 3]
6 a5 N- s# |8 m( a/ Q- M. h/ Wisinteger(x)
- V5 [; j& j1 X1 U6 K, u; uisfloat(x)
1 c. m# U# n" ]) w2 v) O/ wisvector(x)* A( s$ Q- {# t
isscalar(x)* O, Q3 O! e  ~! J4 w$ i

: d4 ?0 `0 F/ v' sx = 'Hello'
- O, j. A" d, r: e9 m" l' K$ y- wisinteger(x)$ y' a7 [1 y; b. {* u
isfloat(x)8 [- ~/ g. H" s" v$ y# E- D
isvector(x)$ Y3 O5 {- A/ M: V6 M( e
isscalar(x)$ _5 i2 B0 I- |4 e7 ?" f
isnumeric(x)
3 |  y% m2 |9 h当运行该文件,它会产生以下结果:5 M$ r) }2 j2 y' H) G

. H2 o4 `0 C7 l- a! Q% O/ p9 `x =( w2 w7 f8 o9 H! y) U3 y
     3
$ G) G2 V* S' W2 o9 W) pans =
4 i2 N3 J2 i# s9 W8 V8 o     0
# N9 }8 \5 H7 {' [ans =
3 b1 J& s- T4 A2 c+ c     10 e% N# \: J4 b5 C
ans =, z) E/ d# W9 u4 G3 Q
     1
& x, p1 D/ m; V2 Eans =4 h, I7 c" d) F8 f
     1
9 T; ^2 j8 }- u7 ?0 nans =5 T. E9 J$ t5 |8 o) g- i% y4 w
     1
. k8 y: Q- Q' @, M" M8 bx =" B$ V; c* ]* l) s9 l: K
   23.54005 f: W  i9 K4 {
ans =* H/ G8 Z1 u) l1 T6 A& G- J
     0
9 E7 d* ?3 J4 U/ [ans =
  S' H% m0 B  m$ M: Q0 c     1& M: n( X8 `4 Q2 @
ans =
$ o5 O" c+ Q! E4 x4 N     1
4 L$ z+ A. @1 S: W* Dans =
6 f- I7 f. |. m/ P9 p- P     1
% p" X& W* E) q- F: i+ P9 @9 \1 }ans =! @0 l$ l$ ]' y5 M) q
     1  O4 s3 ]  M" N8 h* C& n4 {' C% N1 G+ b9 Z
x =, W( R" y1 A, ]* Q
     1     2     33 }8 z; X3 G) s) x  f! u2 @( I6 t
ans =8 I8 m" L/ U2 u/ B8 _8 m' d; j
     0! f; L  _* c4 H0 ^' p: D( @0 i
ans =5 J8 e  Q9 r7 J& w4 r* l
     1; ]7 b1 F3 \8 L2 f) D9 N6 Y
ans =* J  _; c9 V5 v* P0 h$ a9 z
     1
4 N* ^5 i+ m( @4 s3 a5 }+ n) qans =, @1 h5 d3 `9 K* v+ c
     0
7 r. H# F) z% {x =
, u# C) I  S! x5 |7 H( ^Hello
' `0 B& V* w& _3 r5 m- Z/ aans =( m" a8 b2 [( k0 C. W! j3 b
     00 {. y0 A. b6 u2 _% o. z- k) W
ans =( `6 ?" g, }; m" n  d
     06 Z+ W& h, Q6 g( D- T5 x/ R
ans =. Q* e1 ~* V3 U- n  @
     1
& R1 }5 {. z. Y' eans =
# R) b( K& }5 Y6 X9 w$ T     0
& y3 d1 v+ B( q$ W+ Ians =
0 P( X& ?5 X, ?) W, \2 {     0
- @# S4 }, |# P* r3 J! ^  t% ~5 {  d
————————————————
/ ]! n# N. O  d: {' m: K( K: X' v$ L& i版权声明:本文为CSDN博主「Phil__ming」的原创文章。
6 i/ ]* F! k( A1 e! v! j- y原文链接:https://blog.csdn.net/Poseidon__ming/article/details/750925756 y6 U$ p+ J$ E- w) z/ J6 A! }

  b$ J; R' o! q0 s
: Z/ d, P% Z* T" j
作者: 18019574874    时间: 2020-2-4 15:58
发表回复赞& J* |  N( d' w8 m





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