QQ登录

只需要一步,快速开始

 注册地址  找回密码

MATLAB数据类型

发布者: zhangtt123 | 发布时间: 2020-1-20 16:59| 查看数: 2382| 评论数: 1|帖子模式

MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。& H7 M9 B9 E% f& `
3 ?0 K: F" A! j" c' J
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。$ \# @' y  _9 u
) U. g! w- ?8 u# h3 q
例如,7 b  L7 z$ B; [3 S0 k, a; K7 A
) ^& O4 s5 A& k( B8 Q; c( x
Total = 422 G3 D# \) t6 h, L" g
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。& T7 B% C- h. Y# Z

. J7 o: q7 B% ]( ~4 C1 C, YMATLAB中可用的数据类型
4 ?' n' v9 O5 _5 f* X
$ w" R. \1 Q2 y' n* t& HMATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
( o; m. Z" P6 W5 J4 |
; E6 _* i0 I8 y1 s8 F0 N下表显示了在 MATLAB 中最常用的数据类型:
: D- O' g2 u- z9 d2 y- C1 ~. d0 [0 E; b5 _+ z; B& M. V0 [
数据类型        描述
) d( R+ p3 [! N$ c) A1 I7 B( u" r0 vint8        8-bit signed integer
6 E1 v6 E. V8 W: tuint8        8-bit unsigned integer8 m2 H8 x6 B" C# ]5 G3 W
int16        16-bit signed integer( H- N3 e. X1 a& o
uint16        16-bit unsigned integer
/ e- ^% Q  i5 aint32        32-bit signed integer
! p5 Q5 |# D: K+ ouint32        32-bit unsigned integer0 @3 G; W7 y$ B2 Q+ P: T
int64        64-bit signed integer6 T' j$ M7 {: ~7 I, M
uint64        64-bit unsigned integer/ K$ \# w8 J2 M- T6 c
single        single precision numerical data( G7 C2 _' n4 m
double        double precision numerical data- z) s  E6 a/ y- z! ~* m
logical        logical values of 1 or 0, represent true and false respectively
2 }0 l; K# v0 B, q8 r+ @# C9 cchar        character data (strings are stored as vector of characters)+ P% l9 j% ^" u# q. D1 [
cell array        array of indexed cells, each capable of storing an array of a different dimension and data type7 j" {8 M9 I& D3 b% i% C* @
structure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
4 D8 L* }+ Z/ }+ ]+ Sfunction handle        yiibaier to a function% ^6 k! ~6 `6 A8 k
user classes        objects constructed from a user-defined class
# h. J$ ]+ D9 F( Rjava classes        objects constructed from a Java class
4 k  I: i8 x8 E  ]. c* O! C例子$ z  f0 T0 e) i: R5 r
& i5 @9 B2 _& c5 V3 l
创建一个脚本文件,用下面的代码:1 m0 u3 b1 k5 C; B* k% K: k8 T% R

4 w# D1 n# \; l9 T" [ str = 'Hello World!'' Z' o2 L/ L, @- X6 i
n = 2345
1 J" {$ K$ Q0 X  q+ V2 L/ e3 g9 qd = double(n)
7 m' ^+ a" X/ ^un = uint32(789.50)
/ R. O4 i$ Z2 j8 Crn = 5678.92347
1 Q, [2 x& s) t" {7 g2 ?$ \7 |3 Zc = int32(rn)0 n, h; b, t' d6 W* `1 Y8 E4 N. E
上面的代码编译和执行时,它会产生以下结果:6 s+ G+ ^% s. X; ]3 \8 s. b2 E! d- l  c

+ ~" x6 f; |7 s3 ystr =
' |: L6 _6 X, J  Z5 ?9 \" pHello World!
5 f4 X8 x; |7 Sn =$ R2 g1 t. v/ v( h
   2345
. V( J3 V1 U* c5 J* Dd =
3 v1 d3 C* Y, N   23455 m9 U0 k" e& m, h: z. O+ z$ c
un =
- ^! l* D0 ~8 Z" ]% H/ B8 X- b- Z  x   790' y, q& }& O( R! Q
rn =6 i' H7 d3 N& C. `
   5.6789e+03
- C: w7 H2 g6 e/ Hc =; O! F  t* q5 g8 i- U
   5679; `$ D( H) z- J" T5 S  S  m" q: k
数据类型转换
1 H2 H  x3 T( }9 Q2 o+ h8 U7 n1 r* R6 Q6 z2 a
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:
- X0 n" N. r2 s: p8 B; L- n9 w" W* X+ l* T
函数        目的/作用
2 I6 U& b8 |  C% J+ W3 c( Nchar        Convert to character array (string)0 b! Z' }9 H- Y# y1 X0 P  @
int2str        Convert integer data to string1 n- y( U2 f4 ?9 d' t$ o
mat2str        Convert matrix to string
; L% ~' d4 k. Anum2str        Convert number to string
2 h# F; _- }+ o2 }str2double        Convert string to double-precision value$ _. u. [2 J* d) T3 B
str2num        Convert string to number
7 I) R8 p* }' M; ^9 @; hnative2unicode        Convert numeric bytes to Unicode characters; o& H' U9 p' c. O8 x
unicode2native        Convert Unicode characters to numeric bytes
: Z$ Y/ \' V" a* s, |- V6 gbase2dec        Convert base N number string to decimal number
3 g% t1 R) m4 x3 w# \9 X  gbin2dec        Convert binary number string to decimal number
9 T1 C: B# ^( n0 Y( E. D* Tdec2base        Convert decimal to base N number in string  K) ~" ^) ]) [) k, \' Z
dec2bin        Convert decimal to binary number in string
) r" H9 ?1 I0 {* W3 ?1 z# A3 K" r! Ydec2hex        Convert decimal to hexadecimal number in string
1 ^. _" R* W& I1 X6 }4 jhex2dec        Convert hexadecimal number string to decimal number
) Z+ N; O; K5 P/ D! R: phex2num        Convert hexadecimal number string to double-precision number9 R1 H- }, H* {$ M( J: y
num2hex        Convert singles and doubles to IEEE hexadecimal strings0 \+ c: ~# r. \& c" d
cell2mat        Convert cell array to numeric array0 X( Z/ S# ?' }2 E  Q7 _
cell2struct        Convert cell array to structure array) _1 }0 P8 U4 Q! m# O
cellstr        Create cell array of strings from character array
# i; k$ v2 g: c# ~( h+ Wmat2cell        Convert array to cell array with potentially different sized cells
! _* C# b1 c4 C+ [# `- pnum2cell        Convert array to cell array with consistently sized cells: S) V, n- i# f" {2 N
struct2cell        Convert structure to cell array, q  Z0 C& L7 G; e- L
测定的数据类型, g- |" G9 y5 S" `8 _/ a

: Q( M( w0 I0 w3 E* g2 @MATLAB 提供各种函数标识数据类型的变量。8 Y# i! z2 A8 B9 L4 M
+ ~  |( F0 m6 a: e' t- a8 B/ w
下表提供了确定一个变量的数据类型的函数:' j+ P: H# \" p
) Q, @4 ^& B8 X( I
函数        目的/作用1 @% r. q! _6 G# B
is        Detect state2 J' V) D3 F& f- ^3 k
isa        Determine if input is object of specified class0 q. P. a! |* p
iscell        Determine whether input is cell array4 Y$ m6 d/ k8 |1 S4 N
iscellstr        Determine whether input is cell array of strings
! }4 q7 W8 @! z4 H% \/ D( Fischar        Determine whether item is character array8 P: h  K, }0 D; f
isfield        Determine whether input is structure array field1 f+ q8 U: c- u2 y4 M- X
isfloat        Determine if input is floating-yiibai array
1 w5 a! y- t- @- V4 M; {( ^ishghandle        True for Handle Graphics object handles
3 B5 K; o7 Y# Z9 r7 N$ misinteger        Determine if input is integer array5 W% Y! X- w/ A) G: j% Z0 {
isjava        Determine if input is Java object
- F" B6 j; W# `" B8 B: vislogical        Determine if input is logical array8 m% J9 t/ y$ c2 Q0 J' e
isnumeric        Determine if input is numeric array
7 Z- z& \6 _% A4 pisobject        Determine if input is MATLAB object  }! c- ~- r7 D& H. s
isreal        Check if input is real array% {% E5 x+ U  [8 Z) b( {
isscalar        Determine whether input is scalar
: Z4 D6 F; L1 ~. `" gisstr        Determine whether input is character array
  Y; j' K( ?6 `6 k" j2 e* v4 ?isstruct        Determine whether input is structure array
9 ?- v( p+ ^6 n9 n  {- F2 j0 eisvector        Determine whether input is vector/ l# M, z5 i1 Z# Q# Z$ k0 |
class        Determine class of object6 z( i8 e* _; d1 H1 F4 J
validateattributes        Check validity of array6 V! d+ Y* ~. q5 H# b& I
whos        List variables in workspace, with sizes and types
3 ]$ D/ B, s4 ^例子8 \1 J# Y  x5 B# {
1 B  f, M) z2 O  |  b3 e
创建一个脚本文件,用下面的代码:
8 ?4 D- W  z6 W+ r8 p" o
+ Z( M3 b- _6 O4 b6 Z' Xx = 3/ a  F0 [) x: \' `3 x2 P: U5 [
isinteger(x)
) U* q0 ~! G* h. N( ^isfloat(x)- x* h6 p: p& \& Z- A$ ]$ p6 }
isvector(x)
& I: E$ q8 \; z+ gisscalar(x)
- o7 X  A6 d# kisnumeric(x)6 l* ]" O5 A1 h4 k& B
4 ~2 j% t% a9 g% N8 U3 ^9 t
x = 23.54; O* h* Q& O* J' v) i
isinteger(x)2 l: G$ v: l# b9 R
isfloat(x)
2 v$ ?- V! o1 nisvector(x)& t( J6 `  E: d* b' @/ v# J5 I! u/ H
isscalar(x)( ^! z. q6 b: w. a: `. W$ T& A
isnumeric(x)4 A6 M! r7 m8 T* T
7 E) f- n% `1 w- h# O/ r6 x# z
x = [1 2 3]
9 O- X# P9 l5 iisinteger(x)
& |* L. ~# c5 \1 V& Wisfloat(x)4 U. u6 Z# ^) ?+ P* S
isvector(x)
- e1 ~  D# Q2 A8 y! {' Cisscalar(x)
4 @. ^) ~, w! s; e0 r8 D" q5 ~* i  e
x = 'Hello'
: d. [# F6 I! I: Lisinteger(x)
! L- W/ n- D$ J8 Z: ~2 E3 F: s8 o5 y5 \8 |isfloat(x)
, ?& K/ ~- i2 O& [, \isvector(x)+ k3 s& z+ u) {8 `# F* G
isscalar(x)0 M0 X" G6 I8 `( T
isnumeric(x)- J( U4 F" N4 _% m$ u4 d
当运行该文件,它会产生以下结果:
- L. H! v, {7 P0 W) \) {+ f1 v5 O9 L
x =" }4 B7 b8 x: |) }2 z* @7 d7 n
     3' X' ]; H( i" A' ^1 }5 d: |
ans =' Z1 {: |" d% I; [5 i
     0
0 Z' v. A1 Q; F; g: W/ b2 h, ?ans =! B" L8 ~( U7 L9 T' b* y
     1  t+ Q9 E" F% D0 x  f
ans =' G8 s' O9 V$ j! t& }6 x
     1
0 F8 f7 x# O" ]! S: zans =- _8 J1 r/ C. X' L, l
     1
. s) U6 |1 M) Ians =
5 `( N* b! a) A     1
( N/ T3 J, g) f, C/ P, G+ R2 dx =
& K6 V1 t7 A) v6 v- b6 K   23.5400- W% n1 c0 W  O. A# c3 `! @
ans =" q3 W! y$ j) N7 u
     0
8 a# p% T8 p9 R8 b/ v4 cans =
  e% E% n2 S  ~     1+ e' C3 ~$ k) `7 f
ans =
' i: _4 ?4 r3 N0 T, a+ j     1
1 b) W2 C1 c$ z& ]7 g. Lans =, x+ n& H9 I  d4 d! I6 y) F, A
     19 ~5 Q( U3 }! j3 F
ans =
* d" d- N, W8 M     18 F& n$ I. F  p6 n1 u( m# I
x =
: R7 k# D% x! b/ i- o4 D     1     2     3' H; A. ]6 Y- P% s
ans =
( f8 C- F7 h8 i0 F     0
0 K4 t& I: x6 W# P1 R9 u- T( Gans =
& k5 H5 J5 W0 k& }     18 }3 i' z2 l/ h( ^
ans =
7 e" a: {7 _2 a. G. v* h& p     1  v* A. {1 B; u5 E  K
ans =
7 S, y; S7 x1 }& A, K5 \! V8 m     0' m, K* D3 m9 e) v: [* k( ^
x =4 u1 i, }# _; o1 Z& s: g6 F( B7 @
Hello% E" N* q$ b  C4 X! B
ans =: U; c' M# \; j" n
     0
9 l+ F# c' P0 K7 Q7 @6 X- Uans =
. x3 h6 \2 m0 \% S% O     0% m, m2 T, X; O; {( u  m
ans =
# x0 H. ~& T8 V1 ^" B. i+ ?     1
$ G# I( e7 J/ |2 a  J5 nans =( |- V% i. m' G* E9 n
     0  c. z) a  b% ], ^" Z4 w& u' d$ H9 Z' p
ans =
4 l- l$ N/ P0 z! S) d8 Y  Q     0
3 M  ~/ Y0 E; i  _% y9 ^$ I
7 W: q* R- A/ I( y( s1 j& l, r————————————————- [3 G; {& R6 o1 d" I
版权声明:本文为CSDN博主「Phil__ming」的原创文章。
! k" T8 \& B% I7 [7 O/ L原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575
* B  J' U  s( c+ S( a
7 G" O  h: @' @( s
" ~. f( t, d( {8 e6 l6 K1 v4 _5 f
分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

最新评论

18019574874 发表于 2020-2-4 15:58
发表回复赞0 X' K) g) v4 v" K2 ]9 k  j
qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-4-19 23:08 , Processed in 0.402874 second(s), 53 queries .

回顶部