How to specify input arguments of a structure in a 'for' loop separate to field argument
2 views (last 30 days)
Show older comments
Hi,
I am using MATLAB R2020a on a MacBook Air. I am trying to record the results of an experiment such that the results are inserted after each trial. This involves placing the field argument of the structure separate to the input arguments for the structure in a for loop iterating from 1 to 'n' number of trials like this (I have only included some of the input arguments for simplicity):
resultsMatrix = struct('TrialOrder', {}, 'TrialType', {}, 'S1Numbers', {}, 'S1Mean', {}, 'S1Variance', {}, 'S1Position', {}, 'S2Number', {}, 'S2Numbers', {}, 'S2Mean', {}, 'S2Variance', {}, 'S2Position', 'S3Number', {}, 'S3Numbers', {}, 'S3Mean', {}, 'S3Variance', {}, 'S3Position', {}, 'Key1', {}, 'Timeq1', {}, 'Srightnumber', {}, 'Srightnumbers', {}, 'Srightmean', {}, 'Srightvariance', {}, 'Sleftnumber', {}, 'Sleftnumbers', {}, 'Sleftmean', {}, 'Sleftvariance', {}, 'Key2', {}, 'Timeq2', {});
for i = 1:nTrials
resultsMatrix(i).TrialOrder = 1;
resultsMatrix(i).TrialType = randtemp(1);
resultsMatrix(i).S1Numbers = num2str(seq1);
resultsMatrix(i).S1Mean = 10;
resultsMatrix(i).S1Variance = 20;
resultsMatrix(i).S1Position = randpos{1};
resultsMatrix(i).S2Numbers = num2str(seq2);
end
However, I keep getting the following error:
Error using struct
Field and value input arguments must come in pairs.
Error in Fourth_draft (line 147)
resultsMatrix = struct('TrialOrder', {}, 'TrialType', {}, 'S1Numbers', {}, 'S1Mean', {}, 'S1Variance', {},
'S1Position', {}, 'S2Number', {}, 'S2Numbers', {}, 'S2Mean', {}, 'S2Variance', {}, 'S2Position', 'S3Number',
{}, 'S3Numbers', {}, 'S3Mean', {}, 'S3Variance', {}, 'S3Position', {}, 'Key1', {}, 'Timeq1', {},
'Srightnumber', {}, 'Srightnumbers', {}, 'Srightmean', {}, 'Srightvariance', {}, 'Sleftnumber', {},
'Sleftnumbers', {}, 'Sleftmean', {}, 'Sleftvariance', {}, 'Key2', {}, 'Timeq2', {});
I would very much appreciate if somebody could help me with this, thank you!
0 Comments
Accepted Answer
Adam Danz
on 19 Oct 2020
This is why long lines of code should be broken up into separate lines.
resultsMatrix = struct('TrialOrder', {}, 'TrialType', {}, 'S1Numbers', {}, 'S1Mean', {}, ...
'S1Variance', {}, 'S1Position', {}, 'S2Number', {}, 'S2Numbers', {}, 'S2Mean', {}, ...
'S2Variance', {}, 'S2Position', 'S3Number', {}, 'S3Numbers', {}, 'S3Mean', {}, 'S3Variance',{}, ...
'S3Position', {}, 'Key1', {}, 'Timeq1', {}, 'Srightnumber', {}, 'Srightnumbers', {}, ...
'Srightmean', {}, 'Srightvariance', {}, 'Sleftnumber', {}, 'Sleftnumbers', {}, 'Sleftmean', {}, ...
'Sleftvariance', {}, 'Key2', {}, 'Timeq2', {});
Hint: One of your fields is missing a {} (3rd row).
3 Comments
Stephen23
on 19 Oct 2020
Rather than writing out {} a thousand times, just use some indexing and a comma-separated list:
C = {'TrialOrder', 'TrialType', 'S1Numbers', 'S1Mean', ...};
C(2,:) = {{}};
S = struct(C{:});
More Answers (1)
meghannmarie
on 19 Oct 2020
You are missing {} after 'S2Position'
resultsMatrix = struct('TrialOrder', {},... , 'S2Variance', {}, 'S2Position', 'S3Number', {}, 'S3Numbers', {},...
See Also
Categories
Find more on Logical 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!