Write calculated values to an excel file in column 2 and the file name in column 1

5 views (last 30 days)
Hi,
I have the below code. I have ~4000 csv files in a folder.
What I require: I am doing filtering on all these files (one at a time) and calculating a parameter (for each file). Hence each file will have a value. I want to store the file name for which it was calculated in one column and the calculated value in other.
Also, to do this faster, I want to store the values in an array and then write all the values together in the csv file to save time or using xlswrite operation for each file will take a lot of time.
I already have some part of code but how can I do the rest? Thanks.
(I have attached a screenshot of my files)
clearvars
fs=40000;
[z,p,k] = ellip(4,1,40,2400/(fs/2),'high'); %z,p,k method
[sos,g] = zp2sos(z,p,k);
a=0;
indir = '.'; %current directory
%outdir = 'C:\Users\AmplitudeValues.csv';
files = dir( fullfile(indir, '*.csv'));
for file = files'
inname = fullfile(file.folder, file.name);
n = xlsread(inname);
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.43;
y_h2 = filtfilt(sos, g, m);
AmplitudeWaterflow=sum(abs(y_h2));
a=a+1
%%write the value of Amplitude to a csv file with file name in one
%%column and value in other
end

Accepted Answer

MJFcoNaN
MJFcoNaN on 18 Apr 2022
table is a good choice.
% your code
% ...
files = dir( fullfile(indir, '*.csv'));
tbl = struct2table(files);
tbl.AmplitudeWaterflow=NaN(length(tbl.name),1);
for file = files'
% your code
% ...
% you count a from 0, therefore:
a=a+1;
tbl.AmplitudeWaterflow(a)=sum(abs(y_h2));
end
% only keep two variables
tbl = tbl(:, {'name', 'AmplitudeWaterflow'})
writetable(tbl, 'output.xlsx')

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!