Info

This question is closed. Reopen it to edit or answer.

rearranging textfile that contains commas

3 views (last 30 days)
Kafayat Olayinka
Kafayat Olayinka on 11 Mar 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I have a textfile that looks like the following. The text breaks at the end of the 25th typed space
date,time,lat,long,alt
itude,pressure
03/10/2019,12:00,23,-6
0,1000,955
03/10/2019,13:00,23,-6
3,980,900
03/10/2019,14:00,23,-6
3,600,850
03/10/2019,15:00,23,-6
3,800,950
03/10/2019,16:00,23,-6
0,500,750
part of variable "altitude" breaks into "altitu"./n "de"
also part of variable longitude data breaks into "-6" ./n "3".
how can i write a code that put longitude date together as "-63"?
how can i write the delimiter for this problem?
thanks

Answers (1)

GT
GT on 11 Mar 2019
There is probably an easier way of doing this... but this should work:
a = fileread('example.txt'); % txt file that you shared
b = double(a);
c = b==10; % \n in ASCII code, it could be that you need to do the same for \r\n (depending on your OS)
d = cumsum(c);
e = mod(d,2);
b(c&e) = []; % remove every second \n
f = char(b); % string now clean
formatSpec = '%{dd/MM/uuuu}D%{hh:mm}D%f%f%f%f';
delimiter = ',';
dataArray = textscan(f, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,1, 'ReturnOnError', false, 'EndOfLine', '\n');

Community Treasure Hunt

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

Start Hunting!