주요 콘텐츠

구조체형 배열에 대한 코드 생성하기

이 예제에서는 코드 생성에 적합하도록 구조체형 배열을 사용하는 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와 같습니다. 이러한 "동적" 구조체 생성 스타일은 코드 생성 시 지원되지 않습니다. 그 이유 중 하나는 MATLAB에서 구조체형 배열의 서로 다른 두 요소에 대해 서로 다른 구조체 필드가 있을 수 있기 때문인데, 이는 이보다 정적인 유형 추론 방식과 충돌합니다. 따라서 먼저 기본 스칼라 요소를 완전히 지정한 다음 이 완전히 지정된 요소에서 구조체형 배열을 확장해야 합니다. 이 방법은 구조체형 배열의 두 요소가 항상 동일한 유형(필드)을 공유하도록 보장합니다.

MEX 함수 생성하기

codegen 명령 다음에 컴파일할 MATLAB 파일의 이름을 사용하여 MEX 함수를 생성합니다.

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