Main Content

構造体配列のコード生成

この例では、コード生成に適するように構造体配列を使用する MATLAB® 関数を作成する方法を示します。コードを生成するには、構造体を配列にする前に、まずそのスカラー テンプレート バージョンを作成しなければなりません。コード生成推論エンジンは、このスカラー値のデータ型を配列の基本データ型として使用します。

必要条件

この例には必要条件はありません。

関数 struct_array について

struct_array.m ファイルは構造体配列を使用します。

type struct_array
% y = struct_array(n)
% Take an input scalar number 'n' which will designate the size of the
% structure array return.
function y = struct_array(n) %#codegen

%   Copyright 2010-2013 The MathWorks, Inc.

assert(isa(n,'double')); % Input is scalar double

% To create a structure array you start to define the base scalar element
% first. Typically, we initialize all the fields with "dummy" (or zero)
% values so the type/shape of all its contents are well defined.
s.x = 0;
s.y = 0;
s.vx = 0;
s.vy = 0;

% To create a structure array of fixed size you can do this in multiple
% ways. One example is to use the library function 'repmat' which takes a
% scalar element and repeats it to its desired size.
arr1 = repmat(s, 3, 5); % Creates a 3x5 matrix of structure 's'

% At this point you can now modify the fields of this structure array.
arr1(2,3).x = 10;
arr1(2,3).y = 20;
arr1(2,4).x = 5;
arr1(2,4).y = 7;

% Another way of creating a structure array of fixed size is to use the
% concatenation operator.
arr2 = [s s s; s s s; s s s; s s s; s s s];

% If two variables agree on base type and shape you can copy one structure
% array to the other using standard assignment.
arr2 = arr1;

% To create a structure array of variable size with a known upper bound can
% be done in multiple ways as well. Again, we can use repmat for this, but
% this time we will add a constraint to the (non constant) input variable.
% This guarantees that the input 'n' of this function is less than or equal to 10.
assert(n <= 10);

% Create a row vector with at most 10 elements of structures based on 's'
arr3 = repmat(s, 1, n);

% Or we can use a for-loop with the concatenation operator. The compiler is
% unable to analyze that 'arr4' will be at most 10 elements big, so we
% add a hint on 'arr4' using coder.varsize. This will specify that the
% dimensions of 'arr4' is exactly one row with at most 10 columns. Look at
% the documentation for coder.varsize for further information.
coder.varsize('arr4', [1 10]);
arr4 = repmat(s, 1, 0);
for i = 1:n
    arr4 = [arr4 s];
end

% Let the top-level function return 'arr4'.
y = arr4;

MATLAB で構造体配列をビルドする場合は、通常は必要なだけのフィールドを追加します (たとえば、s(1).x = 10; s(2).y = 20; のように)。このように「動的」に構造体を作成する方法は、コード生成ではサポートされません。その理由の 1 つは、MATLAB では、構造体配列の 2 つの異なる要素について異なる構造体のフィールドをもつことができるため、静的な型推論のアプローチと競合するためです。したがって、最初に基本的なスカラー要素をすべて指定した後で、このすべて指定された要素から構造体にまとめる必要があります。この方法によって、構造体配列の 2 つの配列では常に同じ型 (フィールド) を共有します。

MEX 関数の生成

コマンドcodegenを使用して MEX 関数を生成し、コンパイルする MATLAB ファイルの名前を指定します。

codegen struct_array
Code generation successful.

既定で、codegen は、現在のフォルダーに struct_array_mex という名前の MEX 関数を生成します。これにより、MATLAB コードと MEX 関数をテストして結果を比較することができます。

MEX 関数の実行

struct_array_mex(10)
ans=1×10 struct array with fields:
    x
    y
    vx
    vy