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)
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

Accepted Answer

Voss
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
Md Sadiq
Md Sadiq on 13 Mar 2024
Hello,
The data in column 10-17 (for the attached picture) are temperature in celcius unit.How I can convert these data in Farenheit unit? In excel,it's easy.But my number of rows exceed excel limit.So,I need to convert them in Matlab for plotting the temperature in Farenheit unit.Can you help?

Sign in to comment.

More Answers (1)

Mathieu NOE
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");

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!