Cookbook: Difference between revisions

Jump to navigation Jump to search
(→‎Structures: use a less morbid example)
Line 6: Line 6:
You have a struct array with multiple fields, and you want to acess the value from a specific field from all elements. For example, you want to return the age from all pacients in the following case:
You have a struct array with multiple fields, and you want to acess the value from a specific field from all elements. For example, you want to return the age from all pacients in the following case:


   cases(1).name        = "Bob";
   samples = struct ("patient", {"Bob", "Kevin", "Bob" , "Andrew"},
  cases(1).age         = 45;
                    "age",    [ 45  ,  52    ,  45  ,  23    ],
   cases(1).contaminated = true;
                    "protein", {"H2B", "CDK2" , "CDK2", "Tip60" },
 
                    "tube, [ 3  ,  5    ,  2   ,  18    ]
  cases(2).name        = "Andrew";
                    );
  cases(2).age          = 21;
  cases(2).contaminated = true;
 
  cases(2).name        = "Kevin";
   cases(2).age          = 24;
  cases(2).contaminated = false;


==== Solution ====
==== Solution ====
Indexing the struct returns a comma separated list so use them to create a matrix.
Indexing the struct returns a comma separated list so use them to create a matrix.


   [cases(:).age]
   [samples(:).age]


This however does not keep the original structure of the data, instead returning all values in a single column. To fix this, use {{Codeline|reshape()}}.
This however does not keep the original structure of the data, instead returning all values in a single column. To fix this, use {{Codeline|reshape()}}.


   reshape ([cases(:).age], size (cases))
   reshape ([samples(:).age], size (samples))


==== Discussion ====
==== Discussion ====
Returning all values in a comma separated lists allows you to make anything out of them. If numbers are expected, create a matrix by enclosing them in square brackets. But if strings are to be expected, a cell array can also be easily generated with curly brackets
Returning all values in a comma separated lists allows you to make anything out of them. If numbers are expected, create a matrix by enclosing them in square brackets. But if strings are to be expected, a cell array can also be easily generated with curly brackets


   {cases(:).name}
   {samples(:).name}


You are also not limited to return all elements, you may use logical indexing from other fields to get values from the others:
You are also not limited to return all elements, you may use logical indexing from other fields to get values from the others:


   [cases([cases(:).age] > 34).contaminated]         ## return contaminated state from all cases older than 34
   [samples([samples(:).age] > 34).tube]                 ## return tube numbers from all samples from patients older than 34
   [cases(strcmp({cases(:).protein}, "CDK2").tube]  ## return all tube numbers with protein CDK2
   [samples(strcmp({samples(:).protein}, "CDK2").tube]  ## return all tube numbers for protein CDK2


== Input/output ==
== Input/output ==