From char to timetable

10 views (last 30 days)
Adriano
Adriano on 26 Feb 2019
Answered: Adriano on 26 Feb 2019
Hi everybody! I have a char variable of 1x446405. The structure of the variable is always the same like below:
2019-02-25 0.870134 -0.228498 -0.058666 -0.386660 0.083051 0.209336 0.327385 0.440973 0.551807 0.660402 0.766645
2019-02-24
2019-02-23
2019-02-22 0.870663 -0.221149 -0.048423 -0.384026 0.093777 0.219200 0.335723 0.447541 0.556605 0.663568 0.768380
2019-02-21 0.909650 -0.218755 -0.041825 -0.383403 0.105566 0.236380 0.358028 0.474450 0.587488 0.697766 0.805262
2019-02-20 0.874319 -0.220516 -0.047655 -0.383219 0.094916 0.220812 0.337824 0.450094 0.559548 0.666825 0.771874
2019-02-19 0.885250 -0.236768 -0.069948 -0.388208 0.072871 0.202418 0.324662 0.442630 0.557599 0.669865 0.779220
2019-02-18 0.877976 -0.250780 -0.088779 -0.394880 0.052791 0.183115 0.307173 0.427380 0.544648 0.659067 0.770328
2019-02-17
2019-02-16
2019-02-15 0.897203 -0.220365 -0.051245 -0.375429 0.092066 0.221087 0.342288 0.459007 0.572706 0.683782 0.792080
2019-02-14 0.864219 -0.224309 -0.052387 -0.386413 0.089209 0.214173 0.330341 0.441876 0.550706 0.657468 0.762102
2019-02-13 0.898676 -0.233793 -0.063130 -0.389390 0.082196 0.213365 0.336644 0.455262 0.570627 0.683122 0.792598
2019-02-12 0.913564 -0.205047 -0.021548 -0.379842 0.127451 0.257085 0.376145 0.489421 0.599253 0.706544 0.811406
2019-02-11 0.914752 -0.217810 -0.039717 -0.383609 0.108531 0.239985 0.362113 0.478895 0.592196 0.702663 0.810286
2019-02-10
I would like to get a timetable in which the rownames are the dates and the columns are the values. I would like also to eliminate the dates without values like 2019-02-24, 2019-02-23, 2019-02-17, 2019-02-16 and 2019-02-10. Many thanks!

Accepted Answer

Sean de Wolski
Sean de Wolski on 26 Feb 2019
How are you creating the char? Reading it from a file? If you're reading it from a file, then look at importing with fixed width. Attach the file for better help.
Otherwise, something along the lines of the following.
lines = splitlines(string(s))
Which should give you separate strings for each line. Then any line with
empty = strlength(line)<12
Has no data so remove them
lines = lines(~empty)
Now create the timetable by parsing the strings (i.e. create a datetime from the first few characters, and split the rest on white space and convert to double. Something along the lines of the following.
dates = datetime(extractBefore(lines,12))
values = double(split(extractAfter(lines,12)))
timetable(dates,values)

More Answers (1)

Adriano
Adriano on 26 Feb 2019
Simply great!!! The solution is what i wanted. I didn't know the splitlines function and it's very useful. Many thanks!!

Categories

Find more on Characters and Strings 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!