0

私は MATLAB を初めて使用し、別の m ファイルで反復できる構造を構築する際に問題があります。各試行でセグメントを使用して試行をi行った被験者がいます。各セグメントから特定のタイムポイントを保存したいと思います。ここではand と呼びます。私は次のことを試しました(1つの被験者、1つの試行に対して):twstartTimestopTime

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 に答える