JOIN CVS FILES AND PLOT

조회 수: 5 (최근 30일)
Willian
Willian 2024년 8월 22일
편집: Walter Roberson 2024년 9월 3일
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

채택된 답변

Voss
Voss 2024년 8월 22일
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
filename = '01 HRS.csv'
filename = '02 HRS.csv'
filename = '03 HRS.csv'
filename = '04 HRS.csv'
T = vertcat(S.data) % combine all the tables
T = 1448x8 table
No. Date Time Millitm PIT TIT PD FIT ___ __________ ________ _______ ______ ______ ______ ______ NaN NaT NaN NaN NaN NaN NaN NaN 1 2024-08-18 00:04:55 147 152.02 66.019 60.657 2591.9 2 2024-08-18 00:05:05 163 152.02 66.078 61.257 2588.8 3 2024-08-18 00:05:15 143 152.02 66.019 61.19 2588.1 4 2024-08-18 00:05:25 163 151.9 66.019 61.057 2596.5 5 2024-08-18 00:05:35 169 152.02 66.019 59.524 2592.3 6 2024-08-18 00:05:45 138 153.6 66.019 56.591 2526.3 7 2024-08-18 00:05:55 131 154.58 66.137 57.524 2523.6 8 2024-08-18 00:06:05 135 154.7 66.196 60.523 2558.8 9 2024-08-18 00:06:15 145 154.09 66.254 60.957 2602.2 10 2024-08-18 00:06:25 166 153.24 66.254 61.49 2608 11 2024-08-18 00:06:35 152 152.26 66.137 62.19 2623.8 12 2024-08-18 00:06:45 161 151.41 66.019 62.29 2618.1 13 2024-08-18 00:06:55 144 151.17 65.96 61.123 2602.4 14 2024-08-18 00:07:05 158 151.29 65.901 60.857 2571.4 15 2024-08-18 00:07:15 168 151.41 65.842 60.99 2574.6
% 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
Willian
Willian 2024년 9월 3일
tks for you support
Voss
Voss 2024년 9월 3일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by