how to access the cell array ?
1 view (last 30 days)
Show older comments
patients =
'patient' 'status' 'genotype' 'genotype'
[ 1] [ 1] 'A' 'C'
[ 1] [ 1] 'E' 'F'
[ 2] [ 1] 'B' 'D'
[ 2] [ 1] 'E' 'G'
[ 3] [ 0] 'D' 'G'
[ 3] [ 0] 'I' 'K'
[ 4] [ 1] 'C' 'F'
[ 4] [ 1] 'E' 'K'
[ 5] [ 0] 'B' 'C'
[ 5] [ 0] 'D' 'G'
this is my cell array of five patients .i need to select the patient with status one only and display them .
0 Comments
Accepted Answer
Star Strider
on 10 Oct 2015
Assuming this is your cell array structure, the ‘Pts_Status_1’ array will return the correct patient information:
patients = {'patient' 'status' 'genotype' 'genotype'
[ 1] [ 1] 'A' 'C'
[ 1] [ 1] 'E' 'F'
[ 2] [ 1] 'B' 'D'
[ 2] [ 1] 'E' 'G'
[ 3] [ 0] 'D' 'G'
[ 3] [ 0] 'I' 'K'
[ 4] [ 1] 'C' 'F'
[ 4] [ 1] 'E' 'K'
[ 5] [ 0] 'B' 'C'
[ 5] [ 0] 'D' 'G'};
Pts_Status_1 = patients(cellfun(@(x)isequal(x,1), patients(:,2)), :)
Pts_Status_1 =
[1] [1] 'A' 'C'
[1] [1] 'E' 'F'
[2] [1] 'B' 'D'
[2] [1] 'E' 'G'
[4] [1] 'C' 'F'
[4] [1] 'E' 'K'
1 Comment
Star Strider
on 10 Oct 2015
My pleasure.
The Pts_Status_1 assignment uses the cellfun function to compute with the cell array (since cell arrays can be difficult to address directly). Here, it uses the isequal function to compare the second column of your ‘patients’ array with 1, and creates a logical index vector (made up of logical true-false, or 1-0 elements), chooses only those that are ‘true’, and then outputs the entire row. Another way of coding it would be:
index = cellfun(@(x)isequal(x,1), patients(:,2));
Pts_Status_1 = patients(index, :)
I chose to do it in one line instead. Both will work, but experimenting with the two-line version will let you see how the code works.
For a full description of how the code works, see the documentation for the functions cellfun and isequal, and the documentation on ‘Anonymous Functions’ and Array Indexing, specifically ‘logical indexing’.
More Answers (0)
See Also
Categories
Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!