cs0815
cs0815

Reputation: 17388

codegen and scalar struct argument

Codegen commands like this:

codegen -config:dll ep1 -args single(0) ep2 -args {0,0}

are easy for functions like this:

function y = ep1(u) %#codegen
y = u;

function y = ep2(u, v) %#codegen
y = u + v;

I am just wondering how to define scalar struct arguments. Let us assume that the above argument u should look like this (i.e. consist of 2 fields - one char and one double):

u.FirstName = 'Loren';
u.Height = 150

PS:

Just found something like this:

s = struct('a',42,'b',4711);
codegen topfun.m -args { s }

would this be a solution? I believe, this is a definition by example.

Upvotes: 4

Views: 1273

Answers (2)

Nils Lande
Nils Lande

Reputation: 889

The definitions of the structs can be captured using the coder.typeof function.

First initialise the structs using a script, capture the struct definitions using coder.typeof and then pass the definitions using -args as shown below:

u = Initialise(u);
p = InitialiseParameters();

tx = coder.typeof(u);
tp = coder.typeof(p);

codegen topfun.m -args {tx, tp} -config:dll 

Upvotes: 0

cs0815
cs0815

Reputation: 17388

This seems to work for me:

codegen -report -config:dll ep1 ...
                            ep2

The actual 'function signature' (and thus scalar struct) can be defined in the function like this:

function [bla] = ep1(parameters)
%#codegen

assert(isstruct(parameters));
assert(isa(parameters.x1,'char'));
assert(size(parameters.x1, 1) >= 1);
assert(size(parameters.x1, 1) <= 1024);
assert(isa(parameters.x2,'double'));
...

Problem I have is that I seem to have to define all the parameters in the parent functions even-though I do not use the parameter in the parent function.

Looking at the stuff above. Let us say ep1 is the parent function of ep2 and ep1 does not use parameters.x2, I still have to assert it in ep1.Hope this makes sense.

Upvotes: 1

Related Questions