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

1 Ansicht (letzte 30 Tage)
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!

Akzeptierte Antwort

Adam Danz
Adam Danz am 19 Okt. 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 Kommentare
Stephen23
Stephen23 am 19 Okt. 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{:});

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by