数学建模社区-数学中国

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

作者: zhangtt123    时间: 2020-1-20 16:59
标题: MATLAB数据类型
MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
+ l# Z2 y& l0 G7 j' J0 D' d
/ W8 g* }  |5 P4 R如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。
  V$ K# h, V5 k
1 v3 S' B1 Q1 N2 Q- ^& V5 V. l例如,
0 T+ |1 T: i+ n- |" p. n# V& x9 r$ n
Total = 42; V* a$ j' c% M
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。
. @) N  g+ R3 R8 W' ]1 k% X! O( g$ s9 O+ q3 D2 u) ?+ e
MATLAB中可用的数据类型
7 n: ~7 }' e! z9 [1 S6 S* U$ i6 S- Q5 z5 o+ {' J, d$ S
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
) H$ o6 M7 a% m
. t" l8 m* C, i/ [9 }+ D% Q下表显示了在 MATLAB 中最常用的数据类型:1 o, [, T0 o+ [+ w

0 s: l1 a9 \8 M' }( `1 ~- k! E6 ?* B数据类型        描述
& w  Q6 o& K! C% \6 ^# _: s% lint8        8-bit signed integer
' q& }8 R# V+ T* p8 W7 p, fuint8        8-bit unsigned integer" K( X2 Z. u1 A; K- ~
int16        16-bit signed integer" h, \8 Z9 w4 h( ^" z) {- l  |
uint16        16-bit unsigned integer8 [- C& W; v. }+ r8 V6 v
int32        32-bit signed integer
9 U1 u+ k$ z6 N$ ~+ v% r- a# {- {' \uint32        32-bit unsigned integer
9 q: _3 G, I9 z/ `5 ^1 B! d) x# Pint64        64-bit signed integer4 T* _) I( B6 m1 t8 q) c& [
uint64        64-bit unsigned integer& A. m: g, K2 g6 H# G* [$ N
single        single precision numerical data3 N3 W! B3 h- x7 k
double        double precision numerical data
; a0 r" N! k/ R) K" c, b4 B/ J' q. M, tlogical        logical values of 1 or 0, represent true and false respectively3 d% @7 o+ x/ R3 ?
char        character data (strings are stored as vector of characters)
3 I2 ~' G% H% ?" H" z! \1 m. u0 N" Kcell array        array of indexed cells, each capable of storing an array of a different dimension and data type
5 s. S1 v6 S2 r: \' Xstructure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
+ q% j! l) B' I, Q' afunction handle        yiibaier to a function0 D* J* A' D9 c! Y# r$ ~/ r( o: A
user classes        objects constructed from a user-defined class4 o1 g+ }4 K' k7 g5 b1 ?
java classes        objects constructed from a Java class6 ^, Z" s, ^5 i0 S1 s& v, e
例子
7 o9 V. _3 h, Z8 l' ]. Z/ ]+ o" j* X) a. l4 k
创建一个脚本文件,用下面的代码:6 T( D5 {' k# ^5 `6 g: n* Z
) V  m" d. ^4 \. y
str = 'Hello World!'" u6 J& P( m! |7 U3 Y
n = 2345, r  _7 U" S0 n$ O
d = double(n)+ i( W6 m9 }& e
un = uint32(789.50)5 g% D: e; B/ g5 N
rn = 5678.92347
; n& p# A, N0 f' Y  Pc = int32(rn)
4 W* v% }8 @+ L7 x9 E! n上面的代码编译和执行时,它会产生以下结果:
& Z$ f' w7 S$ p8 x  i  |6 m9 W! ^1 E
8 ^* z3 g  g6 Q. W. C; w' z* F# i' fstr =8 g0 f! r, C; T9 H% G0 h7 r1 T
Hello World!* I; X5 v' X2 g8 w1 u0 v# S% U
n =
3 W# D: a2 r! H3 C- D3 T% ^3 X. s  t   2345. S0 t/ C8 x- E! R  h
d =* T3 W: V6 i1 r5 p! }
   2345
% ?: ]6 x: V" A" U5 F& Bun =
1 D1 E: g+ \: n2 f   790
# D) H( K. b, X+ U4 W  Srn =  N1 h4 j4 q8 p5 y7 l+ z3 _
   5.6789e+03- \4 F7 m, F) X
c =
$ x2 d0 s( u) @! S' e! r/ R/ ~   5679
! d9 d, x5 ?0 U2 H# ^数据类型转换
0 p  L( t; r8 O) A& d6 e" A
; H1 n) O0 s* r3 L6 x: C2 \# K  eMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:  r2 P7 v8 F0 \; v
& p% R6 J% T; c( O  G
函数        目的/作用
# G$ |% ^: X% I& }( ~+ D# `char        Convert to character array (string)
) P! _6 X$ o$ @! @* Q  E$ B7 ?int2str        Convert integer data to string
1 @3 `8 k: P2 S! P5 p6 o  Ymat2str        Convert matrix to string
( Y3 C2 I9 Q" h; lnum2str        Convert number to string" \" a0 C; s9 f" x
str2double        Convert string to double-precision value" ]0 v) a7 V( H% I; e+ p, U8 h! l
str2num        Convert string to number$ V8 H: n* n0 F- m  B3 P' o
native2unicode        Convert numeric bytes to Unicode characters
9 H% F5 Y! y: B% A2 junicode2native        Convert Unicode characters to numeric bytes
! x/ T5 u6 Y3 [* k4 Y. R9 N, B/ zbase2dec        Convert base N number string to decimal number
4 Z9 ?4 @; `" |5 D# `bin2dec        Convert binary number string to decimal number
7 q/ }$ ]. Q# x; }* Z" v" u1 q9 ^dec2base        Convert decimal to base N number in string
& {: H! N' M/ |: x# @dec2bin        Convert decimal to binary number in string
2 `8 R7 j$ K  ?4 T2 D: _2 N4 Jdec2hex        Convert decimal to hexadecimal number in string
8 r1 C4 x$ D$ V3 R8 D) ehex2dec        Convert hexadecimal number string to decimal number
0 t; V0 R; J7 i) X! Phex2num        Convert hexadecimal number string to double-precision number
! {9 T' F. p4 V' Q9 vnum2hex        Convert singles and doubles to IEEE hexadecimal strings
* `! _/ A+ l( A3 ?cell2mat        Convert cell array to numeric array
" Z/ z- t* _3 c3 x3 ycell2struct        Convert cell array to structure array, x# z( @8 I# z! O
cellstr        Create cell array of strings from character array
1 C( x8 s( F. g6 y4 Imat2cell        Convert array to cell array with potentially different sized cells! j/ ^& e- U2 m, L& o
num2cell        Convert array to cell array with consistently sized cells' b1 d' z1 A8 L9 n+ q& e
struct2cell        Convert structure to cell array6 k1 B7 x5 R) U; U8 x6 l3 X4 J
测定的数据类型& ?4 a6 o" D0 L6 o! |( V
: o* O. y3 q2 {  X2 z6 m! y- N1 Z8 W
MATLAB 提供各种函数标识数据类型的变量。# T8 F9 M" G1 V# b" o
% V8 c4 g* A+ ~& `7 g% F
下表提供了确定一个变量的数据类型的函数:* U* V9 @) B: F3 [8 d; H
" u+ N' P/ c; W# Z: Q1 E
函数        目的/作用0 J9 R: t9 ?, K5 w6 n; \5 C
is        Detect state2 m2 E' `7 A) U- i5 I5 W) n
isa        Determine if input is object of specified class& `* d7 P2 l1 U3 f' T
iscell        Determine whether input is cell array% X7 I/ M/ q3 m
iscellstr        Determine whether input is cell array of strings
# r/ B) p) l& r$ mischar        Determine whether item is character array
/ X. f0 t# O. c) Z- d7 Qisfield        Determine whether input is structure array field
0 O6 n# `5 O7 Disfloat        Determine if input is floating-yiibai array
. k: U( j$ c. ~* U# wishghandle        True for Handle Graphics object handles
" F; j: d1 ]: p" {isinteger        Determine if input is integer array
0 M, \7 k( _5 [% ?isjava        Determine if input is Java object+ i# r, s% ^; n
islogical        Determine if input is logical array
) k0 i# X! }+ x) b, S, wisnumeric        Determine if input is numeric array6 E1 d- }: E/ |  q6 T
isobject        Determine if input is MATLAB object% N. L0 y- ?. Y9 e$ g1 s+ k! `. Z# U
isreal        Check if input is real array+ g1 ?! R6 K5 c* ~" M
isscalar        Determine whether input is scalar% D0 {3 F- G1 r9 e
isstr        Determine whether input is character array# ^5 M# r8 J; ?" K( v! U( m
isstruct        Determine whether input is structure array
  ^$ x0 _" Y7 k/ g$ z( \/ ^; Misvector        Determine whether input is vector) Q% l2 _' I' ?, \5 I* d9 h1 {
class        Determine class of object2 L& j  r. {; D# X. S/ V) |3 a
validateattributes        Check validity of array
9 H0 Y6 s3 }. Z! Z* i2 w7 C1 mwhos        List variables in workspace, with sizes and types
4 D  e7 W5 y1 _0 X/ g, h) S例子) {& M: B! Z" c) v* B9 b
* Z. `4 Q# U0 U0 u7 T9 K/ ]
创建一个脚本文件,用下面的代码:
/ B3 z' v' W* W- V! p0 e
; K1 O$ O2 Z5 Gx = 3
$ N' s; v. V( w% `# `* ]. ]isinteger(x)
' x" f2 f+ }% C' b! A0 hisfloat(x)
. }  A2 z) ^9 X6 I9 g7 s; M+ Aisvector(x)
, L4 x' I, R0 H; @" S" S2 Yisscalar(x)
% [9 Z" o: e( k' [  E* e8 ?isnumeric(x)
3 D; _0 K6 A* k5 {
  H6 i6 l- e/ ]" P6 {# [2 \. g/ M* Hx = 23.540 _" S5 S! A0 F, Z4 o4 u* U6 ~
isinteger(x)6 I# n8 D. n; X3 a/ Z  E7 _! w( d
isfloat(x)) l0 E$ M( s) w% u6 f* @
isvector(x); z$ \8 h# d! I% t3 z5 `
isscalar(x)! ^8 R  G) I  t2 G: F$ R
isnumeric(x)
3 U# q6 S6 o4 c0 u: y1 P5 h$ j
. ~3 |9 C5 \# a- [+ Y/ y- T" ?x = [1 2 3], z% H& E( Y7 c  k7 q1 ]5 l$ r/ P% J
isinteger(x)8 Y/ s4 l# L8 n( s1 W# a* J# ?- Q
isfloat(x)
( Z, Z' N6 ~8 @) S3 b* ?/ Wisvector(x)7 p* P" t/ R6 \: d& l# P
isscalar(x)( r; ?# V2 `- t: U
+ [% D2 u" c0 L
x = 'Hello'
' b' o# j. ^; D$ Risinteger(x)8 @! P: a, G, V$ x% b
isfloat(x)# D9 O2 r4 x2 X6 |
isvector(x)# f3 j# B& i6 R) R* {( R
isscalar(x)
; P8 _( N3 c$ q. ?2 E( e$ v# l- q  disnumeric(x); o  T' L7 t/ _2 m8 S) ]% d7 Z1 y
当运行该文件,它会产生以下结果:
6 K4 h% v  b% ~1 o5 A
4 n! @. g0 o: H2 Tx =( [+ g2 m" G# d7 L+ K2 V, \9 t
     32 ~% t( {) N, n6 V& O
ans =
! d: _* ]1 F" |5 ?! D* w% ?8 |     0
( d- O: A) g. Q2 c* m# pans =* [% f& ~" W; k1 J' @
     1
4 x! E7 F- }  ^+ E5 g, {ans =
( I- g9 p* ]0 m; [     1
& c* T/ Y5 l+ v0 B  Gans =
) q; h' P! o2 l& b4 h+ D     1
* q0 d; @0 a+ f& \ans =
, z4 L' M* o! ~: r* [7 a3 b     1
* o$ l, c9 U& Mx =
$ G( o1 E8 q* H   23.5400. V  E3 n) T6 t" E! u$ s+ z. P
ans =
2 t( s  v3 A! [/ Q     0( j. D3 ], m& Y4 ?# r
ans =8 S5 G) M- E: O% {# T
     1
5 v% d& X* H; ^, o9 P9 |6 t1 C2 @; zans =
* ?# M6 _6 `+ ^+ z$ d; f, ~     1* K/ A8 J, j0 G/ n* U
ans =
% [) h: ^; Q  Q* S) V; Z- ]     1% v: ~' G" D- h9 z" o+ F
ans =) k# w4 r) g- _; b
     1
' }- ?9 N. R3 I# W" Dx =$ V2 t3 p4 q& _* b+ j# Q3 y7 D
     1     2     3' I( l, {- ], i9 n
ans =
1 i) m/ D1 o3 j$ u     0
; _2 K; m, w% [+ \; X# q* cans =9 d* ?- M; a/ P
     1* T) x5 l1 J; t; l. b
ans =
. n/ L, M( Z6 P: `! Y# x0 i* Q4 Z6 b     1
& u! o0 @, i  t" v+ w) eans =
4 s0 q  h$ s. z' c. A4 H/ G" E     0
9 n" \( D; P9 M1 c" z7 Gx =
' T) L0 B1 u) p6 }: bHello) d8 K, n. v: u% p* D0 e
ans =
. p* G+ H* j$ C2 @     0; j% T; ^! ?' h6 U. C& z
ans =
9 m- f+ y2 ?6 L) \     0
. c* D3 h- D8 R6 J2 rans =
$ [0 G5 [/ H$ o     1
  J$ @* i" a* [: Aans =
" z- z0 D- v3 b  V3 B7 x. r     0  m; h- ?4 W& y2 ]/ b! _
ans =! n) s6 s8 g! |1 K$ M
     0
7 \5 b% K7 c9 c: G2 s9 L3 E9 `
+ [, b" y+ G8 }————————————————7 v" g9 ?; B) V4 s, z
版权声明:本文为CSDN博主「Phil__ming」的原创文章。
8 u8 ^& C) b- u* `5 Z原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575
: \+ U6 j. Z* n: v+ [: O5 _5 P8 j2 _! M) _8 j: B) [+ {9 j. m, l1 C
. ]0 _4 f+ Z! I" y5 c# {

作者: 18019574874    时间: 2020-2-4 15:58
发表回复赞) \" `7 _& D! t9 d





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