access multiple csv files from different folder

4 views (last 30 days)
Hi,
I have three folder in the drive namely 'D/Buck/V=12v' , 'D/Buck/V=24v' and 'D/Buck/V=48v'
each folder 4 differnt csv files namely 2k.csv,4k.csv,6k.csv and 8k.csv.
Each csv has 5 cloumns. delete first column and rename other coulmn as OV_12v_2k, OI_12v_2k,IV_12v_2k and II_12v_2k ........ II_48V_8k
how to read all files from the three folers in editor.
and group data from each csv file like all 1column data in one file and and name the rows like like ' 12v_2 12v_4 12v_6 12v_8 24v_2 ............. 48v_8'
Could any one please help me to resolve the issue?

Accepted Answer

yanqi liu
yanqi liu on 25 Jan 2022
% three folder in the drive namely 'D/Buck/V=12v' , 'D/Buck/V=24v' and 'D/Buck/V=48v'
% 4 differnt csv files namely 2.csv,4.csv,6.csv and 8.csv
% group data from each csv file like all 1column data in one file and and name the rows
% like ' 12v_2 12v_4 12v_6 12v_8 24v_2 ............. 48v_8'
data = [];
for i = 1 : 1 : 3
fd = fullfile(sprintf('D:/Buck/V=%dv', i*12))
for j = 1 : 1 : 4
fi = fullfile(fd, sprintf('%d.csv', j*2))
% read data
di = xlsread(fi);
data{end+1,1} = sprintf('%dv_%d', i*12, j*2);
data{end, 2} = di(:)';
end
end
data
xlswrite('result.xlsx', data);
  4 Comments
Venkatkumar M
Venkatkumar M on 26 Jan 2022
Unable to perform assignment because the size of the left side is 1-by-1048576 and the size of the right side is
1-by-5242880.
Error in Untitled7 (line 11)
data(end, 2:length(di)+1) = num2cell(di(:)');;
error in code
could you please help with it?
yanqi liu
yanqi liu on 27 Jan 2022
% three folder in the drive namely 'D/Buck/V=12v' , 'D/Buck/V=24v' and 'D/Buck/V=48v'
% 4 differnt csv files namely 2.csv,4.csv,6.csv and 8.csv
% group data from each csv file like all 1column data in one file and and name the rows
% like ' 12v_2 12v_4 12v_6 12v_8 24v_2 ............. 48v_8'
data = [];
for i = 1 : 1 : 3
fd = fullfile(sprintf('D:/Buck/V=%dv', i*12))
for j = 1 : 1 : 4
fi = fullfile(fd, sprintf('%d.csv', j*2))
% read data
di = xlsread(fi);
data{end+1,1} = sprintf('%dv_%d', i*12, j*2);
tmp = num2cell(di(:)');
data(end, 2:length(tmp)+1) = tmp;
end
end
data
xlswrite('result.xlsx', data);

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 25 Jan 2022
Edited: Stephen23 on 25 Jan 2022
This should get you started. Because you did not upload any sample files I could not test it, therefore it will probably not work: you will need to modify it to suit your particular file formats and your required output file format. To do that you will need to read the documentation for the functions etc and make modifications to suit your data and needs. If you upload some sample files, then I can help you with that.
P = 'D/Buck';
C = {};
for ii = [12,24,48]
D = sprintf('V=%dv',ii);
for jj = [2,4,6,8]
F = sprintf('%d.csv',jj);
T = readtable(fullfile(P,D,F));
T{:,'name'} = sprintf("%dV_%d",ii,jj); % these would be better as two numeric variables/columns.
C{end+1} = T;
end
end
Z = vertcat(C{:}); % asusmes all files have exactly the same format.
writetable(Z, 'myfile.csv')
  4 Comments
Venkatkumar M
Venkatkumar M on 25 Jan 2022
Edited: Venkatkumar M on 25 Jan 2022
error
>> C{k} = T;
Undefined function or variable 'k'.
is it supposed to beC{:} = T;?

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!