access multiple csv files from different folder

조회 수: 5 (최근 30일)
Venkatkumar M
Venkatkumar M 2022년 1월 24일
댓글: yanqi liu 2022년 1월 27일
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