Using a loop to add rows to a struct, but the counter overwrites the rows.
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;
endI 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 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 loops MATLAB Answers — New Questions