converting .txt file to .csv file with mixed delimiter

39 views (last 30 days)
Hi there,
I want to convert the .txt file into .csv file. The file has mixed delimiter (Comma, tab) like this.
fileilename = '/Users/gulfcarbon2/Downloads/Modis Data/7da2190bbf_NSF_Rapid_HPLC_2018_AC.txt';
csvfile1 = readtable(filename, 'Delimiter','tab' );
writetable(csvfile1, 'seadasfile1.csv');
This is the code I wrote but the result is coming without any headerline (var1, var2 etc). Kindly help me out to write a function i can get the header in the 1st row and unit in the next. I have started coding in Matlab so every help is appriciated.
Thank you

Answers (3)

dpb
dpb on 27 Jul 2022
Using the import tool manually, just go to the section on 'delimiters' and check the comma as well as the identified blank -- it isn't tab-delimited but blank for the bulk of the file so the tab substitution didn't do anything, anyways.

dpb
dpb on 26 Jul 2022
Edited: dpb on 27 Jul 2022
filename='/Users/gulfcarbon2/Downloads/Modis Data/7da2190bbf_NSF_Rapid_HPLC_2018_AC.txt';
data=readlines(filename);
data=strrep(data,char(9),',');
wwritematrix(data,'seadasfile1.csv','QuoteStrings',0); % correction; w R2022B and earlier
  4 Comments
Arnab Paul
Arnab Paul on 27 Jul 2022
I tried writelines and writematrix.
Result is coming like as in the image whiile using writelines.
I think the the first two headlines are needed to be treated differently.
dpb
dpb on 27 Jul 2022
We can't do anything about testing anything with just images and you've not provided a sample (short) test file.
Nor have you specificied clearly what the end objective is???

Sign in to comment.


Arnab Paul
Arnab Paul on 27 Jul 2022
filename='7da2190bbf_NSF_Rapid_HPLC_2018_AC.txt';
data=readlines(filename);
Error using readlines
Unable to find or open '/Users/gulfcarbon2/Downloads/Modis Data/7da2190bbf_NSF_Rapid_HPLC_2018_AC.txt'. Check the path and filename or file permissions.
data=strrep(data,char(9),',');
wwritematrix(data,'seadasfile1.csv','QuoteStrings',0); % correction; w R2022B and earlier
  3 Comments
Arnab Paul
Arnab Paul on 27 Jul 2022
pl_id,sample,station,volfilt,depth,date,time,lat,lon,Tot_Chl_a,Tot_Chl_b,Tot_Chl_c,alpha-beta-Car,But-fuco,Hex-fuco,Allo,Diadino,Diato,Fuco,Perid,Zea,MV_Chl_a,DV_Chl_a,Chlide_a,MV_Chl_b,DV_Chl_b,Chl_c1c2,Chl_c3,Lut,Neo,Viola,Phytin_a,Phide_a,Pras,Gyro,TChl,PPC,PSC,PSP,Tcar,Tacc,Tpg,DP,Tacc_Tchla,PSC_Tcar,PPC_Tcar,TChl_Tcar,PPC_Tpg,PSP_Tpg,TChla_Tpg none,none,none,L,m,yyyymmdd,hh:mm:ss,degrees,degrees,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,mg/m^3,none,none,none,none,none,none,none LO1 K3-S K3 1.500 0.0 20170918 00:00:00 29.4883 -93.4167 1.855 0.160 0.143 0.159 0.008 0.088 0.057 0.124 0.022 0.144 0.071 0.652 1.829 -8888 0.026 0.160 -8888 0.109 0.034 0.010 0.020 0.033 0.024 0.020 0.031 -8888 2.158 1.014 0.311 2.469 1.325 1.628 3.483 1.180 0.880 0.230 0.770 1.630 0.290 0.710 0.530 LO1.5 K3-S K3 1.500 0.0 20170918 00:00:00 29.4883 -93.4167 1.852 0.160 0.137 0.157 0.007 0.087 0.057 0.125 0.020 0.143 0.072 0.651 1.827 -8888 0.025 0.160 -8888 0.108 0.029 0.009 0.020 0.032 0.024 0.020 0.031 -8888 2.149 1.010 0.309 2.458 1.319 1.616 3.468 1.177 0.870 0.230 0.770 1.630 0.290 0.710 0.530 LO2 K4-S K4 1.500 0.0 20170918 00:00:00 29.3250 -93.4167 0.369 0.019 0.070 0.028 0.007 0.043 0.005 0.026 0.005 0.041 0.024 0.119 0.346 -8888 0.023 0.019 -8888 0.052 0.018 0.001 0.002 0.005 -8888 -8888 0.003 -8888 0.458 0.183 0.115 0.573 0.298 0.387 0.756 0.258 1.050 0.390 0.610 1.540 0.240 0.760 0.490 LO3 K5-S K5 1.500 0.0 20170918 00:00:00 29.1667 -93.4167 1.270 0.089 0.192 0.076 0.014 0.116 0.036 0.048 0.011 0.256 0.026 0.211 1.224 -8888 0.047 0.089 -8888 0.114 0.078 0.001 0.010 0.013 0.016 0.030 0.035 -8888 1.551 0.382 0.412 1.963 0.794 1.075 2.345 0.748 0.850 0.520 0.480 1.950 0.160 0.840 0.540 LO4 K8-S K8 1.500 0.0 20170918 00:00:00 28.7109 -93.4167 0.214 0.025 0.020 0.022 0.004 0.018 0.002 0.010 0.003 0.022 0.008 0.121 0.189 0.016 0.008 0.025 -8888 0.013 0.007 -8888 0.003 0.004 -8888 -8888 0.005 -8888 0.259 0.158 0.052 0.311 0.210 0.255 0.469 0.200 1.190 0.250 0.750 1.230 0.340 0.660 0.460 LO5 K9-S K9 1.500 0.0 20170918 00:00:00 28.4218 -93.4167 0.118 0.012 0.010 0.012 0.002 0.009 -8888 0.002 -8888 0.004 0.003 0.097 0.057 0.060 -8888 0.006 0.006 0.006 0.004 -8888 -8888 -8888 -8888 -8888 -8888 -8888 0.140 0.111 0.018 0.158 0.129 0.151 0.269 0.127 1.280 0.140 0.860 1.090 0.410 0.590 0.440 LO6 F7-S F7 1.285 0.0 20170919 00:00:00 28.4500 -91.6167 0.124 0.007 0.008 0.011 -8888 0.007 -8888 0.003 0.002 0.006 0.003 0.100 0.068 0.054 0.003 0.004 0.003 0.005 0.003 -8888 -8888 -8888 -8888 -8888 -8888 -8888 0.139 0.116 0.016 0.155 0.132 0.147 0.271 0.123 1.190 0.120 0.880 1.050 0.430 0.570 0.460 LO7 F5-S F5 1.500 0.0 20170919 00:00:00 28.6917 -91.6167 0.530 0.022 0.085 0.030 0.008 0.068 0.014 0.035 0.006 0.092 0.034 0.091 0.499 -8888 0.031 0.022 -8888 0.054 0.031 -8888 0.003 0.005 0.007 0.005 -8888 -8888 0.637 0.176 0.202 0.839 0.378 0.485 1.015 0.329 0.920 0.530 0.470 1.690 0.170 0.830 0.520 LO8 F3-S F3 1.500 0.0 20170919 00:00:00 28.8833 -91.6167 0.455 0.014 0.061 0.033 0.005 0.050 0.007 0.024 0.005 0.077 0.019 0.064 0.432 -8888 0.023 0.014 -8888 0.038 0.023 -8888 0.002 0.002 0.007 0.007 -8888 -8888 0.530 0.133 0.151 0.681 0.284 0.359 0.814 0.236 0.790 0.530 0.470 1.870 0.160 0.840 0.560 LO9 F1-S F1 1.500 0.0 20170919 00:00:00 29.1500 -91.6167 1.616 0.047 0.327 0.058 0.003 0.026 0.009 0.089 0.017 0.810 0.020 0.088 1.302 -8888 0.314 0.047 -8888 0.268 0.059 0.002 0.007 0.007 0.020 0.125 0.011 -8888 1.990 0.261 0.859 2.849 1.120 1.494 3.110 1.003 0.920 0.770 0.230 1.780 0.080 0.920 0.520 LO10 E6-S E6 1.500 0.0 20170920 00:00:00 28.3750 -91.2500 0.207 0.015 0.029 0.017 0.006 0.024 -8888 0.012 0.005 0.025 0.007 0.094 0.136 0.063 0.008 0.009 0.006 0.018 0.011 -8888 -8888 0.002 -8888 -8888 -8888 -8888 0.251 0.128 0.062 0.313 0.190 0.234 0.441 0.171 1.130 0.330 0.670 1.320 0.290 0.710 0.470 LO11 E4-S E4 1.500 0.0 20170920 00:00:00 28.5833 -91.2500 0.477 0.013 0.053 0.027 0.004 0.028 0.002 0.031 0.012 0.080 0.027 0.105 0.466 -8888 0.010 0.013 -8888 0.040 0.013 0.001 0.001 0.002 -8888 0.008 0.003 -8888 0.543 0.177 0.139 0.682 0.316 0.382 0.859 0.259 0.800 0.440 0.560 1.720 0.210 0.790 0.560 LO12 E2-S E2 1.000 0.0 20170920 00:00:00 28.8583 -91.2500 1.049 0.046 0.181 0.046 0.005 0.026 0.017 0.089 0.031 0.380 0.047 0.066 0.948 -8888 0.102 0.046 -8888 0.155 0.026 0.003 0.005 0.006 0.011 0.081 -8888 -8888 1.276 0.249 0.458 1.734 0.707 0.934 1.983 0.587 0.890 0.650 0.350 1.800 0.130 0.870 0.530 LO13 D5-S D5 1.500 0.0 20170920 00:00:00 28.5000 -90.8333 0.633 0.015 0.073 0.049 0.007 0.042 0.032 0.027 0.006 0.088 0.027 0.178 0.575 -8888 0.058 0.015 -8888 0.053 0.020 -8888 0.001 0.005 0.011 0.017 0.003 -8888 0.721 0.292 0.164 0.885 0.456 0.544 1.177 0.389 0.860 0.360 0.640 1.580 0.250 0.750 0.540 LO14 C10-S C10 1.650 0.0 20170920 00:00:00 28.6100 -90.2333 0.265 0.006 0.040 0.018 0.003 0.025 0.012 0.020 0.004 0.055 0.011 0.029 0.254 -8888 0.012 0.006 -8888 0.028 0.012 -8888 -8888 0.002 -8888 0.005 -8888 -8888 0.311 0.083 0.094 0.405 0.177 0.223 0.488 0.141 0.840 0.530 0.470 1.760 0.170 0.830 0.540 LO15 C6-S C6C 1.500 0.0 20170921 00:00:00 28.8655 -90.4956 0.308 0.015 0.053 0.014 -8888 0.018 0.003 0.023 0.003 0.097 0.025 0.015 0.291 -8888 0.017 0.015 -8888 0.039 0.014 -8888 -8888 0.002 0.006 0.018 0.002 -8888 0.376 0.058 0.142 0.518 0.200 0.268 0.576 0.175 0.870 0.710 0.290 1.880 0.100 0.900 0.530 LO16 C2-S C2 1.500 0.0 20170921 00:00:00 29.0200 -90.4645 0.608 0.045 0.097 0.038 -8888 0.017 0.006 0.037 0.005 0.188 0.022 0.055 0.565 -8888 0.043 0.045 -8888 0.070 0.027 0.001 0.006 0.006 0.015 0.077 0.007 -8888 0.750 0.141 0.230 0.980 0.371 0.513 1.121 0.336 0.840 0.620 0.380 2.020 0.130 0.870 0.540
I copied you the entire file. If that helps

Sign in to comment.

Categories

Find more on Language Support in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!