Info

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

How to read a text file where the data are merged to the first headline title

1 view (last 30 days)
Hi
I am trying to read a text file where where the first data are merged to the headline title, and this title varies between the data. The data start from 1 right after D2O, but title does not always end with D2O.
You can see that the data should be in 4 different columns too
I am looking forward to your help
Thanks in advance :)
  3 Comments
Jeremy Hughes
Jeremy Hughes on 13 Nov 2020
Is that "D","201" - or - "D20","1" ... or something else. There's no easy way to break that up programatically.
There are also two other "1" characters on that first line.
I think this is a difficult file to read generically. You may have to modify the file (and I really hate suggesting anyone modify files) and try some of the standard tools like readtable.
Where does the file come from? If you can fix the source of the file that would be easier maybe?
Dat Pham
Dat Pham on 16 Nov 2020
Hi,
Thank you very much guys! Sorry for late reply as I got a busy weekend.
I have not got readmatrix or readtable function in my matlab yet. I am trying to download them to try
I attach the file here as well
The title ends with D2O (O is letter O, not number 0) and then number 1 appears next to it. Number 1 should be in the next line but unfortunately it appears in the same line as the title :(
Yes I hate to modify the file or import the data manually, thats why I am here asking for your kind help :)

Answers (1)

Cris LaPierre
Cris LaPierre on 13 Nov 2020
Random thought to try. If this is always the format (in particular, the first column is just row number), then try readmatrix or readtable. They will not be able to interpret the very first value, but if that is always 1, you can just add that later.
I made up a dummy data file based on what you showed.
d = readmatrix("ascii-spec.txt")
d = 5×4
NaN -1.4567 0.5193 0.0010 0.0002 -1.4567 0.5193 0.0010 0.0003 -1.4567 0.5193 0.0010 0.0004 -1.4567 0.5193 0.0010 0.0005 -1.4567 0.5193 0.0010
d(1,1) = 1
d = 5×4
0.0001 -1.4567 0.5193 0.0010 0.0002 -1.4567 0.5193 0.0010 0.0003 -1.4567 0.5193 0.0010 0.0004 -1.4567 0.5193 0.0010 0.0005 -1.4567 0.5193 0.0010
%% Now with readtable
d2 = readtable("ascii-spec.txt")
d2 = 5x4 table
Var1 Var2 Var3 Var4 ____ ______ ______ ______ NaN -14567 5193.1 10.383 2 -14567 5193.1 10.383 3 -14567 5193.1 10.383 4 -14567 5193.1 10.383 5 -14567 5193.1 10.383
d2.Var1(1) = 1
d2 = 5x4 table
Var1 Var2 Var3 Var4 ____ ______ ______ ______ 1 -14567 5193.1 10.383 2 -14567 5193.1 10.383 3 -14567 5193.1 10.383 4 -14567 5193.1 10.383 5 -14567 5193.1 10.383

Community Treasure Hunt

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

Start Hunting!