fileTable = struct2table(files);
numOfFiles = height(fileTable);
if strfind(fileTable.name{k}, 'sec')
secfiles{k} = fileTable.name{k};
allsecfiles = secfiles(:,~cellfun(@isempty,secfiles(1,:)));
for j = 1:length(allsecfiles)
file_info(:,j) = split(allsecfiles(j), "_");
masterData(j).group = file_info(1,j);
masterData(j).time = file_info(2,j);
masterData(j).trial = extractBefore(file_info(4,j),'.');
timetrials = {'2sec';'5sec';'10sec'};
for t = 1:length(timetrials)
timetrialfiles = find(strcmp(file_info(2,:),timetrials{t}));
gettimefiles = allsecfiles(timetrialfiles);
for s = 1:length(gettimefiles)
imptimetrials = readtable(char(gettimefiles(s)));
time = table2array(imptimetrials(:,timecol));
emg = table2array(imptimetrials(:,emgcol));
idc = strfind(timetrials,'2sec');
idx = ~cellfun('isempty',idc);
idc = strfind(timetrials,'5sec');
idx = ~cellfun('isempty',idc);
con = find(time < cut & time > 0);
ecc = find(time > cut & time < endtime);
plot(contime,conemg, 'Color','#D95319')
plot(ecctime,eccemg, 'Color','#EDB120')
ylabel('EMG Response (mV)')
legend(sprintf('%s EMG',musc_c), 'Concentric', 'Eccentric')
title(sprintf('%s: %s EMG vs Time [Trial %d]',timetrials{t},musc_c,s))
smooth_con = smooth(abs(conemg - mean(conemg)));
plot(contime,smooth_con,'k')
RMScon = sqrt(movmean(smooth_con.^2,500));
legend('RawEMG','Rectified EMG','Moving Average')
title(sprintf('%s %s EMG: Concentric Contraction vs Time',timetrials{t}, musc_c))
ylabel('EMG Response (mV)')
plot(ecctime, eccemg, 'b')
smooth_ecc = smooth(abs(eccemg-mean(eccemg)));
plot(ecctime, smooth_ecc,'k')
RMSecc = sqrt(movmean(smooth_ecc.^2,500));
legend('RawEMG','Rectified EMG','Moving Average')
title(sprintf('%s %s EMG: Eccentric Contraction vs Time',timetrials{t}, musc_c))
ylabel('EMG Response (mV)')
plot(contime,smooth_con,'k')
intCon = cumtrapz(RMScon);
legend('Rectified EMG','Integrated EMG')
title(sprintf('%s %s EMG: Concentric Contraction vs Time',timetrials{t},musc_c))
ylabel('EMG Response (mV-s)')
ylabel('EMG Response (mV)')
plot(ecctime,smooth_ecc,'k')
intEcc = cumtrapz(RMSecc);
plot(ecctime, intEcc,'g')
legend('Rectified EMG','Integrated EMG')
title(sprintf('%s %s EMG: Eccentric Contraction vs Time',timetrials{t},musc_c))
ylabel('EMG Response (mV-s)')
ylabel('EMG Response (mV)')
maxRMScon(r,c) = max(RMScon);
maxRMSecc(r,c) = max(RMSecc);
maxINTcon(r,c) = max(intCon);
maxINTecc(r,c) = max(intEcc);
fourier_con = smooth_con.*1000;
fourier_ecc = smooth_ecc.*1000;
lenCon = length(fourier_con);
lenEcc = length(fourier_ecc);
fftCon = abs(fft(fourier_con));
fftEcc = abs(fft(fourier_ecc));
truncCon = fftCon(1:lenCon/2+1);
truncEcc = fftEcc(1:lenEcc/2+1);
truncCon(2:end-1) = truncCon(2:end-1).*2;
truncEcc(2:end-1) = truncEcc(2:end-1).*2;
fc = ((0:(lenCon/2))./lenCon).*fs;
fe = ((0:(lenEcc/2))./lenEcc).*fs;
for fr = 1:length(freqrange)-1
freqcon(fr) = medfreq(truncCon(freqrange(fr):freqrange(fr+1)),fs);
freqecc(fr) = medfreq(truncEcc(freqrange(fr):freqrange(fr+1)),fs);
[pMaxCon,pMaxConLocs] = max(freqcon);
[pMaxEcc,pMaxEccLocs] = max(freqecc);
xline(pMaxCon, '--r','LineWidth',2);
title(sprintf('%s %s EMG: Concentric Power Spectrum',timetrials{t},musc_c))
legend(sprintf('Median f = %d Hz', pMaxCon),sprintf('Power: %s Concentric %s', musc_c, timetrials{t}))
xline(pMaxEcc, '--r','LineWidth',2);
title(sprintf('%s %s EMG: Eccentric Power Spectrum',timetrials{t},musc_c))
legend(sprintf('Median f = %d Hz', pMaxEcc),sprintf('Power: %s Eccentric %s', musc_c, timetrials{t}))
saveas(myfig,sprintf('%s_Fig%d.png', musc_c, myfig))