How do I define a structure that has an element which is an array?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Fred Smith
el 7 de Dic. de 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 comentario
Más respuestas (3)
John Petersen
el 6 de Dic. de 2012
Editada: John Petersen
el 6 de Dic. de 2012
1. Find out which assertion fails. The code looks fine.
2. Replace the 'for' loop with
y = sum([personnel.name.salary]);
0 comentarios
Jan
el 7 de Dic. de 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])
0 comentarios
Sean de Wolski
el 7 de Dic. de 2012
total_payroll = total_payroll + personnel.name(i).salary;
total_payroll is never defined before being used for the first time!
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!