JOIN CVS FILES AND PLOT
6 views (last 30 days)
Show older comments
I have 4 csv files, I try to generate a single graph and generate a single new file, I have tried from a .xlsx code but I have not been able to.
fileDir = pwd; % current directory (or specify which one is the working directory)
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
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
T = readtable(fullfile(fileDir, filename));
x = T{:,1} + days(T{:,2}); % Combining date and time
y = T{:,3}; % Pressure data
plot(x,y);
hold on
end
% save new file
writetable(s, 'new_file.csv');
when I have a single csv file I do it as follows:
data1 = readtable('file.csv', 'VariableNamingRule','preserve');
MyDateTime = data1.Date + data1.Time;
MyDateTime.Format = 'yyyy-MM-dd HH:mm:ss';
data2 = [data1(:,1) table(MyDateTime) data1(:,[3:end])];
figure (1)
plot(data2.MyDateTime, data2.('FIT'),'-r')
grid on
hold on
I would appreciate help on the first code when I have multiple csv files. Matlab R2021a
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1758069/image.png)
0 Comments
Answers (1)
Voss
on 22 Aug 2024 at 16:32
fileDir = pwd; % current directory (or specify which one is the working directory)
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
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
T = readtable(fullfile(fileDir, filename),'VariableNamingRule','preserve');
S(k).data = T; % store each table in an element of the S struct array
end
T = vertcat(S.data) % combine all the tables
% plot all the data
x = T.Date + T.Time; % Combining date and time
y = T.FIT; % Pressure data
plot(x,y);
% save new file
writetable(T, 'new_file.csv');
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!