1

So far I have this,

Order = struct('Name',{},'Item',{},'Quantity',{},'DueDate',{});
Order(1).Name = 'Order 1'; Order(1).Item = 'Rolo'; Order(1).Quantity = '1'; Order(1).DueDate = '735879';
Order(1).Name = 'Order 1'; Order(1).Item = 'Trident'; Order(1).Quantity = '2'; Order(1).DueDate = '735887';
Order(2).Name = 'Order 2'; Order(2).Item = 'Hershey';Order(2).Quantity = '3'; Order(2).DueDate = '735875';
Order(3).Name = 'Order 3'; Order(3).Item = 'Kitkat'; Order(3).Quantity = '6'; Order(3).DueDate = '735890';

Within each order, there are multiple items and quantities of items, so I would like each struct array for each order to be able to hold multiple items, quantities, and due dates of orders.

Thank you!

1
  • Which Matlab version are you using? Commented Sep 29, 2014 at 14:55

1 Answer 1

1

The best option is to use table() (or dataset() if your Matlab version is older than 2014a but you have the Statistics toolbox):

Order = table({'Order 1';'Order 2';'Order 3'},...
              {'Trident';'Hershey';'Kitkat'},...
              [2; 3; 6],...
              [735887; 735875; 735890],...
              'VariableNames',{'Name','Item','Quantity','DueDate'})

Order = 
      Name         Item       Quantity    DueDate
    _________    _________    ________    _______
    'Order 1'    'Trident'    2           735887 
    'Order 2'    'Hershey'    3           735875 
    'Order 3'    'Kitkat'     6           735890 

You can access it as you would do with a structure but you have more advantages, e.g. accessing and inspecting data is easier, smaller memory footprint etc..


What you are trying to build 'manually' is a structure array (and let me stress the array here):

% A structure array
s = struct('Name',     {'Order 1';'Order 2';'Order 3'},...
           'Item',     {'Trident';'Hershey';'Kitkat'},...
           'Quantity', {2; 3; 6},...
           'DueDate',  {735887; 735875; 735890});

s = 
3x1 struct array with fields:
    Name
    Item
    Quantity
    DueDate

Each scalar structure (/unit/record/object/member call it how you like) of the array will have a set of properties:

s(1)
ans = 
        Name: 'Order 1'
        Item: 'Trident'
    Quantity: 2
     DueDate: 735887

The organization of the data looks intuitive. However, if you want to apply operations across the whole array, e.g. select those which have Quantity > 2, you need to first concatenate the whole field into a temporary array and only then apply your operation, and in the worst case scenario (if you nest the fields) you will have to loop.

I do personally prefer a database/dataset/table approach where each record is a row and columns are the properties. You can do this by flattening the structure array into a scalar structure (pay attention to the braces):

% A flat structure
s = struct('Name',     {{'Order 1';'Order 2';'Order 3'}},...
           'Item',     {{'Trident';'Hershey';'Kitkat'}},...
           'Quantity', [2; 3; 6],...
           'DueDate',  [735887; 735875; 735890]);
s = 
        Name: {3x1 cell}
        Item: {3x1 cell}
    Quantity: [3x1 double]
     DueDate: [3x1 double]

Even though the data organization does't appear as intuitive as previously, you will be able to index directly into the structure (and will have lower memory footprint).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.