I have a raw data file in *.txt
that I want to read using Matlab and convert to excel with appropriate headers.
Raw Data
0.050265,202241546530,50397417,0-127,128-255,1300812
0.051295,202245273937,65971821,0-127,128-255,1300812
0.050893,202248987612,65466246,0-127,128-255,1300812
0.050906,202252718742,65606097,0-127,128-255,1295785
0.050924,202256444380,65667670,0-127,128-255,1295785
For above data, csvread()
works but puts the data in multiple line as follows:
0.0502650000000000 202241546530.000 50397417 0 -127 128
-255 1300812 0 0 0 0
0.0512950000000000 202245273937.000 65971821 0 -127 128
-255 1300812 0 0 0 0
0.0508930000000000 202248987612.000 65466246 0 -127 128
-255 1300812 0 0 0 0
0.0509060000000000 202252718742.000 65606097 0 -127 128
-255 1295785 0 0 0 0
0.0509240000000000 202256444380.000 65667670 0 -127 128
-255 1295785 0 0 0 0
Desired Data Format After Importing
C1 C2 C3 C4 C5 C6
0.0502650000000000 202241546530.000 50397417 0-127 128-255 1300812
0.0512950000000000 202245273937.000 65971821 0-127 128-255 1300812
0.0508930000000000 202248987612.000 65466246 0-127 128-255 1300812
0.0509060000000000 202252718742.000 65606097 0-127 128-255 1295785
0.0509240000000000 202256444380.000 65667670 0-127 128-255 1295785
I can handle how to add specific headers, but the data is coming into multiple lines, I think due to the special character -
.
Can anyone please suggest better way to read this data into Matlab line by line? Thanks.
This works for me
It reads the file line at a time and converts the resulting cell array into a table entry. Since it splits on a ',' the '-' remain intact.
writetable
writes the table into excel spreadsheet, Sheet 1 starting with position A1. You can start with different positions and sheet numbers. There are also other potentially useful options in that function.Downside of this solution is that you need to explicitly convert the spreadsheet to numeric values, which is, on the other hand, pretty much a one selection - one click process. Converting to numbers directly in MATLAB is not straightforward, as far as I know, because part of your entries have the '-'.