How do I define a structure that has an element which is an array?

1 visualizzazione (ultimi 30 giorni)
How do I define a structure that has an element which is an array and for which there are sub elements. I have composed a simple example below of what I want to do along with a failed attempt at the needed assert statements.
function y = payroll(personnel)
%#codegen
% Specify the class of the input as struct.
assert(isstruct(personnel));
% Specify the class and size of the fields r and i
% in the order in which you defined them.
assert(isa(personnel.num_employees,'int32'));
assert(isa(personnel.employee,'int32'));
assert(all(size(personnel.employee) == [5 1]));
assert(isa(personnel.employee(1).salary,'int32'));
for i = personnel.num_employees
total_payroll = total_payroll + personnel.name(i).salary;
end
y = total_payroll;
end
  3 Commenti
Wayne Prather
Wayne Prather il 7 Dic 2012
My purpose is to generate C code using Matlab Coder. I get an error related to my failure to properly define the employee element of my structure since it is an array and has a sub element uder it.

Accedi per commentare.

Risposta accettata

Fred Smith
Fred Smith il 7 Dic 2012
This line seems wrong:
assert(isa(personnel.employee,'int32'));
In your code employee is supposed to be a struct with sub-field name and salary? Once you make it a struct, you then also need to set its class and size of the fields name and salary.
-Fred
  1 Commento
Wayne Prather
Wayne Prather il 7 Dic 2012
It finally works. Apparently you have to define the arrayed sub elements in the same way that you define the main structure. The following Matlab code works and succesfully produces the C++ code that follows.
Matlab code:
function y = payroll(personnel)
%#codegen
% Specify the class of the input as struct.
assert(isstruct(personnel));
% Specify the class and size of the fields r and i
% in the order in which you defined them.
assert(isa(personnel.num_employees,'int32'));
assert(isstruct(personnel.employee));
assert(all(size(personnel.employee) == [5 1]));
assert(isa(personnel.employee(1).salary,'double'));
total_payroll = 0;
for i = 1 : personnel.num_employees
total_payroll = total_payroll + personnel.employee(i).salary;
end
y = total_payroll;
end
Matlab Coder produced C++ code:
real_T payroll(const b_struct_T personnel)
{
real_T y;
int32_T i;
y = 0.0;
for (i = 1; i <= personnel.num_employees; i++) {
y += personnel.employee[i - 1].salary;
}
return y;
}

Accedi per commentare.

Più risposte (3)

John Petersen
John Petersen il 6 Dic 2012
Modificato: John Petersen il 6 Dic 2012
1. Find out which assertion fails. The code looks fine.
2. Replace the 'for' loop with
y = sum([personnel.name.salary]);

Jan
Jan il 7 Dic 2012
This will crash, if the personnel.employee has more than 2 dimensions:
all(size(personnel.employee) == [5 1])
Better:
isequal(size(personnel.employee), [5 1])

Sean de Wolski
Sean de Wolski il 7 Dic 2012
total_payroll = total_payroll + personnel.name(i).salary;
total_payroll is never defined before being used for the first time!
  1 Commento
Wayne Prather
Wayne Prather il 7 Dic 2012
Thanks, you are correct. I forgot to initialize it. The code is just a made up simplistic example that addresses the problem I am having in my real code in regards to defining a structure with elements which are arrays for the C++ code generation.

Accedi per commentare.

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by