Loop calculation and output

i have 170 files of eeg recording. i want output of each file to be stored in a seperate excel file. i am not sure how to do this. pls help
My code to initiate is as below:
txtFiles = dir('*.txt') ;
l=length(txtFiles);
N = l*10 ;
Bandsheet = zeros(N,8);
PSDSheet1 =zeros(N,16);
PSDSheet2 =zeros(N,16);
PSDSheet3 =zeros(N,16);
for n=1:170
filename = ['00000460_', int2str(n), '.txt'];
x=load(filename);
figure;

 Accepted Answer

You meant this:
txtFiles = dir('*.txt') ;
l=length(txtFiles);
N = l*10 ;
Bandsheet = zeros(N,8);
PSDSheet1 =zeros(N,16);
PSDSheet2 =zeros(N,16);
PSDSheet3 =zeros(N,16);
for n=1:170
filename = ['00000460_', int2str(n), '.txt'];
x=load(filename);
figure;
% then you are going to update Bandsheet, PSDSheet1, etc here?
% and you want to write them into different excel files?
fname = ['00000460_', int2str(n), '.xlsx'];
writetable(Bandsheet, fname, 'Sheet', 'Bandsheet');
writetable(PSDSheet1, fname, 'Sheet', 'PSDSheet1');
writetable(PSDSheet2, fname, 'Sheet', 'PSDSheet2');
writetable(PSDSheet3, fname, 'Sheet', 'PSDSheet3');
end

6 Comments

% this will give you all test files names
txtFiles = dir('*.txt') ;
l=length(txtFiles);
N = l*10 ;
Bandsheet = zeros(N,8);
PSDSheet1 =zeros(N,16);
PSDSheet2 =zeros(N,16);
PSDSheet3 =zeros(N,16);
for n=1:2
filename = ['00000460_', int2str(n), '.txt'];
x=load(filename);
figure;
x=load(filename);
ab= x2(0001:24000);
%%band power%%
delta = bandpower(ab,24000, [1 4]);
theta = bandpower(ab,24000,[4 8]);
alpha = bandpower(ab,24000,[8 12]);
low_beta = bandpower(ab,24000,[12 20]);
high_beta = bandpower(ab,1000,[20 35]);
beta = bandpower(ab,24000,[12 35]);
gamma = bandpower(ab,24000,[35 80]);
HFO= bandpower (ab,24000,[200 300]);
bandcomplete1 = [delta theta alpha low_beta high_beta beta gamma HFO]
Fs=24000;
N=ceil(log2(length(ab)));
L=length(ab);
%%Delta Filtering and Spectrum%%
delta1 = bandpass(ab,[1 4],1000);
figure
delta2= fft(delta1,2^N)/(L/2);
if isreal(ab)==0
fy=fy/2;
end
Power=delta2.*conj(delta2);
phy=angle(delta2);
% Frequency axis
f=(Fs/2^N)*(0:2^(N-1)-1);
%if nargin==4
deltapower= Power(1:2^(N-1));
% Find local maxima
maxIndices = islocalmax(deltapower,'MaxNumExtrema',3,'SamplePoints',f);
% Display results
clf
plot(f,deltapower,'Color',[109 185 226]/255,'DisplayName','Input data')
hold on
% Plot local maxima
plot(f(maxIndices),deltapower(maxIndices),'^','Color',[217 83 25]/255,...
'MarkerFaceColor', [217 83 25]/255, 'DisplayName', 'Local maxima')
hold off
deltavalue = plot(f(maxIndices),deltapower(maxIndices), '*','Color',[20 83 20]/255,...
'MarkerFaceColor', [55 83 25]/255, 'DisplayName', '.')
legend
x=get(deltavalue,'XData') ;
y=get(deltavalue,'YData') ;
disp(x)
disp(y)
t= [x;y]
sorted_delta = sortrows(t',2,'descend')
AllPSD2= [sorted_delta sorted_theta sorted_alpha sorted_lowbeta sorted_highbeta sorted_beta sorted_gamma sorted_hfo ]
psd11 = AllPSD2 (1,:)
psd12 = AllPSD2 (2,:)
psd13 = AllPSD2(3,:)
Bandsheet (n,:) = bandcomplete2;
PSDSheet1 (n,:) =psd21;
PSDSheet2 (n,:) =psd22;
PSDSheet3 (n,:) =psd23;
ab= x2(24001:48000);
%%band power%%
delta = bandpower(ab,24000, [1 4]);
theta = bandpower(ab,24000,[4 8]);
alpha = bandpower(ab,24000,[8 12]);
low_beta = bandpower(ab,24000,[12 20]);
high_beta = bandpower(ab,1000,[20 35]);
beta = bandpower(ab,24000,[12 35]);
gamma = bandpower(ab,24000,[35 80]);
HFO= bandpower (ab,24000,[200 300]);
disp (delta);
disp (theta);
disp (alpha);
disp (low_beta);
disp (high_beta);
disp (beta);
disp (gamma);
disp (HFO);
bandcomplete2 = [delta theta alpha low_beta high_beta beta gamma HFO]
%%Delta Filtering and Spectrum%%
delta1 = bandpass(ab,[1 4],1000);
figure
delta2= fft(delta1,2^N)/(L/2);
if isreal(ab)==0
fy=fy/2;
end
Power=delta2.*conj(delta2);
phy=angle(delta2);
% Frequency axis
f=(Fs/2^N)*(0:2^(N-1)-1);
%if nargin==4
deltapower= Power(1:2^(N-1));
% Find local maxima
maxIndices = islocalmax(deltapower,'MaxNumExtrema',3,'SamplePoints',f);
% Display results
clf
plot(f,deltapower,'Color',[109 185 226]/255,'DisplayName','Input data')
hold on
% Plot local maxima
plot(f(maxIndices),deltapower(maxIndices),'^','Color',[217 83 25]/255,...
'MarkerFaceColor', [217 83 25]/255, 'DisplayName', 'Local maxima')
hold off
deltavalue = plot(f(maxIndices),deltapower(maxIndices), '*','Color',[20 83 20]/255,...
'MarkerFaceColor', [55 83 25]/255, 'DisplayName', '.')
legend
x=get(deltavalue,'XData') ;
y=get(deltavalue,'YData') ;
disp(x)
disp(y)
t= [x;y]
sorted_delta = sortrows(t',2,'descend')
AllPSD2= [sorted_delta sorted_theta sorted_alpha sorted_lowbeta sorted_highbeta sorted_beta sorted_gamma sorted_hfo ]
psd21 = AllPSD2 (1,:)
psd22 = AllPSD2 (2,:)
psd23 = AllPSD2(3,:)
Bandsheet (n+1,:) = bandcomplete2;
PSDSheet1 (n+1,:) =psd21;
PSDSheet2 (n+1,:) =psd22;
PSDSheet3 (n+1,:) =psd23;
this is my actual code
ab= x2(0001:24000);
this x2(....) has to run till 24,00,000 in few files
and each 0000460_ needs to be saved as a different excel file with 4
sheets
im into life sciences and dont have a clue about matlab.. tried this and its req for my thesis
I see. Try the script posted above. It shall save those 4 sheets into an excel file.
thanks a ton worked
Great.
Accept the answer please then.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!