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

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Adam Danz
Adam Danz il 19 Ott 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 Commenti

Accedi per commentare.

Più risposte (1)

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

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by