0

我对 MATLAB 相当陌生,并且在构建可以使用另一个 m 文件迭代的结构时遇到问题。我有i受试者在每次t试验中进行w分段试验。我现在想存储每个段的特定时间点。这里称为startTimeand stopTime。我尝试了以下方法(针对一个主题,一项试验):

i=1; %Testperson #


% Trial 1
t=1; %Trial #

w=1; %Segment 1

selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'startTime',0.001*5000)
selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'stopTime',24.5*5000)

w=2; %Segment 2

selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'startTime',0.001*5000);
selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'stopTime',24.5*5000);

w=3; %Segment 3

selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'startTime',0.001*5000);
selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'stopTime',24.5*5000);

w=4; %Segment 4

selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'startTime',0.001*5000);
selectedData = setfield('selectedData',['Subj' num2str(i)],['Trial' num2str(t)],{w},'stopTime',24.5*5000);

似乎setfield覆盖了我要存储的以前的值?有什么建议么?

4

1 回答 1

1

如果上面的代码真的是你正在执行的,我会有点惊讶它确实有效。

的第一个参数setfield应该是您要更改的结构,而不是它的字符串名称。所以试试:

selectedData = setfield(selectedData,['Subj' num2str(i)],['Trial' num2str(t)],{w},'startTime',0.001*5000);
selectedData = setfield(selectedData,['Subj' num2str(i)],['Trial' num2str(t)],{w},'stopTime',24.5*5000);

但是: 这看起来像是一个设计得不太好的结构。当您也可以使用适当的索引时,您不应该使用“枚举”字段名作为索引。

在您的情况下,您应该使用结构数组,例如:

subjects(i).trials(t).startTime(w) = xx;
subjects(i).trials(t).stopTime(w) = yy;

这使得事情 a) 更容易编码和 b) 允许以后更容易地访问数据。

于 2013-11-13T08:50:40.980 回答