How do I replace one data column from a text file to the data column of another text file

4 views (last 30 days)
Data column 6 from file outputrch.txt needs to be replaced by data column 4 from outputsub.txt.

Answers (1)

Les Beckham
Les Beckham on 1 Nov 2022
Edited: Les Beckham on 1 Nov 2022
rch = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1175438/outputrch.txt', ...
'HeaderLines', 8)
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.
rch = 212×50 table
RCH GIS MON AREAkm2 FLOW_INcms FLOW_OUTcms EVAPcms TLOSScms SED_INtons SED_OUTtonsSEDCONCmg_kg ORGN_INkg ORGN_OUTkg ORGP_INkg ORGP_OUTkg NO3_INkg NO3_OUTkg NH4_INkg NH4_OUTkg NO2_INkg NO2_OUTkg MINP_INkg MINP_OUTkg CHLA_INkg CHLA_OUTkg CBOD_INkg CBOD_OUTkg DISOX_INkg DISOX_OUTkg SOLPST_INmgSOLPST_OUTmg SORPST_INmgSORPST_OUTmg REACTPSTmg VOLPSTmg SETTLPSTmgRESUSP_PSTmgDIFFUSEPSTmgREACBEDPSTmg BURYPSTmg BED_PSTmg BACTP_OUTctBACTLP_OUTct CMETAL_1kg CMETAL_2kg CMETAL_3kg TOT Nkg TOT_1 Pkg NO3ConcMg_l Var45 Var46 Var47 Var48 Var49 Var50 _________ ___ ___ _______ __________ ___________ _______ ________ __________ _______________________ _________ __________ _________ __________ ________ _________ _________ _________ _________ _________ _________ __________ _________ __________ _________ __________ __________ ___________ _______________________ _______________________ __________ ________ ______________________________________________ _________ _________ _______________________ __________ __________ __________ ___ ___ _____ ___ ___________ _____ _____ _____ _________ _________ _____ {'REACH'} 1 0 1 18140 49.68 50.63 1.943 0 2.522e-07 0 0 0.2255 0.05123 0.06727 0 1153 1254 0 190 0 37.39 0.01414 46.79 0 0 0 0 0 3.181e+05 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1481 46.79 0 {'REACH'} 2 0 1 42760 236.9 250.6 9.527 0 3.387 1.165 0.001256 2018 847.7 498.7 127.5 7410 9362 0 937.7 0 404.5 128.5 629.8 0 0 0 0 0 5.467e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11550 757.3 0 {'REACH'} 3 0 1 17530 44.74 46.3 2.738 0 0.004958 0.002245 8.74e-06 29.57 14.22 7.995 2.415 1382 1588 0 237.9 0 92.23 1.246 57.01 0 0 0 0 0 1.005e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1932 59.42 0 {'REACH'} 4 0 1 12050 47.67 46.98 0.513 0 1.862e-08 5.406e-09 1.779e-11 0.02245 0 0.06734 0 1068 1055 0 85.81 0 0 0.02248 0 0 0 0 0 0 378 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1140 0 0 {'REACH'} 5 0 1 31820 150.9 160.5 7.509 0 4.354 1.449 0.002376 3770 1540 888.8 219.6 6554 8572 0 1088 0 479.9 177.5 921.3 0 0 0 0 0 3.5e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11680 1141 0 {'REACH'} 6 0 1 16690 78.66 80.55 2.235 0 0.0001324 6.65e-05 1.389e-07 24.74 13.44 5.835 2.048 3058 3319 0 283.9 0 98.74 0.8347 57.16 0 0 0 0 0 1.744e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3715 59.21 0 {'REACH'} 7 0 1 20260 294 297.9 3.48 0 63.3 28.11 0.02782 11470 5883 2725 885.3 18850 22130 0 3103 0 1201 232.5 2258 0 0 0 0 0 6.499e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32320 3143 0 {'REACH'} 8 0 1 13890 56.66 56.38 0.2595 0 1.773e-08 1.487e-08 1.623e-10 0.02462 0 0.07385 0.01954 1271 1266 0 105.6 0 0 0.02466 0 0 0 0 0 0 453 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1371 0.01954 0 {'REACH'} 9 0 1 27910 480.7 485.4 4.557 0 26.7 11.98 0.00737 3854 1971 939.9 303.5 24540 26830 0 1518 0 577.2 195.3 996.1 0 0 0 0 0 1.059e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30900 1300 0 {'REACH'} 10 0 1 4.681e+05 2996 3021 12.11 0 3970 2597 0.2669 2.959e+05 1.704e+05 22630 8924 3.314e+06 3.53e+06 1.564e+05 96310 1.049e+05 72580 8.6e+05 8.786e+05 0 0 0 0 6.427e+07 6.561e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.87e+06 8.875e+05 0 {'REACH'} 11 0 1 40710 137.5 147.2 6.97 0 0.03722 0.01759 3.006e-05 57.7 24.77 13.1 3.402 3644 4755 323.7 436.7 92.23 246.4 60.29 172.1 0 0 0 0 1.005e+06 3.201e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5462 175.5 0 {'REACH'} 12 0 1 53260 249.1 254.1 4.417 0 1.872 1.205 0.001104 2222 1213 383.1 139.6 12010 14190 1372 1374 578.7 835.5 987 1350 0 0 0 0 5.244e+06 5.541e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17610 1489 0 {'REACH'} 13 0 1 60800 830.9 840.9 8.533 0 2610 1095 0.5132 2.116e+05 1.048e+05 43750 13770 64770 1.084e+05 4621 47020 1778 19530 4841 34650 0 0 0 0 1.709e+07 1.831e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.797e+05 48410 0 {'REACH'} 14 0 1 24240 158.1 164.5 4.342 0 0.0496 0.02077 2.895e-05 269.9 130.1 65.79 19.79 4637 5359 0 432 0 170.6 10.95 141.4 0 0 0 0 0 3.59e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6092 161.2 0 {'REACH'} 15 0 1 79110 421.9 445.2 15.27 0 23.25 9.865 0.006676 2479 1065 681.3 179.1 14820 18910 1128 1574 441.9 863.4 968.7 1705 0 0 0 0 5.785e+06 9.701e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22410 1884 0 {'REACH'} 16 0 1 1.216e+05 1110 1123 12 0 1149 557.3 0.1859 1.249e+05 65430 18810 6420 1.226e+05 1.945e+05 48390 35430 20370 25070 36100 49970 0 0 0 0 2.385e+07 2.445e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.205e+05 56390 0
sub = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1175443/outputsub.txt', ...
'HeaderLines', 8)
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.
sub = 212×26 table
SUB GIS MON AREAkm2 PRECIPmm SNOMELTmm PETmm ETmm SWmm PERCmm SURQmm GW_Qmm WYLDmm SYLDt_ha ORGNkg_ha ORGPkg_haNSURQkg_ha SOLPkg_ha SEDPkg_ha LAT Q_mm_LATNO3kg_hGWNO3kg_haCHOLAmic_LCBODU mg_L DOXQ mg_L_1 TNO3kg_ha Var25 Var26 __________ ___ ___ __________ ________ _________ ______ ______ ______ ______ ______ ______ ______ ________ _________ ___________________ _________ _________ _____ ________________________________________ ____ _____ ______ _________ _____ _____ {'BIGSUB'} 1 0 1.1814e+05 0.1 0 118.12 13.575 96.811 0 0 7.304 7.334 0 0 0 0 0 0 0.03 0 0.001 0 0 0 0 {'BIGSUB'} 2 0 1.4276e+05 2.5 0 119.19 16.449 96.541 0 0.137 14.67 14.838 0 0 0 0 0 0 0.031 0 0.001 0 0 0 0 {'BIGSUB'} 3 0 1.1753e+05 0.1 0 117.83 11.944 72.767 0 0.003 6.761 6.834 0 0 0 0 0 0 0.07 0 0 0 0 0 0 {'BIGSUB'} 4 0 1.1205e+05 0.1 0 118.04 8.928 50.588 0 0 10.561 10.592 0 0 0 0 0 0 0.031 0 0.001 0 0 0 0 {'BIGSUB'} 5 0 1.3182e+05 2.5 0 119.94 15.037 72.915 0 0.251 12.381 12.699 0 0.001 0 0 0 0 0.068 0 0.001 0 0 0 0 {'BIGSUB'} 6 0 1.1669e+05 2.5 0 119.92 15.035 72.918 0 0.002 12.562 12.624 0 0 0 0 0 0 0.059 0 0.002 0 0 0 0 {'BIGSUB'} 7 0 1.2026e+05 16.6 0 109.55 27.105 84.91 0 0.535 38.244 38.867 0 0.006 0.001 0 0 0.001 0.087 0 0.009 0 0 0 0 {'BIGSUB'} 8 0 1.1388e+05 0.1 0 116.5 9.018 50.693 0 0 10.901 10.929 0 0 0 0 0 0 0.029 0 0.001 0 0 0 0 {'BIGSUB'} 9 0 1.2791e+05 16.6 0 109.48 24.007 60.833 0 0.32 45.72 46.134 0 0.001 0 0 0 0 0.095 0 0.008 0 0 0 0 {'BIGSUB'} 10 0 19895 0.1 0 118.42 8.995 50.618 0 0 10.977 11.003 0 0 0 0 0 0 0.026 0 0.001 0 0 0 0 {'BIGSUB'} 11 0 1.1112e+05 0.1 0 118.17 8.918 50.577 0 0.013 10.603 10.646 0 0 0 0 0 0 0.03 0 0.001 0 0 0 0 {'BIGSUB'} 12 0 14750 2.5 0 120.19 15.882 197.72 6.382 0.077 4.253 4.522 0 0.001 0 0 0 0 0.192 0 0 0 0 0 0 {'BIGSUB'} 13 0 1.1263e+05 16.6 0 109.74 33.988 269.29 6.663 4.952 4.743 10.071 0.002 0.161 0.02 0.012 0.001 0.014 0.377 0 0 0 0 0 0 {'BIGSUB'} 14 0 1.2424e+05 2.5 0 119.27 12.481 49.829 0 0.021 17.406 17.476 0 0 0 0 0 0 0.049 0 0.002 0 0 0 0 {'BIGSUB'} 15 0 1.1821e+05 2.3 0 117.47 12.598 52.582 0 0.764 16.913 17.743 0 0.001 0 0 0 0 0.066 0 0.002 0 0 0 0 {'BIGSUB'} 16 0 17554 2.5 0 120.05 16.072 198.38 6.413 0.636 4.314 5.145 0 0.025 0.003 0 0 0.003 0.196 0 0 0 0 0 0
rch{1:10,6}
ans = 10×1
1.0e+03 * 0.0497 0.2369 0.0447 0.0477 0.1509 0.0787 0.2940 0.0567 0.4807 2.9960
sub{1:10,4}
ans = 10×1
1.0e+05 * 1.1814 1.4276 1.1753 1.1205 1.3182 1.1669 1.2026 1.1388 1.2791 0.1990
rch{:,6} = sub{:,4}; % replace rch column 6 with sub column 4
rch % display to see if it worked
rch = 212×50 table
RCH GIS MON AREAkm2 FLOW_INcms FLOW_OUTcms EVAPcms TLOSScms SED_INtons SED_OUTtonsSEDCONCmg_kg ORGN_INkg ORGN_OUTkg ORGP_INkg ORGP_OUTkg NO3_INkg NO3_OUTkg NH4_INkg NH4_OUTkg NO2_INkg NO2_OUTkg MINP_INkg MINP_OUTkg CHLA_INkg CHLA_OUTkg CBOD_INkg CBOD_OUTkg DISOX_INkg DISOX_OUTkg SOLPST_INmgSOLPST_OUTmg SORPST_INmgSORPST_OUTmg REACTPSTmg VOLPSTmg SETTLPSTmgRESUSP_PSTmgDIFFUSEPSTmgREACBEDPSTmg BURYPSTmg BED_PSTmg BACTP_OUTctBACTLP_OUTct CMETAL_1kg CMETAL_2kg CMETAL_3kg TOT Nkg TOT_1 Pkg NO3ConcMg_l Var45 Var46 Var47 Var48 Var49 Var50 _________ ___ ___ _______ __________ ___________ _______ ________ __________ _______________________ _________ __________ _________ __________ ________ _________ _________ _________ _________ _________ _________ __________ _________ __________ _________ __________ __________ ___________ _______________________ _______________________ __________ ________ ______________________________________________ _________ _________ _______________________ __________ __________ __________ ___ ___ _____ ___ ___________ _____ _____ _____ _________ _________ _____ {'REACH'} 1 0 1 18140 1.1814e+05 50.63 1.943 0 2.522e-07 0 0 0.2255 0.05123 0.06727 0 1153 1254 0 190 0 37.39 0.01414 46.79 0 0 0 0 0 3.181e+05 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1481 46.79 0 {'REACH'} 2 0 1 42760 1.4276e+05 250.6 9.527 0 3.387 1.165 0.001256 2018 847.7 498.7 127.5 7410 9362 0 937.7 0 404.5 128.5 629.8 0 0 0 0 0 5.467e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11550 757.3 0 {'REACH'} 3 0 1 17530 1.1753e+05 46.3 2.738 0 0.004958 0.002245 8.74e-06 29.57 14.22 7.995 2.415 1382 1588 0 237.9 0 92.23 1.246 57.01 0 0 0 0 0 1.005e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1932 59.42 0 {'REACH'} 4 0 1 12050 1.1205e+05 46.98 0.513 0 1.862e-08 5.406e-09 1.779e-11 0.02245 0 0.06734 0 1068 1055 0 85.81 0 0 0.02248 0 0 0 0 0 0 378 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1140 0 0 {'REACH'} 5 0 1 31820 1.3182e+05 160.5 7.509 0 4.354 1.449 0.002376 3770 1540 888.8 219.6 6554 8572 0 1088 0 479.9 177.5 921.3 0 0 0 0 0 3.5e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11680 1141 0 {'REACH'} 6 0 1 16690 1.1669e+05 80.55 2.235 0 0.0001324 6.65e-05 1.389e-07 24.74 13.44 5.835 2.048 3058 3319 0 283.9 0 98.74 0.8347 57.16 0 0 0 0 0 1.744e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3715 59.21 0 {'REACH'} 7 0 1 20260 1.2026e+05 297.9 3.48 0 63.3 28.11 0.02782 11470 5883 2725 885.3 18850 22130 0 3103 0 1201 232.5 2258 0 0 0 0 0 6.499e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32320 3143 0 {'REACH'} 8 0 1 13890 1.1388e+05 56.38 0.2595 0 1.773e-08 1.487e-08 1.623e-10 0.02462 0 0.07385 0.01954 1271 1266 0 105.6 0 0 0.02466 0 0 0 0 0 0 453 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1371 0.01954 0 {'REACH'} 9 0 1 27910 1.2791e+05 485.4 4.557 0 26.7 11.98 0.00737 3854 1971 939.9 303.5 24540 26830 0 1518 0 577.2 195.3 996.1 0 0 0 0 0 1.059e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30900 1300 0 {'REACH'} 10 0 1 4.681e+05 19895 3021 12.11 0 3970 2597 0.2669 2.959e+05 1.704e+05 22630 8924 3.314e+06 3.53e+06 1.564e+05 96310 1.049e+05 72580 8.6e+05 8.786e+05 0 0 0 0 6.427e+07 6.561e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.87e+06 8.875e+05 0 {'REACH'} 11 0 1 40710 1.1112e+05 147.2 6.97 0 0.03722 0.01759 3.006e-05 57.7 24.77 13.1 3.402 3644 4755 323.7 436.7 92.23 246.4 60.29 172.1 0 0 0 0 1.005e+06 3.201e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5462 175.5 0 {'REACH'} 12 0 1 53260 14750 254.1 4.417 0 1.872 1.205 0.001104 2222 1213 383.1 139.6 12010 14190 1372 1374 578.7 835.5 987 1350 0 0 0 0 5.244e+06 5.541e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17610 1489 0 {'REACH'} 13 0 1 60800 1.1263e+05 840.9 8.533 0 2610 1095 0.5132 2.116e+05 1.048e+05 43750 13770 64770 1.084e+05 4621 47020 1778 19530 4841 34650 0 0 0 0 1.709e+07 1.831e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.797e+05 48410 0 {'REACH'} 14 0 1 24240 1.2424e+05 164.5 4.342 0 0.0496 0.02077 2.895e-05 269.9 130.1 65.79 19.79 4637 5359 0 432 0 170.6 10.95 141.4 0 0 0 0 0 3.59e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6092 161.2 0 {'REACH'} 15 0 1 79110 1.1821e+05 445.2 15.27 0 23.25 9.865 0.006676 2479 1065 681.3 179.1 14820 18910 1128 1574 441.9 863.4 968.7 1705 0 0 0 0 5.785e+06 9.701e+06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22410 1884 0 {'REACH'} 16 0 1 1.216e+05 17554 1123 12 0 1149 557.3 0.1859 1.249e+05 65430 18810 6420 1.226e+05 1.945e+05 48390 35430 20370 25070 36100 49970 0 0 0 0 2.385e+07 2.445e+07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.205e+05 56390 0
Note that this replaced the data in that column but didn't change the column name.
writetable(rch, 'newrch.txt'); % write the modified data to a file

Categories

Find more on Environment and Settings 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!