フィルターのクリア

access multiple csv files from different folder

3 ビュー (過去 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;?
Stephen23
Stephen23 2022 年 1 月 25 日
C{end+1} = T;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by