Reading Text File In Matlab With Special Characters

365 views Asked by At

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.

1

There are 1 answers

0
atru On BEST ANSWER

This works for me

clear

% Names of input and output files
file_in = 'sample_data.txt';
xl_filename = 'output.xlsx';

% Number of columns (assumed fixed)
n_col = 6;

fIN = fopen(file_in,'r');

% Load each line of the file as cell, split by ',', convert into a table
% entry and add it to the table
tline = fgets(fIN);
res_table = cell2table(cell(0,n_col));
while (ischar(tline))
    res_table = [res_table ; cell2table(strsplit(tline,','))];
    tline = fgets(fIN);
end

% Change table headers and write to the excel file
res_table.Properties.VariableNames = {'C1' 'C2' 'C3' 'C4' 'C5' 'C6'}
writetable(res_table,xl_filename,'Sheet',1,'Range','A1');

fclose(fIN);

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 '-'.