Cookbook: Difference between revisions

Jump to navigation Jump to search
3 bytes added ,  23 August 2012
m
→‎Retrieve a field value from all entries in a struct array: fixing code, leftoverd from older example
m (→‎Problem: fix code, must be cell array)
m (→‎Retrieve a field value from all entries in a struct array: fixing code, leftoverd from older example)
Line 24: Line 24:
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


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


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:


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


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

Navigation menu