Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

coder.ClassType クラス

名前空間: coder
スーパークラス: coder.ArrayType

入力仕様に対して許容される MATLAB クラスのセットを表す

説明

coder.ClassType のオブジェクトは、生成コードが受け入れる値クラス オブジェクトを指定します。このクラスのオブジェクトは、関数 fiaccel-args オプションでのみ使用します。生成された MEX 関数に入力として渡さないでください。

作成

t = coder.typeof(classObject) は、classObjectcoder.ClassType オブジェクトを作成します。

t = coder.newtype(className) は、クラス className のオブジェクトの coder.ClassType オブジェクトを作成します。

メモ

coder.Type オブジェクトの作成と編集は、コード生成の型エディターを使用して対話形式で行うことができます。コード生成の型エディターを使用した入力の型の作成と編集を参照してください。

入力引数

すべて展開する

coder.ClassType オブジェクトを作成する値クラス オブジェクト。この入力は値クラスのオブジェクトとして評価される式です。

MATLAB パス上にある値クラス定義ファイルの名前。文字ベクトルまたは string スカラーとして指定します。

プロパティ

値クラス オブジェクト vcoder.typeof に渡して coder.ClassType オブジェクト t を作成すると、t のプロパティは Constant 属性が false に設定された v のプロパティと同じになります。

同様に、値クラス オブジェクトの名前 vcoder.newtype に渡して coder.ClassType オブジェクト t を作成すると、t のプロパティは Constant 属性が false に設定された v のプロパティと同じになります。

すべて折りたたむ

この例では、ワークスペースでオブジェクト例に基づく型オブジェクトを作成する方法を示します。

値クラス myRectangle を作成します。

type myRectangle.m
classdef myRectangle
    properties
        length;
        width;
    end
    methods
        function obj = myRectangle(l,w)
            if nargin > 0
                obj.length = l;
                obj.width = w;
            end
        end
        function area = calcarea(obj)
            area = obj.length * obj.width;
        end
    end
end

myRectangle のオブジェクトを入力として取得する関数を作成します。

type getarea.m
function z = getarea(r)
%#codegen
z = calcarea(r);
end

myRectangle のオブジェクトを作成します。

v = myRectangle(1,2)
v = 
  myRectangle with properties:

    length: 1
     width: 2

v に基づく coder.ClassType オブジェクトを作成します。

t = coder.typeof(v)
t = 
coder.ClassType
   1×1 myRectangle   
      Properties : 
      	length : 1×1 double
      	width  : 1×1 double

coder.typeof は、v と同じ名前と型のプロパティをもつ coder.ClassType オブジェクトを作成します。

getarea のコードを生成します。coder.ClassType オブジェクト t-args オプションに渡すことで、入力の型を指定します。

codegen getarea -args {t} -report
Code generation successful: To view the report, open('codegen/mex/getarea/html/report.mldatx')

この例では、coder.newtype を使用して値クラス mySquare のオブジェクトの coder.ClassType オブジェクトを作成する方法を示します。

1 つのプロパティ side をもつ値クラス mySquare を作成します。

type mySquare.m
classdef mySquare
    properties
        side;
    end
    methods
        function obj = mySquare(val)
            if nargin > 0
                obj.side = val;
            end
        end
        function a = calcarea(obj)
            a = obj.side * obj.side;
        end
    end
end

プロパティの値は割り当てずに mySquare の型 coder.ClassType を作成します。

t = coder.newtype('mySquare')
t = 
coder.ClassType
   1×1 mySquare -- class with no properties

tmySquare のプロパティが確実に割り当てられるように、t.Properties を使用して side の型を指定します。

t.Properties.side = coder.typeof(2)
t = 
coder.ClassType
   1×1 mySquare   
      Properties : 
      	side : 1×1 double

ヒント

  • coder.ClassType の作成後、プロパティのタイプを変更できます。たとえば、オブジェクト tprop1 プロパティと prop2 プロパティの型を変更します。

    t = coder.typeof(myClass)
    t.Properties.prop1 = coder.typeof(int16(2));
    t.Properties.prop2 = coder.typeof([1 2 3]);

  • coder.ClassType オブジェクトの作成後、プロパティを追加できます。たとえば、オブジェクト tnewprop1 プロパティと newprop2 プロパティを追加します。

    t = coder.typeof(myClass)
    t.Properties.newprop1 = coder.typeof(int8(2));
    t.Properties.newprop2 = coder.typeof([1 2 3]);

  • コードを生成するときに関数 codegen に渡す coder.ClassType オブジェクトのプロパティは、クラス定義ファイル内のプロパティと一致していなければなりません。ただし、コードで使用されていないプロパティがクラス定義ファイルに存在する場合、coder.ClassType オブジェクトはこれらのプロパティを含める必要はありません。コード ジェネレーターは、コードで使用しないプロパティを無視します。

バージョン履歴

R2017a で導入