access multiple csv files from different folder

5 次查看(过去 30 天)
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?

采纳的回答

yanqi liu
yanqi liu 2022-1-25
% 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 个评论
Venkatkumar M
Venkatkumar M 2022-1-26
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 2022-1-27
% 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);

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2022-1-25
编辑:Stephen23 2022-1-25
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 个评论
Venkatkumar M
Venkatkumar M 2022-1-25
编辑:Venkatkumar M 2022-1-25
error
>> C{k} = T;
Undefined function or variable 'k'.
is it supposed to beC{:} = T;?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by