필터 지우기
필터 지우기

2 codes ovewriting each other?

조회 수: 1 (최근 30일)
Alexa Z
Alexa Z 2020년 12월 18일
답변: Cris LaPierre 2020년 12월 18일
clear all
close all
startpath ='1EXAMENOPDRACHT\Assignment_V2\Data';
Datapath = [startpath '\Data'];
% rechtersubjecten
FirstSwingExcursionAngle_HR = zeros(5,5)
FirstSwingExcursionAngle_MR = zeros(5,5)
sumHR = 0;
sumMR = 0;
avgHR = zeros(1,5);
avgMR = zeros(1,5);
for i = 1:5
subject = [3 5 7 9 14];
dataFolder = [startpath '\TD' num2str(subject(i))];
dataFile_HR = fullfile(dataFolder, '\IK\HR*.mot'); %naam van de map een bestandsnaam geven
pathwayIK_HR = dir(dataFile_HR);
dataFile_MR = fullfile(dataFolder, '\IK\MR*.mot');
pathwayIK_MR = dir(dataFile_MR)
for k = 1:length(pathwayIK_HR)
for za = 1:length(pathwayIK_MR)
IKHRdata = importdata(fullfile(pathwayIK_HR(k).folder,pathwayIK_HR(k).name));
IKMRdata = importdata(fullfile(pathwayIK_MR(za).folder,pathwayIK_MR(za).name));
DataHRlinks = IKHRdata.data(:,strcmp('knee_angle_l',IKHRdata.colheaders));
TimeHRlinks = IKHRdata.data(:,strcmp('time',IKHRdata.colheaders));
DataMRlinks = IKHRdata.data(:,strcmp('knee_angle_l',IKMRdata.colheaders));
TimeMRlinks = IKHRdata.data(:,strcmp('time',IKMRdata.colheaders));
dataFile_HR = fullfile(dataFolder, '\Event\HR*.mat'); %naam van de map een bestandsnaam geven
pathwayEvent_HR = dir(dataFile_HR);
dataFile_MR = fullfile(dataFolder, '\Event\MR*.mat'); %naam van de map een bestandsnaam geven
pathwayEvent_MR = dir(dataFile_MR);
EventHRdata = importdata(fullfile(pathwayEvent_HR(k).folder,pathwayEvent_HR(k).name));
t0knielinks_HR = EventHRdata.t0;
EventMRdata = importdata(fullfile(pathwayEvent_MR(za).folder,pathwayEvent_MR(za).name));
t0knielinks_MR = EventMRdata.t0;
for j = 1:length(TimeHRlinks) % vervangen door functie
if t0knielinks_HR == TimeHRlinks(j)
t0HR = j
break
end
end
for t = 1:length(TimeMRlinks)
if t0knielinks_MR == TimeMRlinks(t)
t0MR = t
break
end
end
FirstSwingExcursionAngle_HR(k,i)=DataHRlinks(t0HR) % plot
sumHR = sumHR + DataHRlinks(t0HR);
FirstSwingExcursionAngle_MR(za,i)=DataMRlinks(t0MR) % plot
sumMR = sumMR + DataMRlinks(t0HR);
end
end
avgHR(i)=sumHR/length(pathwayIK_HR) % staaf diagram van het gemiddelde
sumHR = 0; % i gebruiken voor de juiste subplots
avgMR(i)=sumMR/length(pathwayIK_MR) % staaf diagram van het gemiddelde
sumMR = 0;
end
filename='AverageFirstSwingExcursionAngle_HR.xlsx'
writematrix(avgHR,filename)
filename='AverageFirstSwingExcursionAngle_MR.xlsx'
writematrix(avgMR,filename)
Why are the HR and MR FirstSwingExcursionAngle overwriting each other in the command window?
If we run them seperate it works fine, but if we make a combination of HR/MR in the code it does not.
This works fine:
clear all
close all
startpath ='1EXAMENOPDRACHT\Assignment_V2\Data';
Datapath = [startpath '\Data'];
% rechtersubjecten
FirstSwingExcursionAngle_MR = zeros(5,5)
sum = 0;
avgMR = zeros(1,5);
for i = 1:5
subject = [3 5 7 9 14];
dataFolder = [startpath '\TD' num2str(subject(i))];
dataFile = fullfile(dataFolder, '\IK\MR*.mot'); %naam van de map een bestandsnaam geven
pathwayIK_MR = dir(dataFile);
for k = 1:length(pathwayIK_MR)
IKMRdata = importdata(fullfile(pathwayIK_MR(k).folder,pathwayIK_MR(k).name));
DataMRlinks = IKMRdata.data(:,strcmp('knee_angle_l',IKMRdata.colheaders));
TimeMRlinks = IKMRdata.data(:,strcmp('time',IKMRdata.colheaders));
dataFile = fullfile(dataFolder, '\Event\MR*.mat'); %naam van de map een bestandsnaam geven
pathwayEvent_MR = dir(dataFile);
EventMRdata = importdata(fullfile(pathwayEvent_MR(k).folder,pathwayEvent_MR(k).name));
t0knielinks_MR = EventMRdata.t0;
for j = 1:length(TimeMRlinks) % vervangen door functie
if t0knielinks_MR == TimeMRlinks(j)
t0MR = j
break
end
end
FirstSwingExcursionAngle_MR(k,i)=DataMRlinks(t0MR) % plot
sum = sum + DataMRlinks(t0MR);
end
avgHR(i)=sum/length(pathwayIK_MR) % staaf diagram van het gemiddelde
sum = 0; % i gebruiken voor de juiste subplots
end
filename='AverageFirstSwingExcursionAngle_MR.xlsx'
writematrix(avgMR,filename)

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 12월 18일
We don't know anything about your files, but I don't think you want to combine your j and za for loops the way you did. I think you want to keep these as two separte loops.
Also note that in your calculation of sumMR you are using t0HR instead of t0MR.
clear all
close all
startpath ='1EXAMENOPDRACHT\Assignment_V2\Data';
Datapath = [startpath '\Data'];
% rechtersubjecten
FirstSwingExcursionAngle_HR = zeros(5,5)
FirstSwingExcursionAngle_MR = zeros(5,5)
sumHR = 0;
sumMR = 0;
avgHR = zeros(1,5);
avgMR = zeros(1,5);
for i = 1:5
subject = [3 5 7 9 14];
dataFolder = [startpath '\TD' num2str(subject(i))];
dataFile_HR = fullfile(dataFolder, '\IK\HR*.mot'); %naam van de map een bestandsnaam geven
pathwayIK_HR = dir(dataFile_HR);
dataFile_MR = fullfile(dataFolder, '\IK\MR*.mot');
pathwayIK_MR = dir(dataFile_MR)
for k = 1:length(pathwayIK_HR)
IKHRdata = importdata(fullfile(pathwayIK_HR(k).folder,pathwayIK_HR(k).name));
DataHRlinks = IKHRdata.data(:,strcmp('knee_angle_l',IKHRdata.colheaders));
TimeHRlinks = IKHRdata.data(:,strcmp('time',IKHRdata.colheaders));
dataFile_HR = fullfile(dataFolder, '\Event\HR*.mat'); %naam van de map een bestandsnaam geven
pathwayEvent_HR = dir(dataFile_HR);
EventHRdata = importdata(fullfile(pathwayEvent_HR(k).folder,pathwayEvent_HR(k).name));
t0knielinks_HR = EventHRdata.t0;
for j = 1:length(TimeHRlinks) % vervangen door functie
if t0knielinks_HR == TimeHRlinks(j)
t0HR = j
break
end
end
FirstSwingExcursionAngle_HR(k,i)=DataHRlinks(t0HR) % plot
sumHR = sumHR + DataHRlinks(t0HR);
end
avgHR(i)=sumHR/length(pathwayIK_HR) % staaf diagram van het gemiddelde
sumHR = 0; % i gebruiken voor de juiste subplots
for k = 1:length(pathwayIK_MR)
IKMRdata = importdata(fullfile(pathwayIK_MR(k).folder,pathwayIK_MR(k).name));
DataMRlinks = IKMRdata.data(:,strcmp('knee_angle_l',IKMRdata.colheaders));
TimeMRlinks = IKMRdata.data(:,strcmp('time',IKMRdata.colheaders));
dataFile = fullfile(dataFolder, '\Event\MR*.mat'); %naam van de map een bestandsnaam geven
pathwayEvent_MR = dir(dataFile);
EventMRdata = importdata(fullfile(pathwayEvent_MR(k).folder,pathwayEvent_MR(k).name));
t0knielinks_MR = EventMRdata.t0;
for j = 1:length(TimeMRlinks) % vervangen door functie
if t0knielinks_MR == TimeMRlinks(j)
t0MR = j
break
end
end
FirstSwingExcursionAngle_MR(k,i)=DataMRlinks(t0MR) % plot
sum = sum + DataMRlinks(t0MR);
end
avgHR(i)=sum/length(pathwayIK_MR) % staaf diagram van het gemiddelde
sum = 0; % i gebruiken voor de juiste subplots
end
filename='AverageFirstSwingExcursionAngle_MR.xlsx'
writematrix(avgMR,filename)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by