I wanted to combine 100 csv files for plotting in Matlab. All the files have same number of columns and rows. Can anyone help?
2 views (last 30 days)
Show older comments
I Used the following code.But it is not working
csvFiles = dir('*.csv') ;
N = length(csvFiles) ;
for i = 1:N
T = readtable(csvFiles(i).name) ;
end
0 Comments
Accepted Answer
Voss
on 12 Mar 2024
your_folder = 'folder where the csv files are'; % absolute or relative path
csvFiles = dir(fullfile(your_folder,'*.csv')) ;
names = fullfile({csvFiles.folder},{csvFiles.name});
N = length(csvFiles);
for i = 1:N
csvFiles(i).data = readtable(names{i});
end
% this works only if the tables read from the files all
% have the same set of column names in the same order:
T = vertcat(csvFiles.data);
7 Comments
Voss
on 13 Mar 2024
readtable the file into a table T, then
T{:,10:17} = T{:,10:17}*9/5+32;
then writetable T out to a file.
More Answers (1)
Mathieu NOE
on 12 Mar 2024
maybe this ?
here I wanted to do a vertical concatenation - up to you to change that portion of the code
also , if processing the files must absolutely be done with correctly sorted files , please consider using this Fex submission :
fileDir = pwd; % current directory (or specify which one is the working directory)
outfile = 'OUT.dat'; % output file name
S = dir(fullfile(fileDir,'*.csv')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
out_data = [];
for k = 1:length(S)
filename = S(k).name; %
out = readmatrix( fullfile(fileDir, filename)); %
[m,n] = size(out);
if (m<1) | (n<1)
disp([' Warning : File :' filename ' is empty !'])
else
out_data = [out_data; out]; % vertical concatenation
end
end
% store out_data in excel file
writematrix(out_data,fullfile(fileDir,outfile),"Delimiter","tab");
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!