Cross-referencing fields in a struct

2 次查看(过去 30 天)
Nick
Nick 2014-1-7
编辑: Matt J 2014-1-8
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.

回答(2 个)

Matt J
Matt J 2014-1-7
num2cell(intersect(E1,cell2mat(BHV.AnalogData)))
  2 个评论
Matt J
Matt J 2014-1-7
编辑:Matt J 2014-1-7
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 2014-1-7
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.

请先登录,再进行评论。


Nick
Nick 2014-1-8
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 个评论
Matt J
Matt J 2014-1-8
编辑:Matt J 2014-1-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by