7 E( |2 d! b' V例如, , I* o3 B' ]% F : o9 L8 w- r9 ~4 j; b2 Y( [/ \Total = 42 , J& ?+ X- @+ C( @7 E上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。 % l$ w& S8 I3 F; F& z: E4 V X' W5 x p# \0 }
MATLAB中可用的数据类型& F" d1 {$ ]6 j: L. O
3 x3 X- e* K9 SMATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。3 q$ O+ }& a! T. ^. K
& M# J6 P; O m( j( q1 D& I2 B下表显示了在 MATLAB 中最常用的数据类型:# b5 [2 D9 [3 x* i: Y+ S: X% G
8 @: j+ J" q6 U. |- v4 z- |2 M
数据类型 描述 & A6 _' v# w0 p: yint8 8-bit signed integer# K) P) c9 I: }0 p
uint8 8-bit unsigned integer+ y+ e" `0 v' E' N p, d+ H
int16 16-bit signed integer 4 L" E' G" I" y3 F1 muint16 16-bit unsigned integer # q! {$ m4 O* b! ]1 h* `; [ dint32 32-bit signed integer5 m5 c' m2 v2 L1 L
uint32 32-bit unsigned integer , \+ O- R. D Qint64 64-bit signed integer 4 V& P/ M4 c, m5 ^ D9 I# @+ v& Guint64 64-bit unsigned integer % v5 l$ l( y% N8 V" Gsingle single precision numerical data ; W" ~$ q& N' ^6 Y6 R' b& V7 Sdouble double precision numerical data' S& \: h+ Q0 C/ n/ ~
logical logical values of 1 or 0, represent true and false respectively : P3 j: Q5 l7 dchar character data (strings are stored as vector of characters)7 ]+ a- ]3 T# B, E; S
cell array array of indexed cells, each capable of storing an array of a different dimension and data type ' o. r% O) q0 `structure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type$ c O, K, Z! n) H; C' o
function handle yiibaier to a function, Y {: v, K& Y" R0 T9 q6 P
user classes objects constructed from a user-defined class9 j5 x2 C) O5 U. h2 A
java classes objects constructed from a Java class 2 c* f9 ]; F& s Y; }; |, ?! V例子 E' z1 e. H- ^: C9 C. U: D ) o% `1 e' w0 T# [创建一个脚本文件,用下面的代码: ; [5 v# o' ~0 B; g 0 E- y% W: m2 v& N6 i3 u# u1 n8 A str = 'Hello World!'3 O% L" x/ H! @# Y( q! V
n = 2345 ! U2 i" D! ~9 V! Nd = double(n)7 G% {8 g' R: l, W; s1 H1 f. b" P, O- X6 Z
un = uint32(789.50)3 {0 W2 @! c$ X& `* M& U, ]
rn = 5678.923477 W4 |1 ^5 ]2 a" r. o: K) g1 |
c = int32(rn) 2 Y* Q9 {/ b* ]! h9 s上面的代码编译和执行时,它会产生以下结果:, o( D. p- p: }$ J5 K* D* P4 Q( r
( B5 W( k+ ^' E% S1 l1 m6 y: L* s! \str =5 B8 B' W6 k. O
Hello World! 4 { ]" V8 {" p2 [n =: y' v- L) L6 Q: @/ w, N
2345) b- d+ `. m% A" Y
d =* w$ P" Q" V, {! a' _* `* D; W
2345 5 ]4 A% e8 |' U& h: lun =0 b7 ]5 w+ `' I& Y: E! v; @
790 ; c2 S4 g: g! e& Zrn =! v' ?6 ^$ H5 i8 b
5.6789e+032 _( K: j3 o0 Z1 D
c = 2 N2 x6 u6 A1 g- o1 M1 [- k/ Z 5679 & m4 _! [4 q' \1 j" D5 H数据类型转换4 d( A. d p( L7 f) a2 h8 r
% b- U% ]4 T6 g* sMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数: & `5 H/ h' x# v$ @7 O 5 r1 E# d* Y( _' L: j, \函数 目的/作用 8 d7 T K4 C5 m: T& B( k0 [5 Tchar Convert to character array (string)% J3 t' G& p# y3 D8 M
int2str Convert integer data to string - R# T! Z. b4 T8 C5 b9 Vmat2str Convert matrix to string - Q# I1 t2 v2 W5 A3 G% bnum2str Convert number to string% N/ l/ B P r I
str2double Convert string to double-precision value7 q+ m" m- u! Y- X( o3 w
str2num Convert string to number , F% v0 o/ q* I9 rnative2unicode Convert numeric bytes to Unicode characters) Z) u/ N6 O4 B$ C& K( g, x
unicode2native Convert Unicode characters to numeric bytes/ h( u* [. Z7 _2 B
base2dec Convert base N number string to decimal number2 L+ D2 k( [' P4 v- o
bin2dec Convert binary number string to decimal number9 [9 F$ c, C& E z2 C: s. T. z: ]
dec2base Convert decimal to base N number in string" F. k1 {+ d3 H9 w
dec2bin Convert decimal to binary number in string9 a5 H5 g$ V9 Q6 Z
dec2hex Convert decimal to hexadecimal number in string 3 t; L/ S- M- w& j! M* _hex2dec Convert hexadecimal number string to decimal number $ M+ c) m8 `! f3 G- G8 vhex2num Convert hexadecimal number string to double-precision number+ t8 N4 y$ ]+ I- S% V0 f' l w# Y
num2hex Convert singles and doubles to IEEE hexadecimal strings& s( o5 V1 I- c2 Q+ j
cell2mat Convert cell array to numeric array 8 u2 t! T D! Fcell2struct Convert cell array to structure array ; y" I6 b( A0 c! @) Jcellstr Create cell array of strings from character array$ p4 P0 s- ^0 p7 W7 U% ?
mat2cell Convert array to cell array with potentially different sized cells! B( A2 K3 X h$ [) y5 s Z
num2cell Convert array to cell array with consistently sized cells2 [& Q* |% n' _5 O i
struct2cell Convert structure to cell array 8 ~1 ^ U2 q6 a5 y! O$ c! j测定的数据类型' ^# r' G$ } ?. J& t
4 c0 e: e n( PMATLAB 提供各种函数标识数据类型的变量。" R. O; d8 J0 }, \1 n
" W' l U, g5 v! `4 a下表提供了确定一个变量的数据类型的函数: ) k+ |) _6 d* D& G 2 K8 d+ T( {7 d$ w函数 目的/作用 6 U' F, S' X9 I7 Eis Detect state ' O* {# Q4 z3 U* l# B9 T0 {/ Y! yisa Determine if input is object of specified class 9 s' z! z" Y+ d, X' H0 L. aiscell Determine whether input is cell array7 Y$ u" @# Y4 g; I) H
iscellstr Determine whether input is cell array of strings& I" N! P3 [/ z* j2 u% M
ischar Determine whether item is character array! x l) @0 P% h: U" ?. M0 U
isfield Determine whether input is structure array field9 h* {: W; K I6 p# X
isfloat Determine if input is floating-yiibai array& S! ~! v$ f5 ~' U0 N8 Z1 {, M6 {6 V' ?
ishghandle True for Handle Graphics object handles; C7 r7 w- G1 c0 r( F
isinteger Determine if input is integer array ) _6 A* O9 q% ~+ Z5 E4 Q7 Xisjava Determine if input is Java object9 @ _6 P# X) t' Y
islogical Determine if input is logical array 5 d) o* N/ u5 {- P; uisnumeric Determine if input is numeric array * E$ H4 n/ }9 C# ?; q; o; o, [: Qisobject Determine if input is MATLAB object% R" Y' Q* G" Y; K) e5 X, @
isreal Check if input is real array & p4 i! I5 k/ h* P* T# ?5 e4 i. ]isscalar Determine whether input is scalar3 _. A( I* Y* H3 N- ^# j; Y
isstr Determine whether input is character array/ [7 t7 G- Y" e- i2 e$ w7 ~+ `
isstruct Determine whether input is structure array 9 i+ `( G$ J4 H7 A- J4 j9 a. b( Lisvector Determine whether input is vector' d; u* m- `( d, N/ J3 W* ?4 [+ m
class Determine class of object# N& Y: T# ~0 X% r& o
validateattributes Check validity of array- u% Z: V9 ~. I: D7 p6 w
whos List variables in workspace, with sizes and types% Z3 X$ P7 b& L! w. ?: N6 F4 m
例子 & [6 y3 e0 X" q+ a6 h8 o 8 ~6 Q& O+ f* {. g9 y3 h创建一个脚本文件,用下面的代码:; x/ \' d: y( L& B: k/ e5 x4 G
6 F: R# Q. z- |$ Z- F! x+ j7 r% xx = 39 ~% U1 n* D9 `6 o/ j
isinteger(x) 9 g; P8 K E5 \% O, I* p7 }) Visfloat(x)% M' Z6 V7 x Z
isvector(x), _. q& ~4 k6 {$ Q0 C& V
isscalar(x)+ N- B( D! j. i; h, m
isnumeric(x) ! q. _6 T3 ?6 _: u1 D7 |0 E: g. D# s4 |# y$ K `9 h+ e
x = 23.54+ U7 z1 \6 d( v* e6 h' m
isinteger(x)6 r6 u2 Y# C1 b9 X
isfloat(x)' I1 D# m' \$ j% S% r) A
isvector(x) % v% ~' p/ H# f5 w0 s% A2 Sisscalar(x) " g2 S/ ]4 [; e5 i# z0 Uisnumeric(x) ( n, |, i6 @% E) k" K2 l$ W2 m" v; L2 j- J; V8 z5 P2 ?$ `" Q% a) B
x = [1 2 3] ; c3 Z) H7 d2 misinteger(x) % X+ F# l& Y0 b( S/ }% [isfloat(x) - B" C+ @! M" j- w0 f2 misvector(x)9 o# J% U0 X9 s$ Y* h
isscalar(x)% \ g; M( s( F' n
1 x% ~0 L5 `& U5 ?/ _4 Y
x = 'Hello'2 i' l3 J( K& @6 E' b @
isinteger(x), e: U0 r9 S% T& `; x
isfloat(x)+ a1 ~6 E. G( E7 u: O
isvector(x) / c. _' J. B h: yisscalar(x)8 N1 O7 v$ [7 _
isnumeric(x) ) x, Z0 b) G) E2 p0 O8 c; l+ ]当运行该文件,它会产生以下结果: 3 g5 I9 T% T: C# Z. `3 Y. R# Y5 I$ R; X( f9 S
x =$ k$ V: i$ K, [ Z& W: m' n
30 X% I K9 q: p$ ?
ans = 4 \: L6 t! u% g' S 08 _8 }* _3 v `4 A' Q
ans =- Q g9 A4 |/ n- i
1 2 w$ p5 z+ |1 H8 J. zans = * J3 l) f7 R; { 1 / J4 d2 p: b3 {$ G6 Kans = 6 k: N0 ^9 Y# a* j: W( n3 M! s 1+ O f$ f- q* t% Q4 m# A
ans = 9 i) E; e3 g" m$ q5 n. P 1 & S/ J, A' l2 L% j' v! }; U* G3 y9 u% Gx = ! c" ~3 @, F L/ O& x$ P9 K 23.5400 8 b' u/ b Y- k1 `4 _! pans =* X7 O+ P) V" B. w
0 9 N4 v" Y6 J6 F; B9 ]2 yans = ' n. B4 |; A( D. C1 n 1. \/ |! k' g+ y; c1 ^
ans =; `: E/ a; B/ g# h+ H
1 7 T% Q4 U. D7 m kans = * W t8 T9 J( p0 W7 } 1 2 s2 {$ z7 u( e+ Eans =- V, J# E8 y* y) r! C4 Z6 Z' ?
1$ ?) G/ z- U! j% I! L& J' N
x =) y+ M+ g7 L* }
1 2 3 * ?3 K! [! R }ans = ; Z, t+ ]9 u, c 0 . e' Y8 q2 k/ i6 a) |. y) I5 wans = 3 i# V* B7 g. p' P% d 1( N U7 C: l6 V8 |1 Y5 C1 ^, z
ans = & ~# B& @, s! r/ N 1 6 J0 N8 B* A3 h6 o7 b8 `ans =% u0 V5 L8 a3 ?
0; B# ], M+ C3 d2 I; D! P/ W# A/ }
x =. D; Q, f! k \3 o1 p9 r
Hello - w; |7 v7 b. Q- pans =% I- a2 K$ ~$ J3 Z, O1 ]
0' F& t* f% ?6 j) z: H
ans = 6 |- r" Z5 L' J' I 0 8 N: I) K# z# |( }7 O3 i) s) L. qans = 2 I3 w' Y, A) N 1 " J$ W% } h( ^; tans = * v8 T: T% p( U8 L1 L1 o" t" _ 03 t2 c8 E; I* N
ans = . e$ W5 k$ ~. `0 w8 ] 05 Z8 Z' x! Z5 d8 N, A9 ^ P& q
/ I7 Z! M: `7 Y; S" o3 G$ _
———————————————— % X$ u. F% e! @. v版权声明:本文为CSDN博主「Phil__ming」的原创文章。0 w( e1 N! s% V) R; D; l" R
原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575 " X t- c6 }- ?) ]$ x' n- H- J) d- E' d' P$ V6 V9 q5 C
& _% g1 w( [: L R: E' T4 O