JOIN CVS FILES AND PLOT
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Willian
am 22 Aug. 2024
Bearbeitet: Walter Roberson
am 3 Sep. 2024 um 22:05
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
0 Kommentare
Akzeptierte Antwort
Voss
am 22 Aug. 2024
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');
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!