How do I define a structure that has an element which is an array?
Show older comments
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 Comments
Arnab De
on 7 Dec 2012
Do you just want to run the MATLAB code or do you want to generate C code from it?
Wayne Prather
on 7 Dec 2012
Sean de Wolski
on 7 Dec 2012
Good thread +1
Accepted Answer
More Answers (3)
John Petersen
on 6 Dec 2012
Edited: John Petersen
on 6 Dec 2012
1. Find out which assertion fails. The code looks fine.
2. Replace the 'for' loop with
y = sum([personnel.name.salary]);
Jan
on 7 Dec 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
on 7 Dec 2012
total_payroll = total_payroll + personnel.name(i).salary;
total_payroll is never defined before being used for the first time!
1 Comment
Wayne Prather
on 7 Dec 2012
Categories
Find more on Algorithm Design Basics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!