For loop overwriting data

조회 수: 3 (최근 30일)
Mikel  Jimenez
Mikel Jimenez 2021년 8월 8일
댓글: Mikel Jimenez 2021년 8월 8일
Hello,
I have this code below:
clear all;
names= {'AB_Clean','AK_Clean'};
data=[];
for iSubj=1:numel(names)
filetoload= names{iSubj};
t = readtable([filetoload '.xls']);
data.id = num2str (iSubj);
response = mod(t.MouseClickAngleToCenter,180);
condition = mod(t.Condition,180);
target = mod(t.CenterPackmanOriantation,180);
for i=1:length(response)
A=str2num(t.SurroundingPackmansRotations_tl_bl_tr_br_{i});
if ~isempty(A)
distractors_local(i,:) = mod(A,180);
else
distractors_local(i,:) = [nan nan nan nan];
end
end
distractors_global = mod(t.PackmansRectangleRotation+90,180); %Original analysis
%distractors_global = mod(t.PackmansRectangleRotation,180);%New collinearity analysis
errors = circdist(response,target);
errors(errors>90) = errors(errors>90) -180;
errors(errors<-90) = errors(errors<-90) +180;
distractors1 = circdist(distractors_global,target);
distractors4 = circdist(distractors_local,target);
distractors1(distractors1>90) = distractors1(distractors1>90) -180;
distractors1(distractors1<-90) = distractors1(distractors1<-90) +180;
distractors4(distractors4>90) = distractors4(distractors4>90) -180;
distractors4(distractors4<-90) = distractors4(distractors4<-90) +180;
distractors4 = distractors4(:,1:2);
for c=unique(condition)'
data.errors = errors(condition==c)';
data.distractors = [distractors1(condition==c,:)'; distractors4(condition==c,[1 2])'];
end
end
I want the loop to go through the data of each participant (i.e AB_Clean and AK_Clean) and save the data.errors, data.distractors and data.id variables for each participant in the "data" struct. There is something missing in my code, since the variables are being overwritten every time the loop starts. Any help with this would be very much appreciated.
Thanks!
-Mikel

채택된 답변

Walter Roberson
Walter Roberson 2021년 8월 8일
clear all;
names= {'AB_Clean','AK_Clean'};
data = struct();
for iSubj=1:numel(names)
filetoload= names{iSubj};
t = readtable([filetoload '.xls']);
data(iSubj).id = num2str(iSubj);
response = mod(t.MouseClickAngleToCenter,180);
condition = mod(t.Condition,180);
target = mod(t.CenterPackmanOriantation,180);
for i=1:length(response)
A=str2num(t.SurroundingPackmansRotations_tl_bl_tr_br_{i});
if ~isempty(A)
distractors_local(i,:) = mod(A,180);
else
distractors_local(i,:) = [nan nan nan nan];
end
end
distractors_global = mod(t.PackmansRectangleRotation+90,180); %Original analysis
%distractors_global = mod(t.PackmansRectangleRotation,180);%New collinearity analysis
errors = circdist(response,target);
errors(errors>90) = errors(errors>90) -180;
errors(errors<-90) = errors(errors<-90) +180;
distractors1 = circdist(distractors_global,target);
distractors4 = circdist(distractors_local,target);
distractors1(distractors1>90) = distractors1(distractors1>90) -180;
distractors1(distractors1<-90) = distractors1(distractors1<-90) +180;
distractors4(distractors4>90) = distractors4(distractors4>90) -180;
distractors4(distractors4<-90) = distractors4(distractors4<-90) +180;
distractors4 = distractors4(:,1:2);
ucond = unique(condition).';
data(iSubj).conditions = ucond;
ncond = length(ucond);
data(iSubj).errors = cell(ncond,1);
data(iSubj).distractors = cell(ncond,1);
for cidx = 1 : ncond
c = ucond(cidx);
mask = condition==c;
data(iSubj).errors{cidx} = errors(mask)';
data(iSubj).distractors{cidx} = [distractors1(mask,:)'; distractors4(mask,[1 2])'];
end
end
Now data will be a struct array with the same length as the number of names.
There is a new field conditions to record the unique conditions.
The errors and distractors fields have been turned into cell arrays, with one entry for each unique condition for this subject.
I do this instead of just concatenating everything together because there are a variable number of matches for each condition, and if you were to concatenate them all together you would not be able to tell which entry matched to which condition.
  댓글 수: 1
Mikel  Jimenez
Mikel Jimenez 2021년 8월 8일
Dear Walter,
Thank you very very much for your help. Now I'll try to understand what's going on in the new part of the code:)
Thanks,
Mikel

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by