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 件のコメント
Stephen23
Stephen23 2020 年 10 月 19 日
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{:});
Cai Chin
Cai Chin 2020 年 10 月 19 日
Thank you for this suggestion

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by