Using a loop to add rows to a struct, but the counter overwrites the rows.
2 views (last 30 days)
Show older comments
I am using this loop, which is embeded in another loop which loads 3 files, to add rows to a struct. After it itterates through 1 files, the rows get overwritten starting from 1. I know this is because of the counter, but I just can't wrap my head around how have it start after the last entry. Any help would be greatly appreciated!
for structRow = 1:nTrials
masterStruct(structRow).mouse = file.subjectNumber;
masterStruct(structRow).date = matfiles.name;
masterStruct(structRow).trialEnd = trials(structRow).trialEnd
masterStruct(structRow).reactTimeMS = trials(structRow).reactTimeMS;
masterStruct(structRow).rewardUL = trials(structRow).rewardUL;
masterStruct(structRow).stimulusType = trial.stimulusType;
masterStruct(structRow).preStimMS = trial(structRow).preStimMS;
masterStruct(structRow).visualDurMS = trial(structRow).visualDurMS;
masterStruct(structRow).optiDurMS = trial(structRow).optiDurMS;
masterStruct(structRow).minPowerMW = blockInfo(structRow).minPowerMW;
masterStruct(structRow).maxPowerMW = blockInfo(structRow).maxPowerMW;
masterStruct(structRow).minContrastPC = blockInfo(structRow).minContrastPC;
masterStruct(structRow).maxContrastPC = blockInfo(structRow).maxContrastPC;
end
0 Comments
Answers (2)
Torsten
on 14 May 2024
Edited: Torsten
on 15 May 2024
I don't know how "trial" and "blockInfo" are organized. Here is one suggestion:
for ifile = 1:3
for s = 1:nTrials
structRow = (i-1)*nTrials + s;
masterStruct(structRow).mouse = file.subjectNumber;
masterStruct(structRow).date = matfiles.name;
masterStruct(structRow).trialEnd = trials(s).trialEnd
masterStruct(structRow).reactTimeMS = trials(s).reactTimeMS;
masterStruct(structRow).rewardUL = trials(s).rewardUL;
masterStruct(structRow).stimulusType = trial.stimulusType;
masterStruct(structRow).preStimMS = trial(s).preStimMS;
masterStruct(structRow).visualDurMS = trial(s).visualDurMS;
masterStruct(structRow).optiDurMS = trial(s).optiDurMS;
masterStruct(structRow).minPowerMW = blockInfo(s).minPowerMW;
masterStruct(structRow).maxPowerMW = blockInfo(s).maxPowerMW;
masterStruct(structRow).minContrastPC = blockInfo(s).minContrastPC;
masterStruct(structRow).maxContrastPC = blockInfo(s).maxContrastPC;
end
end
2 Comments
Torsten
on 15 May 2024
The answer remains almost the same:
structRow = 0;
for fileNum = 1:nFiles
load(matfiles(fileNum).name)
nTrials = length(trials);
for s = 1:nTrials
structRow = structRow + 1;
masterStruct(structRow).mouse = file.subjectNumber;
masterStruct(structRow).date = matfiles.name;
masterStruct(structRow).trialEnd = trials(s).trialEnd
masterStruct(structRow).reactTimeMS = trials(s).reactTimeMS;
masterStruct(structRow).rewardUL = trials(s).rewardUL;
end
end
Shivani
on 15 May 2024
After analysing the code snippet shared in the question, it is my understanding that you are encountering this error because the index ‘structRow’ used to add rows to ‘masterStruct’ is reset to 1 for each new file processed in the loop. This means that for each file, the code is filling ‘masterStruct’ from the first row again, thereby overwriting the data added from previous files.
This can be resolved by adding a separate counter that does not reset with each new file. Instead, it continuously increments across all files and trials. This way, each new piece of data is added to the next available position in ‘masterStruct’, preventing any overwriting. Kindly refer to the code snippet below for an example on how this can be implemented.
masterStructIndex = 1;
for fileNum = 1:nFiles
load(matfiles(fileNum).name);
nTrials = length(trials);
for structRow = 1:nTrials
masterStruct(masterStructIndex).mouse = file.subjectNumber;
masterStruct(masterStructIndex).date = matfiles.name;
masterStruct(masterStructIndex).trialEnd = trials(structRow).trialEnd;
masterStruct(masterStructIndex).reactTimeMS = trials(structRow).reactTimeMS;
masterStruct(masterStructIndex).rewardUL = trials(structRow).rewardUL;
masterStructIndex = masterStructIndex + 1;
end
end
Hope this resolves the error encountered!
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!