数学建模社区-数学中国

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

作者: zhangtt123    时间: 2020-1-20 16:59
标题: MATLAB数据类型
MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
+ Z9 n0 {2 A* |/ ]' N: n$ ^
0 F! N6 q0 ]0 H. a8 \3 D如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。
" ?* D8 ?5 x8 e, A4 a- d& b, }0 h* v5 g6 |5 v, ~+ [
例如,- @. \. o1 n* L# U' _

' q! z2 y0 A  c  HTotal = 42
2 g  M: r5 }( {" E+ Q( ], T$ M上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。
# f' I, n! k+ o+ G: N6 r2 f9 y) b6 I, ?2 C0 Y/ h
MATLAB中可用的数据类型
  ]$ ]( o! q" f) t! P6 E/ Y: A, P6 L" v' o" l' s$ }+ p! N
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。" G! x* y( x, a$ h: {

3 o# G. x) ~+ P9 e* b下表显示了在 MATLAB 中最常用的数据类型:
: Q# D( W$ ~" F) n- Z) z- e3 j$ E  K1 e0 T! [& [
数据类型        描述  v8 B4 m, C4 [; \
int8        8-bit signed integer% \# N' E- e2 v: Q" D: u! I$ L9 f
uint8        8-bit unsigned integer
$ e/ t/ v/ {2 p6 Pint16        16-bit signed integer
+ y8 R' @: G; t1 @! Yuint16        16-bit unsigned integer
- y8 t( v0 ?* Z) r/ ~0 \( fint32        32-bit signed integer4 }: u1 L# |+ |' E7 o% D$ e( i6 t
uint32        32-bit unsigned integer
+ [, M  H2 u$ C* [7 {5 aint64        64-bit signed integer
5 @8 }* u$ c- x& N* R& zuint64        64-bit unsigned integer$ L) V2 r4 m. h' C& r
single        single precision numerical data
3 Y. ~: R: S6 c5 s8 n* C0 Zdouble        double precision numerical data
0 s+ `3 S5 S; Z! W* a9 e( P* I3 flogical        logical values of 1 or 0, represent true and false respectively' C2 J7 _6 j  r; Q7 [
char        character data (strings are stored as vector of characters)3 ^6 c$ p! X; \( [
cell array        array of indexed cells, each capable of storing an array of a different dimension and data type7 O& Y4 f( _2 @5 ~3 F6 y$ K
structure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
" n0 X6 Q2 ], m; `, |% _function handle        yiibaier to a function
$ N# R9 J* W& y: Ruser classes        objects constructed from a user-defined class
) K3 ^" b& h1 g" Bjava classes        objects constructed from a Java class
: D, \+ D! t, x) G) E* m4 B* T" S例子! r3 S3 p/ Q3 q5 s& _8 ^) ?

/ H) D5 Z, O: v( ~1 q1 Y* ]创建一个脚本文件,用下面的代码:
/ ~7 P& `/ k6 q/ H- V+ m4 O
- B- P7 `2 j/ P. F str = 'Hello World!'
) t1 w5 \: [) ?2 Qn = 2345' P" o) V( s' W( u
d = double(n)5 Z( ]* P2 b) d
un = uint32(789.50)9 ~1 J. i4 @/ e1 {) o
rn = 5678.923478 c% n$ c, p# }6 h
c = int32(rn)! y, V* }  W; i' v* T' g  b/ f( K5 e
上面的代码编译和执行时,它会产生以下结果:
. A, z4 _" B2 j4 @& `, b2 e( g0 E5 g* b8 a( {9 S9 \4 ~& D
str =
2 O& P4 H3 p( M0 r4 c- QHello World!* a+ H4 F) c- G) C( Z' N
n =
1 C5 c/ G2 ~, w4 g$ K" W6 I   2345, ^* M- w8 O$ k0 P/ h
d =, F7 T6 o9 `* t1 g' d5 z. p
   2345" A9 z: g3 Y7 k; K" K* R3 ~, p+ Q: r
un =
! C/ S0 O. ]( m1 ?# L+ [+ f   790" d3 D0 T6 z# C1 K6 o0 u4 d
rn =
" R3 e& G  x1 }, e8 g' T   5.6789e+032 v# D' h3 O2 C$ L1 i( g1 N  S4 W: M
c =
* `4 h9 N' a* t+ k+ i   56799 }, k, L) p1 P1 `% m
数据类型转换
  C& @5 X7 u! k" ~7 m
/ g5 C6 S1 ?5 s, q  FMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:
. d, D. y. J# Q7 U+ Z7 L* Z9 A9 s: v: P/ [4 L
函数        目的/作用
' @  R6 Y6 t% @2 ^2 l* k- T+ Tchar        Convert to character array (string); M" o4 y) ^- [* y8 C  l/ y
int2str        Convert integer data to string7 n# W. ]" k/ j7 Q- Z5 f
mat2str        Convert matrix to string; q2 F* G5 @% B- ?* s
num2str        Convert number to string
( o1 r: x7 }1 }2 p3 `str2double        Convert string to double-precision value. E; ]8 k8 h8 @3 V% r
str2num        Convert string to number% J' i+ f  A7 ]! Z; v0 p) F' G
native2unicode        Convert numeric bytes to Unicode characters
  @7 q; Y# C$ [' L5 v/ p9 tunicode2native        Convert Unicode characters to numeric bytes# [6 d, d2 I0 k: g
base2dec        Convert base N number string to decimal number
; H5 m! f! R# O! l3 Ubin2dec        Convert binary number string to decimal number  W* A1 F6 Z! R  n  c4 J
dec2base        Convert decimal to base N number in string. f1 s& Q, _- i4 o, J
dec2bin        Convert decimal to binary number in string
& o; w" K7 m& F4 `) T, tdec2hex        Convert decimal to hexadecimal number in string
/ b0 z: Q8 C2 jhex2dec        Convert hexadecimal number string to decimal number7 D% ?+ f; N2 d/ i9 j7 \' n* `6 ]
hex2num        Convert hexadecimal number string to double-precision number6 `4 a& x( v/ S) V
num2hex        Convert singles and doubles to IEEE hexadecimal strings' W" a7 t8 f/ U3 j; d; M  v: O4 {) P
cell2mat        Convert cell array to numeric array
5 w$ H+ _' u' _$ s3 B9 A* j, V7 |cell2struct        Convert cell array to structure array
/ h* z' ?9 v% w5 |( z8 ]4 d5 Jcellstr        Create cell array of strings from character array& \! r% S  b/ [7 |% w6 y$ l, J
mat2cell        Convert array to cell array with potentially different sized cells
( m4 G" S) z& `num2cell        Convert array to cell array with consistently sized cells- |4 B/ U( v6 D
struct2cell        Convert structure to cell array( \  e% w$ ~# x6 I" ]: y+ ^
测定的数据类型
; |$ w8 |  ]* P, R/ {2 B/ @5 i" M0 z% P
MATLAB 提供各种函数标识数据类型的变量。6 v5 l% B( c9 F$ t/ R

* |: j: g2 l, y/ v下表提供了确定一个变量的数据类型的函数:8 \9 f) T+ {( D/ p; D
' Y2 G9 C. ^" h" l/ O# j; R9 g! s+ C
函数        目的/作用9 d$ J* E1 \# L0 B
is        Detect state5 Q# S9 @, ~9 v- b) [* u0 y0 x
isa        Determine if input is object of specified class
& h4 Y. M1 O) W+ F) d1 A$ O9 F, {  }: ziscell        Determine whether input is cell array
* I" s, B6 S0 n- O1 d2 l+ ~iscellstr        Determine whether input is cell array of strings7 s, W9 X4 P, _3 s* R
ischar        Determine whether item is character array8 U& r+ U9 h7 @/ ~
isfield        Determine whether input is structure array field; X. I9 G3 l+ K3 Z
isfloat        Determine if input is floating-yiibai array
/ ~  R2 L5 S; ^5 q0 U$ M9 y9 K: \; Xishghandle        True for Handle Graphics object handles, I' K! s+ r. |* c
isinteger        Determine if input is integer array: Y$ w( C2 D  w) q# ?' H
isjava        Determine if input is Java object& C# p/ ~6 d$ V2 ^6 y' N
islogical        Determine if input is logical array3 o* b% E7 G' z+ i/ c
isnumeric        Determine if input is numeric array/ u, a' o' K1 J$ @! t3 I0 k. t$ v
isobject        Determine if input is MATLAB object5 l6 |! Y0 [8 d2 m1 {* l& v- I/ `
isreal        Check if input is real array- |3 ^% R( h$ ^3 \
isscalar        Determine whether input is scalar  ~% {( k2 D( i) F8 q& s' ]& m
isstr        Determine whether input is character array" G# S, O. y: k) R
isstruct        Determine whether input is structure array
9 n8 O' @+ E9 Jisvector        Determine whether input is vector8 d1 Q8 v2 o& ^% l3 x) Q
class        Determine class of object" D2 g' b. x) A: b3 \" `
validateattributes        Check validity of array
* T" r+ u' v+ l) b. z2 ]+ L6 Zwhos        List variables in workspace, with sizes and types6 b- G4 `' i* F3 O' E* l. t' L% }
例子. Z- A, v+ T" c6 }

/ c; l- P; p+ e1 v: v3 Y7 t6 G2 t创建一个脚本文件,用下面的代码:2 H( B& q3 B' ]

$ P: U: r8 d: `; g: N& h" @3 xx = 3
( O/ n0 Z0 R* d$ o. z+ z. [isinteger(x)  g  n' [, b- C2 d
isfloat(x)% v3 ]! o" B9 l) q/ d( e1 w
isvector(x)
4 f6 E4 b7 Z- ~isscalar(x)+ O% h6 c; s4 m
isnumeric(x)$ o1 A3 i9 L4 I
7 u. f! e7 R5 g
x = 23.54* u4 ^& l' C9 ~8 |+ c7 J! w' `! [
isinteger(x)$ C8 p  b8 B4 j6 @3 E: R
isfloat(x)
1 E3 X; {0 G+ E5 o- q4 Visvector(x)
; C2 l0 f. F1 d, \* ?3 uisscalar(x)
$ A3 u0 L4 X" ^: e0 R- aisnumeric(x)
. g" P2 ^7 J, ]6 Q- B: h4 V. U1 z/ r- u; U. j) P
x = [1 2 3]3 P( {" a: r  W$ f; n
isinteger(x)
8 i% x& x) H, a; j$ Aisfloat(x)
- f$ w2 ~" t9 k6 U2 A3 nisvector(x)
+ e- N$ a: x( Y/ zisscalar(x)+ N; \- {5 Q0 ~& y
/ y2 E9 H$ S2 Q; i+ M1 h$ S
x = 'Hello'
* I; P; E# s2 P$ ~. M0 z: Xisinteger(x)- _5 f: w; r1 I9 Y" H: x) a7 l1 L! ]# U
isfloat(x)
2 b; I1 E# q( w( visvector(x)
, N) h& ]3 s& i4 l& \) c: @6 gisscalar(x)
. a0 c) t4 G* A) L; w# _/ kisnumeric(x)
, b# [8 [9 u$ Z# ^0 c1 ^当运行该文件,它会产生以下结果:1 K% W6 b& `- Z# B3 ?8 `' \

" @; z9 c0 ~" Ax =, S3 w) b. `0 }/ l
     3* Z  k/ |! A/ h/ F$ Z
ans =
$ j4 L8 R$ L; f9 X! _/ {% B$ Z     0
8 Y2 Q) }# C4 k) G1 p1 tans =
1 l3 f! @/ I! l% ~; c( r     1
7 Q/ M- }$ F# [9 wans =
1 ?9 r% L) |7 q  D- t. z, [8 ]     1/ Q/ d! \/ u- a
ans =" D$ }7 Z1 _+ Q1 l: I9 W" D) [1 G, g
     1; a4 v5 l" s) o# R" B( j6 q
ans =3 G) h) i: I/ r: T0 ]
     1
. s8 ?( R' V( Y8 J+ |" N: R' D. l: {x =) o$ \3 k& y4 G* h
   23.5400
! u) m1 i) x1 D: }; Pans =
) W7 F/ ?9 f. F; }     0
& P/ r+ h# k: m7 M1 }1 ^9 L: r( sans =/ [7 Z3 h' O5 d1 [
     1  K* O- i7 p, C
ans =5 b! O% J1 x, e  x; O
     1+ S; y$ K0 l! o- I3 G- L5 g0 r
ans =- e+ r* o$ g  D; c+ r2 C
     18 v- \! @" U9 U7 h3 x
ans =& p+ s3 b4 z5 ]9 {- O$ c1 l
     14 P" w  z" h* C9 Z. ]
x =& _1 V$ k2 Z' A# |/ Q
     1     2     3
1 `+ ~: r. U7 K) P2 n, qans =
- Y2 @3 K* ~+ ?+ H2 u) [     0$ A9 e, J( r; N. `, `, W6 N
ans =, L2 W& r  M6 q  Q8 S$ j
     16 o; Z$ N: q0 J0 _
ans =
( u' i2 S% K/ [     1
# }% x! r2 L- Q1 f- e- Gans =* k* J: Z1 W3 ]9 C1 f. K7 ?
     0
2 G9 A" q5 n, d. hx =5 K4 }" b0 X9 P" B1 h9 ?# \( B
Hello
8 S3 S3 L8 G  Y- X% zans =
2 ^& S" f7 C; i8 [: f/ n     0
7 V6 E, N+ _+ r8 D5 h/ p4 A$ H  Bans =
( R' f1 n* r" q6 W     06 t+ _3 H& D: Y( Y
ans =
& G& u5 f( m3 O     1+ T7 T5 J# I; X$ ^, s
ans =
8 N; G' C( \2 Z/ X& P     06 j2 C; l/ Q7 x3 a% P! m
ans =) f+ ?+ @$ R$ V0 k( U5 w
     0
8 K5 ]3 ]6 L% F3 k, f; _9 G/ x
9 D& E3 a# l# h0 P' Y————————————————
8 S. t# t) g* |# {) I5 M4 Z+ b+ S, L9 R版权声明:本文为CSDN博主「Phil__ming」的原创文章。
6 P" p# n  t  @3 O原文链接:https://blog.csdn.net/Poseidon__ming/article/details/750925755 f8 E4 g: e1 n4 W0 T
0 b: z5 c, H1 o9 S6 j& E

" a' D2 T" i( Z! r& X
作者: 18019574874    时间: 2020-2-4 15:58
发表回复赞8 q/ _2 @& d8 O1 T





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