Merging Date and time

13 Ansichten (letzte 30 Tage)
Benju Baniya
Benju Baniya am 20 Apr. 2023
Bearbeitet: Stephen23 am 20 Apr. 2023
I have an eddy pro file that has date in datetime format and time in cell array. How do I merge them into single datetime? I have tried couple of codes with no luck. Thanks.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Apr. 2023
Bearbeitet: Stephen23 am 20 Apr. 2023
Rather than fiddling after importing, the best approach is to import the file correctly using READTABLE options, e.g.:
fnm = 'LI7500_eddypro_LP_2023_JFM_full_output_2023-04-17T105102_adv.csv';
opt = detectImportOptions(fnm, 'VariableNamesLine',2, 'VariableUnitsLine',3, 'VariableNamingRule','preserve');
opt = setvartype(opt, 'time','duration');
opt = setvartype(opt, 'date','datetime');
opt = setvaropts(opt, 'time','InputFormat','hh:mm');
opt = setvaropts(opt, 'date','InputFormat','M/d/yy');
Then the importing of every identically-formatted file is very simple, as well as adding the date+time:
tbl = readtable(fnm,opt)
tbl = 6×185 table
filename date time DOY daytime file_records used_records Tau qc_Tau rand_err_Tau H qc_H rand_err_H LE qc_LE rand_err_LE co2_flux qc_co2_flux rand_err_co2_flux h2o_flux qc_h2o_flux rand_err_h2o_flux ch4_flux qc_ch4_flux rand_err_ch4_flux none_flux qc_none_flux rand_err_none_flux H_strg LE_strg co2_strg h2o_strg ch4_strg none_strg co2_v-adv h2o_v-adv ch4_v-adv none_v-adv co2_molar_density co2_mole_fraction co2_mixing_ratio co2_time_lag co2_def_timelag h2o_molar_density h2o_mole_fraction h2o_mixing_ratio h2o_time_lag h2o_def_timelag ch4_molar_density ch4_mole_fraction ch4_mixing_ratio ch4_time_lag ch4_def_timelag none_molar_density none_mole_fraction none_mixing_ratio none_time_lag none_def_timelag sonic_temperature air_temperature air_pressure air_density air_heat_capacity air_molar_volume ET water_vapor_density e es specific_humidity RH VPD Tdew u_unrot v_unrot w_unrot u_rot v_rot w_rot wind_speed max_wind_speed wind_dir yaw pitch roll u* TKE L (z-d)/L bowen_ratio T* model x_peak x_offset x_10% x_30% x_50% x_70% x_90% un_Tau Tau_scf un_H H_scf un_LE LE_scf un_co2_flux co2_scf un_h2o_flux h2o_scf un_ch4_flux ch4_scf un_none_flux un_none_scf spikes_hf amplitude_resolution_hf drop_out_hf absolute_limits_hf skewness_kurtosis_hf skewness_kurtosis_sf discontinuities_hf discontinuities_sf timelag_hf timelag_sf attack_angle_hf non_steady_wind_hf u_spikes v_spikes w_spikes ts_spikes co2_spikes h2o_spikes ch4_spikes none_spikes head_detect_LI-7200 t_out_LI-7200 t_in_LI-7200 aux_in_LI-7200 delta_p_LI-7200 chopper_LI-7200 detector_LI-7200 pll_LI-7200 sync_LI-7200 chopper_LI-7500 detector_LI-7500 pll_LI-7500 sync_LI-7500 not_ready_LI-7700 no_signal_LI-7700 re_unlocked_LI-7700 bad_temp_LI-7700 laser_temp_unregulated_LI-7700 block_temp_unregulated_LI-7700 motor_spinning_LI-7700 pump_on_LI-7700 top_heater_on_LI-7700 bottom_heater_on_LI-7700 calibrating_LI-7700 motor_failure_LI-7700 bad_aux_tc1_LI-7700 bad_aux_tc2_LI-7700 bad_aux_tc3_LI-7700 box_connected_LI-7700 mean_value_RSSI_LI-7200 mean_value_LI-7500 u_var v_var w_var ts_var co2_var h2o_var ch4_var none_var w/ts_cov w/co2_cov w/h2o_cov w/ch4_cov w/none_cov air_t_mean vin_sf_mean co2_mean h2o_mean dew_point_mean co2_signal_strength_7500_mean ch4_mean air_p_mean rssi_77_mean __________________________________ ______ _____ _____ _______ ____________ ____________ ________ ______ ____________ _______ ____ __________ ________ _____ ___________ ________ ___________ _________________ ________ ___________ _________________ _________ ___________ _________________ _________ ____________ __________________ _______ _______ ________ ________ ________ _________ _________ _________ _________ __________ _________________ _________________ ________________ ____________ _______________ _________________ _________________ ________________ ____________ _______________ _________________ _________________ ________________ ____________ _______________ __________________ __________________ _________________ _____________ ________________ _________________ _______________ ____________ ___________ _________________ ________________ _________ ___________________ ______ ______ _________________ ______ ______ ______ ________ ________ _______ _______ _________ _______ __________ ______________ ________ ______ _____ ____ _______ _______ ________ _______ ___________ _______ _____ ______ ________ ______ ______ ______ _____ ______ ________ _______ _______ ______ ________ ______ ___________ _______ ___________ _______ ___________ _______ ____________ ___________ _________ _______________________ ___________ __________________ ____________________ ____________________ __________________ __________________ __________ __________ _______________ __________________ ________ ________ ________ _________ __________ __________ __________ ___________ ___________________ _____________ ____________ ______________ _______________ _______________ ________________ ___________ ____________ _______________ ________________ ___________ ____________ _________________ _________________ ___________________ ________________ ______________________________ ______________________________ ______________________ _______________ _____________________ ________________________ ___________________ _____________________ ___________________ ___________________ ___________________ _____________________ _______________________ __________________ _______ _______ _______ _______ ________ _______ ________ ________ ________ _________ _________ _________ __________ __________ ___________ ________ ________ ______________ _____________________________ ________ __________ ____________ {'2023-01-09T160000_AIU-1485.ghg'} 1/9/23 16:30 9.687 1 17935 17935 0.0665 1 NaN -14.738 1 NaN 26.778 1 NaN 0.0367 2 NaN 0.60063 1 NaN 0.00191 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN -401.12 -7.4227 -2.1283 NaN 16.96 387.3 390.11 0.2 0 313.85 7.1671 7.2188 0 0 0.09 2.0783 2.0934 2.1 0 NaN NaN NaN NaN 9 284.22 284.13 1.0345e+05 1.265 1009.2 0.0228 0.0389 0.00566 741.6 1307 0.00447 56.742 565.37 275.85 0.0297 -1.7682 0.21998 1.7818 6.68e-16 -0.0237 1.7819 3.8905 69.039 271.12 NaN NaN 0.22929 0.30973 73.058 0.37449 -0.55036 -0.0503 0 186.89 -28.361 64.154 159.75 243.63 341.7 511.92 0.0659 1.0093 -13.106 1.0065 26.491 1.0251 0.48109 1.0251 0.59418 1.0251 0.00422 1.1024 NaN NaN 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 81110 81110 80 80 4 10 1 27 6 7 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16028 16028 16028 16028 NaN 100 0.21354 0.19176 0.21417 0.0283 0.00137 26.625 2.35e-07 NaN -0.0103 0.000481 0.59418 4.22e-06 NaN 285.13 0 388.63 7.2342 2.7633 103.81 2.0718 1.0152e+05 42.415 {'2023-01-09T163000_AIU-1485.ghg'} 1/9/23 17:00 9.708 0 17903 17903 0.0166 1 NaN -12.721 1 NaN 7.0114 1 NaN -0.0921 1 NaN 0.15717 1 NaN -0.0102 1 NaN NaN NaN NaN -12.121 1.1637 0.99663 0.0261 0.0161 NaN -242.37 -4.4919 -1.2962 NaN 17.059 388.73 391.61 0.1 0 316.17 7.2045 7.2568 0.1 0 0.0912 2.1015 2.1171 -1.7 0 NaN NaN NaN NaN 9 283.64 283.54 1.0345e+05 1.2676 1009.2 0.0228 0.0102 0.0057 745.5 1256.2 0.00449 59.343 510.75 275.92 -0.15911 -1.2705 0.15138 1.2886 4.24e-16 -0.0142 1.2887 3.3667 77.138 263.12 NaN NaN 0.1146 0.1211 10.566 2.5894 -1.8143 -0.0868 1 235.34 103 217 444 831 1794 7645 0.0165 1.0076 -12.2 1.011 6.9526 1.0723 0.41505 1.0723 0.15586 1.0723 -0.00491 1.3468 NaN NaN 8e+08 8e+08 8e+08 8e+08 8e+08 8.011e+08 8e+08 8e+08 80100 80100 80 81 18 15 2 13 18 9 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17778 17778 17778 17778 NaN 100 0.13076 0.0369 0.0746 0.0403 0.000594 20.289 9.46e-08 NaN -0.00954 0.000415 0.15586 -4.91e-06 NaN 283.86 0 389.15 7.3729 3.0325 103.8 2.0948 1.0152e+05 42.366 {'2023-01-09T170000_AIU-1485.ghg'} 1/9/23 17:30 9.729 0 18000 18000 0.0042 1 NaN 5.7231 2 NaN -2.8692 1 NaN -0.2457 1 NaN -0.0642 1 NaN -0.00367 1 NaN NaN NaN NaN -42.241 -9.2783 -0.34004 -0.20758 0.0021 NaN -853.1 -15.181 -4.5823 NaN 17.166 388.25 391.21 -0.6 0 305.47 6.9089 6.957 -0.6 0 0.0922 2.1045 2.1206 -2.1 0 NaN NaN NaN NaN 9 282.86 281.48 1.0347e+05 1.2773 1009 0.0226 -0.00416 0.0055 715.05 1093.9 0.00431 65.368 378.83 275.33 0.19319 -0.88732 0.12718 0.91694 2.29e-16 -0.0497 0.91828 1.4566 57.717 282.37 NaN NaN 0.0573 0.0288 -2.9419 -9.3002 -1.9946 0.0774 1 100.4 45 78 125 184 285 602 0.00391 1.0741 5.2987 1.0487 -2.9055 1.0548 -0.46602 1.0548 -0.065 1.0548 -0.00481 1.0889 NaN NaN 8e+08 8e+08 8e+08 8e+08 8e+08 8.001e+08 8e+08 8e+08 80000 80000 80 81 18 6 1 4 0 12 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18000 18000 18000 18000 NaN 100 0.0136 0.0379 0.00614 0.11397 0.000952 17.661 8.23e-08 NaN 0.00411 -0.000466 -0.065 -4.81e-06 NaN 281.95 0 388.86 7.5936 3.4526 103.78 2.0979 1.0155e+05 42.044 {'2023-01-09T173000_AIU-1485.ghg'} 1/9/23 18:00 9.749 0 18000 18000 0.000491 1 NaN 0.0756 2 NaN 2.2866 1 NaN -0.0281 2 NaN 0.0511 1 NaN 2.23e-06 1 NaN NaN NaN NaN -36.517 -5.8353 0.76599 -0.13033 0.00283 NaN -989.78 -17.096 -5.318 NaN 17.329 389.33 392.35 -0.6 0 299.31 6.7245 6.7701 0.6 0 0.0931 2.1086 2.1249 -1.3 0 NaN NaN NaN NaN 9 281.07 279.71 1.0351e+05 1.2859 1008.9 0.0225 0.00331 0.00539 696.25 969.52 0.00419 71.814 273.27 274.96 0.21159 -0.26596 0.0588 0.34491 -6.19e-17 -0.0571 0.34961 0.94274 31.496 308.33 NaN NaN 0.0195 0.0209 -8.8214 -3.1015 0.0331 0.00298 1 185.49 83 144 233 343 534 1140 0.000429 1.1451 0.18896 1.0968 2.0548 1.1039 -0.0467 1.1039 0.0459 1.1039 -0.000155 1.1456 NaN NaN 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 80000 80000 80 81 1 0 0 2 0 1 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18000 18000 18000 18000 NaN 100 0.0297 0.0105 0.00147 0.79534 0.00722 33.623 9.67e-08 NaN 0.000146 -4.67e-05 0.0459 -1.55e-07 NaN 280.09 0 389.81 7.7078 3.6694 103.75 2.1019 1.0158e+05 41.785 {'2023-01-09T180000_AIU-1485.ghg'} 1/9/23 18:30 9.77 0 18000 18000 0.000438 1 NaN -1.2536 0 NaN -0.49682 0 NaN 0.0592 1 NaN -0.0111 0 NaN -0.00531 0 NaN NaN NaN NaN -38.084 1.1723 2.2123 0.0261 -0.0036 NaN -1258.7 -21.685 -6.701 NaN 17.587 392.44 395.53 0.6 0 303 6.7613 6.8073 -0.6 0 0.0936 2.1035 2.12 -2.1 0 NaN NaN NaN NaN 9 279.64 277.89 1.0354e+05 1.2947 1008.9 0.0223 -0.000718 0.00546 700.22 854.02 0.00422 81.991 153.8 275.04 0.10415 -0.00275 0.00168 0.10332 -7.04e-17 -0.0716 0.12569 0.38919 341.51 358.39 NaN NaN 0.0184 0.00926 0.44324 61.727 2.5232 -0.0522 1 5.6053 2 5 9 16 32 109 0.000412 1.0627 -1.1358 1.1212 -0.26417 1.6903 0.0736 1.6903 -0.00589 1.6903 -0.000928 5.2186 NaN NaN 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 8e+08 81000 81000 81 81 2 9 0 0 0 0 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18000 18000 18000 18000 NaN 100 0.0118 0.005 0.00168 0.14606 0.00763 17.434 6.13e-08 NaN -0.00087 7.36e-05 -0.00589 -9.28e-07 NaN 278.3 0 392.99 7.8211 3.8806 103.71 2.0966 1.016e+05 40.409 {'2023-01-09T183000_AIU-1485.ghg'} 1/9/23 19:00 9.791 0 18000 18000 0.000412 1 NaN 1.9632 2 NaN 1.2987 1 NaN -0.88109 1 NaN 0.0289 1 NaN -0.000768 1 NaN NaN NaN NaN -25.038 -1.1225 3.5124 -0.025 -0.00132 NaN -1097.2 -18.573 -5.7674 NaN 17.89 397.35 400.48 -0.6 0 302.84 6.7263 6.7719 0.6 0 0.094 2.1016 2.1182 2.1 0 NaN NaN NaN NaN 9 278.41 276.69 1.0357e+05 1.3008 1008.9 0.0222 0.00187 0.00546 696.85 785.19 0.0042 88.749 88.339 274.97 0.10182 -0.10158 0.0218 0.14544 -1.42e-16 -0.0613 0.15784 0.5507 24.933 314.76 NaN NaN 0.0178 0.009 -0.25643 -106.7 1.5117 0.0841 1 16.632 8 13 21 30 47 99 0.000336 1.2271 1.7659 1.1535 1.0462 1.1626 -0.85092 1.1626 0.0233 1.1626 -0.00121 1.217 NaN NaN 8e+08 8e+08 8e+08 8e+08 8.0001e+08 8.0011e+08 8e+08 8e+08 80110 80110 81 81 7 24 0 4 1 4 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18000 18000 18000 18000 NaN 100 0.00901 0.00707 0.00191 0.46931 0.0294 20.456 6.77e-08 NaN 0.00135 -0.000851 0.0233 -1.21e-06 NaN 277.08 0 397.87 7.8389 3.9175 103.67 2.0946 1.0162e+05 38.505
dt = tbl.date + tbl.time;
dt.Format = 'uuuu-MM-dd HH:mm:ss'
dt = 6×1 datetime array
2023-01-09 16:30:00 2023-01-09 17:00:00 2023-01-09 17:30:00 2023-01-09 18:00:00 2023-01-09 18:30:00 2023-01-09 19:00:00
  9 Kommentare
Benju Baniya
Benju Baniya am 20 Apr. 2023
Excel. The first version was edited. The zipped file I just uloaded is original version from eddy pro software. And now I understanf excel might have changed something during edits.
Stephen23
Stephen23 am 20 Apr. 2023
Bearbeitet: Stephen23 am 20 Apr. 2023
"And now I understanf excel might have changed something during edits. "
The only reason to use MS Excel for editing a CSV file is if you want random, unpredictable, untraceable, unhelpful changes to your filedata without any warning. For all other purposes, avoid getting MS Excel anywhere near textfiles.
In any case, there is no "might have" here, whatever app you used definitely did change the date formats, as my previous comments and screenshots showed. Having mangled data files made it harder to help you.
Tip for the future: look at your text files in an actual text editor. Not in Excel (it changes your data).
Look. At. Your. Data. Files.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 20 Apr. 2023
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1361583/LI7500_eddypro_LP_2023_JFM_full_output_2023-04-17T105102_adv.csv') ;
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, supply a datetime format using SETVAROPTS, e.g.
opts = setvaropts(opts,varname,'InputFormat','MM/dd/uuuu');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
dates = T.(2) ;
TimeStrings = T.(3) ;
times = datenum(TimeStrings);
t = table(dates,times);
dates = datetime(t.dates,'Format','MM/dd/yyyy HH:mm');
times = datetime(t.times,'ConvertFrom','datenum','Format','MM/dd/yyyy HH:mm:SS');
%Add dates to times.
fullt = dates+timeofday(times);
t.DatesNTimes = fullt;
iwant = t.(3)
iwant = 6×1 datetime array
01/09/0023 16:30 01/09/0023 17:00 01/09/0023 17:30 01/09/0023 18:00 01/09/0023 18:30 01/09/0023 19:00
  1 Kommentar
Benju Baniya
Benju Baniya am 20 Apr. 2023
Bearbeitet: Benju Baniya am 20 Apr. 2023
This works. Thank you so much.

Melden Sie sich an, um zu kommentieren.


Askic V
Askic V am 20 Apr. 2023
I would also like to suggest a bit different approach:
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1361583/LI7500_eddypro_LP_2023_JFM_full_output_2023-04-17T105102_adv.csv') ;
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, supply a datetime format using SETVAROPTS, e.g.
opts = setvaropts(opts,varname,'InputFormat','MM/dd/uuuu');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
dates = string(T.(2));
times = string(T.(3));
dates_times = strcat(dates, " ", times);
C = datetime(dates_times, 'InputFormat', 'dd/MM/yyyy HH:mm')
C = 6×1 datetime array
01-Sep-0023 16:30:00 01-Sep-0023 17:00:00 01-Sep-0023 17:30:00 01-Sep-0023 18:00:00 01-Sep-0023 18:30:00 01-Sep-0023 19:00:00
  1 Kommentar
Benju Baniya
Benju Baniya am 20 Apr. 2023
Thank you so mcuh but this gives me error : Error using datetime
Unable to convert the text to datetime using the format 'dd/MM/yyyy HH:mm'.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by