How to specify input arguments of a structure in a 'for' loop separate to field argument

1 次查看(过去 30 天)
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!

采纳的回答

Adam Danz
Adam Danz 2020-10-19
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 个评论

请先登录,再进行评论。

更多回答(1 个)

meghannmarie
meghannmarie 2020-10-19
You are missing {} after 'S2Position'
resultsMatrix = struct('TrialOrder', {},... , 'S2Variance', {}, 'S2Position', 'S3Number', {}, 'S3Numbers', {},...

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by