For loop overwriting data
3 views (last 30 days)
Show older comments
Mikel Jimenez
on 8 Aug 2021
Commented: Mikel Jimenez
on 8 Aug 2021
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
0 Comments
Accepted Answer
Walter Roberson
on 8 Aug 2021
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.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!