How do I append to an array within a cell during a loop?
Show older comments
I'm trying to calculate average reaction times from multiple (zz) reaction times, within trials(j), within files(i). Ideally I would first like the reaction times to be added to an array within each cell belonging to the particular file and trial, making the calculation of averages easy. But I can't get the code to do this, it just overwrites the cell every iteration.
As another option I'm now trying a workaround using a 3d matrix, and then removing the third dimension, by averaging the non-zero values along it, which I also can't get to work. Instead I'm now using a loop to get the data I need from a concatenated 3d to 2d matrix.
Stil, I feel it should be possible to add values to an array within a cell, instead of being forced to make a 3d matrix....Am I missing something?
Would appreciate any help, thank you!
StartTime = End + 1;
RTNumbers = zeros(1,length(StartTime));
for zz = 1:length(StartTime)
if isnan(A1(:,6)) | ~ismember(1,A1(:,5)) | StartTime(zz)>length(A1) | ~any(A1(:,6) == 1)
continue
end
DigitStart = A1(StartTime(zz),6);
DigitEnd2 = DigitStart+1;
if DigitEnd2 > max(A1(:,6))
continue
end
ButtonPress = find(A1(:,6)==DigitEnd2,1);
TimeStart(zz) = A1(StartTime(zz),2);
TimeEnd(zz) = A1(ButtonPress,2);
RT{i,j,zz} = [TimeEnd(zz) - TimeStart(zz)];
RT2 = reshape(permute(RT,[1,3,2]),[],size(RT,2));
end
8 Comments
Sounds like this has gone off the rails from the git-go; instead of trying to patch some convoluted storage that appears less than ideal, how about giving us the data structure within the files you're trying to process?
The solution instead I think would be to build a composite table and use grouping variables over the reaction times, trials, ... either within a given file or across all files; that part is unclear what is intended/wanted.
Skippy99
on 7 Sep 2022
dpb
on 7 Sep 2022
We can't do a thing with images -- attach sample files.
I'm sure the problem could be solved in a few lines of code with grouping variables but need to be able to read enough to be able to construct the table...
Skippy99
on 7 Sep 2022
tP=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1119060/participant_2_data_2019-05-16_11-59.csv');
head(tP)
reads the participant file in quite readily -- so now what it is that you actually want/need to do with it?
I'd turn variables like "Experiment" into categorical variables and it's not clear what "TrialTime" is between the first and subsequent entries, and whether or not 'VisitTime' is/should be a duration variable with missing or zero values for some cases, but those are minor details to be resolved.
If there are multiple subjects and the issue is to either aggregate by or across subject, then compiling the data across them is the next step -- if there are some number of these 30-something variables that aren't of interest, the expedient thing is to only import those which are of interest for the present analyses to conserve some memory, but again that's a detail.
What we now need is a problem definition to turn to code not obfuscated by all the machinations gone through in the other m-file.
Perhaps we can make an illustrative start from the statement of the original Q? that read "calculate average reaction times from multiple (zz) reaction times, within trials(j), within files(i)."
We've only got one file, but from what I can see it looks like
tAvgByTrial_Exper=groupsummary(tP,{'Experiment','TrialNumber'},@mean,{'TrialTime'});
tAvgByTrial_Exper.Properties.VariableNames=strrep(tAvgByTrial_Exper.Properties.VariableNames,'fun1','Mean');
[head(tAvgByTrial_Exper); tail(tAvgByTrial_Exper)] % show first, last results...
would be at least one stab at it. This can be refined as needed, of course, but certainly is much simpler than building the convoluted data structure.
Skippy99
on 8 Sep 2022
One of my personal goals in the forum is to try to minimize the spread of bad practice in MATLAB; from the use of eval and named variables to poor data structure.
You may be in a situation where you're forced by this "somebody" being in a position of power, but I'd urge you to pick up the mantle and illustrate they're on the wrong track, here, and will have a devil of a time doing whatever analyses they intend with such a convoluted data structure; and, furthermore, will be continuing to need to create such byzantine messes uniquely for every case that comes up. Instead, if they simply organize the data in the table, all those issues will go away and they can then concentrate on the actual problem instead.
NB: It still wasn't clear to me whether all the analyses are by or across the files/subjects; it the latter, the table is still the way to go; just create the indicator variable when importing -- unless there are a tremendous number you should be able to hold all in memory. It may be that you do give the user a variable selector popup menu to not bring in 20-something variables that aren't going to be used, but that's a trivial exercise to implement.
Skippy99
on 9 Sep 2022
Answers (0)
Categories
Find more on Loops and Conditional Statements 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!
