Borrar filtros
Borrar filtros

access multiple csv files from different folder

1 visualización (últimos 30 días)
Venkatkumar M
Venkatkumar M el 24 de En. de 2022
Comentada: yanqi liu el 27 de En. de 2022
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?

Respuesta aceptada

yanqi liu
yanqi liu el 25 de En. de 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 comentarios
Venkatkumar M
Venkatkumar M el 26 de En. de 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 el 27 de En. de 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);

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 25 de En. de 2022
Editada: Stephen23 el 25 de En. de 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 comentarios
Venkatkumar M
Venkatkumar M el 25 de En. de 2022
Editada: Venkatkumar M el 25 de En. de 2022
error
>> C{k} = T;
Undefined function or variable 'k'.
is it supposed to beC{:} = T;?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by