Matlab-data-types

提供:Dev Guides
移動先:案内検索

MATLAB-データ型

MATLABは、型宣言または次元ステートメントを必要としません。 MATLABは、新しい変数名を検出するたびに、変数を作成し、適切なメモリスペースを割り当てます。

変数が既に存在する場合、MATLABは元のコンテンツを新しいコンテンツに置き換え、必要に応じて新しいストレージスペースを割り当てます。

例えば、

Total = 42

上記のステートメントは、 'Total’という名前の1行1列の行列を作成し、値42を格納します。

MATLABで利用可能なデータ型

MATLABは15の基本データ型を提供します。 すべてのデータ型には、マトリックスまたは配列の形式のデータが格納されます。 この行列または配列のサイズは、最小で0行0列であり、任意のサイズの行列または配列に拡大できます。

次の表は、MATLABで最も一般的に使用されるデータ型を示しています-

Sr.No. Data Type & Description
1

int8

8ビット符号付き整数

2

uint8

8ビット符号なし整数

3

int16

16ビット符号付き整数

4

uint16

16ビット符号なし整数

5

int32

32ビット符号付き整数

6

uint32

32ビット符号なし整数

7

int64

64ビット符号付き整数

8

uint64

64ビット符号なし整数

9

single

単精度数値データ

10

double

倍精度数値データ

11

logical

1または0の論理値は、それぞれtrueとfalseを表します

12

char

文字データ(文字列は文字のベクトルとして保存されます)

13

cell array

それぞれが異なる次元とデータ型の配列を格納できるインデックス付きセルの配列

14

structure

Cに似た構造。各構造には、異なる次元とデータ型の配列を格納できる名前付きフィールドがあります

15

function handle

関数へのポインター

16

user classes

ユーザー定義のクラスから構築されたオブジェクト

17

java classes

Javaクラスから構築されたオブジェクト

次のコードでスクリプトファイルを作成します-

str = 'Hello World!'
n = 2345
d = double(n)
un = uint32(789.50)
rn = 5678.92347
c = int32(rn)

上記のコードをコンパイルして実行すると、次の結果が生成されます-

str = Hello World!
n =  2345
d =  2345
un = 790
rn = 5678.9
c =  5679

データ型変換

MATLABは、あるデータ型から別のデータ型に値を変換するためのさまざまな関数を提供します。 次の表は、データ型変換関数を示しています-

Function Purpose
char Convert to character array (string)
int2str Convert integer data to string
mat2str Convert matrix to string
num2str Convert number to string
str2double Convert string to double-precision value
str2num Convert string to number
native2unicode Convert numeric bytes to Unicode characters
unicode2native Convert Unicode characters to numeric bytes
base2dec Convert base N number string to decimal number
bin2dec Convert binary number string to decimal number
dec2base Convert decimal to base N number in string
dec2bin Convert decimal to binary number in string
dec2hex Convert decimal to hexadecimal number in string
hex2dec Convert hexadecimal number string to decimal number
hex2num Convert hexadecimal number string to double-precision number
num2hex Convert singles and doubles to IEEE hexadecimal strings
cell2mat Convert cell array to numeric array
cell2struct Convert cell array to structure array
cellstr Create cell array of strings from character array
mat2cell Convert array to cell array with potentially different sized cells
num2cell Convert array to cell array with consistently sized cells
struct2cell Convert structure to cell array

データ型の決定

MATLABは、変数のデータ型を識別するためのさまざまな関数を提供します。

次の表は、変数のデータ型を決定するための機能を提供します-

Function Purpose
is Detect state
isa Determine if input is object of specified class
iscell Determine whether input is cell array
iscellstr Determine whether input is cell array of strings
ischar Determine whether item is character array
isfield Determine whether input is structure array field
isfloat Determine if input is floating-point array
ishghandle True for Handle Graphics object handles
isinteger Determine if input is integer array
isjava Determine if input is Java object
islogical Determine if input is logical array
isnumeric Determine if input is numeric array
isobject Determine if input is MATLAB object
isreal Check if input is real array
isscalar Determine whether input is scalar
isstr Determine whether input is character array
isstruct Determine whether input is structure array
isvector Determine whether input is vector
class Determine class of object
validateattributes Check validity of array
whos List variables in workspace, with sizes and types

次のコードでスクリプトファイルを作成します-

x = 3
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)

x = 23.54
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)

x = [1 2 3]
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)

x = 'Hello'
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)

ファイルを実行すると、次の結果が生成されます-

x = 3
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x = 23.540
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x =

          1          2          3

ans = 0
ans = 1
ans = 1
ans = 0
x = Hello
ans = 0
ans = 0
ans = 1
ans = 0
ans = 0