Cross-referencing fields in a struct

2 views (last 30 days)
Nick
Nick on 7 Jan 2014
Edited: Matt J on 8 Jan 2014
I have a struct called BHV which contains a number of fields. I am interested in four of them.
TrialNumber: [203x1 double] % containing a sequence of trial numbers 1 to 203
ConditionNumber: [203x1 double] % which movie was shown on that trial, 1, 2, 3, 4, 5 or 6
TrialError: [203x1 double] % whether correct (0) or error (1) or aborted (2)
AnalogData: {1x203 cell} % eye movement data for each trial
The following code
n=1:203;
TrialCode = BHV.TrialError(n); % the error code for each trial
Condition = BHV.ConditionNumber(n); % which movie
E1=find(TrialCode==0 & Condition==1);
will create an array with only the TrialNumbers that correspond to error code 0 (correct) and movie 1. I would next like to make a cell array, a subset of AnalogData, that contains just the cells corresponding to those trials in E1. I would be grateful for any helpful suggestions.

Answers (2)

Matt J
Matt J on 7 Jan 2014
num2cell(intersect(E1,cell2mat(BHV.AnalogData)))
  2 Comments
Matt J
Matt J on 7 Jan 2014
Edited: Matt J on 7 Jan 2014
Although, it is questionable that you are using cell arrays to hold data that could be held in vectors. It just slows things down and necessitates lots of cell2mat, num2cell conversions.
Nick
Nick on 7 Jan 2014
Many thanks for the amazing code, I would have never worked that out on my own. Sadly it fails, because on closer examination, the cells in AnalogData are structures which themselves contain structures and fields, of which fields one named EyeSignal contains the relevant information (a two-column array of numbers). The following
BHV.AnalogData{1,200}.EyeSignal
gives the eye movement data for trial 200. However, I am still struggling to get the formula to work with the contents of the cells being stuctures.
Questionable, yes - I expect there are more elegant ways of using Matlab than what I am capable of.

Sign in to comment.


Nick
Nick on 8 Jan 2014
I am still unable to make the above work. I have instead gone halfway with this:
Movie1Data = BHV.AnalogData(E1);
which simplifies the problem. Let's say the resulting cell array Movie1Data contains just two cells, each with the following struct
EyeSignal: [3552x2 double]
General: [1x1 struct]
PhotoDiode: []
How can I retrieve the EyeSignal fields from these cells?
  1 Comment
Matt J
Matt J on 8 Jan 2014
Edited: Matt J on 8 Jan 2014
How can I retrieve the EyeSignal fields from these cells?
Retrieve them in what form? Do you want the EyeSignal data from the two different cells concatenated into a 3552x4 array? If so
result = [Movie1Data.EyeSignal]
If that's not it, I think you need to start a fresh thread explaining the data extraction you're trying to do in full and from scratch.
I also recommend you reconsider your data organization. It looks like a nightmare. Structs inside cells inside structs... Among other things, it never makes sense to have a cell array where the cells contain scalar structs with the same fields. That's what struct arrays are for.

Sign in to comment.

Categories

Find more on Structures 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!