How do I append to an array within a cell during a loop?

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 comentarios

dpb
dpb el 6 de Sept. de 2022
Editada: dpb el 6 de Sept. de 2022
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.
Thanks for your reply. I hope the pictures attached show how the data is constructed. The first pic shows the files (i) in column 1, row 1. The second pic shows that data, but it contains multiple trials(j), I need the data per trial. The problem is that in the third step, calculating a specific type of reaction time, there will be a variable amount of those reaction times per trial..... which I did not manage to store in a single cell. I did manage to get the data I need in the end though, by making a 3d, then 2d matrix.
dpb
dpb el 7 de Sept. de 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...
dpb
dpb el 7 de Sept. de 2022
Editada: dpb el 7 de Sept. de 2022
tP=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1119060/participant_2_data_2019-05-16_11-59.csv');
head(tP)
ans = 8×32 table
SubjectNr RadiusCircle StandardDeviationOfNoise CurrentTime TrialTime VisitTime BlockNumber TrialNumber Experiment TrackingTaskPresent TypingTaskPresent TrackerWindowVisible TypingWindowVisible TrackingWindowEntryCounter TypingWindowEntryCounter CursorCoordinatesX CursorCoordinatesY JoystickAxisX JoystickAxisY EnteredDigits EnteredDigitsLength GeneratedTypingTaskNumbers GeneratedTypingTaskNumberLength NumberOfCircleExits TrialScore VisitScore CorrectDigitsVisit IncorrectDigitsVisit IncorrectDigitsTrial OutsideRadius EventMessage1 EventMessage2 _________ ____________ ________________________ ___________ _________ _________ ___________ ___________ ______________________ ___________________ _________________ ____________________ ___________________ __________________________ ________________________ __________________ __________________ _____________ _____________ _____________ ___________________ __________________________ _______________________________ ___________________ __________ __________ __________________ ____________________ ____________________ _____________ ___________________ _____________ 2 100 -1 3.6794 1.558e+09 {'-'} 0 0 {'singleTaskTracking'} 1 1 1 1 0 0 928.33 275 0 0 {0×0 char} {'0'} {'123456789'} {'9'} 0 0 0 0 0 0 0 {'MousePressed' } {'431_141'} 2 120 3 31.953 0.016 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 928.33 275 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trialStart' } {'-' } 2 120 3 31.999 0.062 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 926.82 275.33 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' } 2 120 3 32.034 0.097 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 925.69 274.82 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' } 2 120 3 32.069 0.132 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 929.02 277.22 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' } 2 120 3 32.105 0.168 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 932.19 277.44 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' } 2 120 3 32.142 0.205 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 936.74 279.95 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' } 2 120 3 32.178 0.241 {'-'} 1 1 {'practiceTracking' } 1 0 1 0 1 0 935.59 281.55 0 0 {'-' } {'-'} {'-' } {'-'} 0 0 0 0 0 0 0 {'trackingVisible'} {'-' }
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...
ans = 16×4 table
Experiment TrialNumber GroupCount Mean_TrialTime ______________________ ___________ __________ ______________ {'dualTask' } 9 361 45.547 {'dualTask' } 10 338 47.027 {'dualTask' } 11 313 46.189 {'dualTask' } 14 390 41.812 {'dualTask' } 15 331 47.839 {'dualTask' } 16 307 45.171 {'dualTask' } 19 341 46.139 {'dualTask' } 20 337 45.962 {'singleTaskTracking'} 27 288 5.0695 {'singleTaskTracking'} 32 287 5.0913 {'singleTaskTyping' } 8 55 10.421 {'singleTaskTyping' } 13 58 9.7827 {'singleTaskTyping' } 18 54 10.039 {'singleTaskTyping' } 23 52 10.618 {'singleTaskTyping' } 28 51 9.926 {'singleTaskTyping' } 33 52 10.05
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.
Thank you very much for all the work you're putting into this. I'm afraid we're starting to solve a problem that isn't really there though....
"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."
The raw data files (the participant 2 and 3 data attached to the previous post) contain continuous data from which I needed to extract Rts, time on task etc., and put those in a format that somebody else can run analyses on. They wanted the results file to be structured this way (rows = participant or file, and then multiple columns for the variables of interest (like RTs), a column for each trial). Presumably because this structure allows for RM-Anovas in SPSS. The exact calculations I needed are really complicated to explain and this is probably not the place to do so, but that's not really where I had the problem in the first place.
I already have the data file that was requested of me. During the process I just got stuck on the fact that I could not put mulitple values in a single cell during a loop (within 2 other loops, which migh have been the difficulty). It would have saved me a lot of time if that were possible, instead of having to work with a 3d matrix, with which I had no experience and was kinda hard to get my head around.
Again, thank you very much for your time though, and I will keep in mind that if possible it's better to use the tablemethods you show above.
dpb
dpb el 8 de Sept. de 2022
Editada: dpb el 8 de Sept. de 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.
It's both across files/subjects and trials. So per subject the same variable (like mean RT) can't be reduced to a single mean, they need to be given a separate column per trial. Which makes the data structure so convoluted since there are more than 20 trials. When you now what to look for this structure is probably exactly what you need to simply copy paste in Spss.
Again, thanks for the help though, I will keep the proper practices you mentioned in mind.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Sept. de 2022

Comentada:

el 9 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by