Importing table from text file and matching header to column
4 views (last 30 days)
Show older comments
Hi everyone,
Here is an example of my data:
# r CAC-CAC CAC-OAC
0.2700000E+01 0.0000000E+00 0.0000000E+00 0.3860216E-03 0.2814651E-01
0.2730000E+01 0.0000000E+00 0.0000000E+00 0.3775852E-03 0.2753156E-01
0.2760000E+01 0.0000000E+00 0.0000000E+00 0.5541359E-02 0.1041859E+00
0.2790000E+01 0.0000000E+00 0.0000000E+00 0.7230470E-02 0.1176749E+00
0.2820000E+01 0.0000000E+00 0.0000000E+00 0.1096994E-01 0.1432550E+00
0.2850000E+01 0.0000000E+00 0.0000000E+00 0.1870899E-01 0.1847078E+00
I would like to import my data into a table, with the text in the first row used as a variable for each column that it lines up with. The unwanted "#" character and the inconsistent spacing at the start of the first row means that readtable(example_data.txt); does not does not produce the correct headers for the table and instead leads to Var1, Var2,..., ect.
For example, in Excel the import data option slices up the data and the headers end up in the correct location in the imported table:
Note that some of the columns do not have headers and I do not require this data in my imported table.
I am very new to MATLAB, so any help is very appreciated!
Cheers!
0 Comments
Accepted Answer
AndresVar
on 20 Mar 2022
Edited: AndresVar
on 20 Mar 2022
I tried the import tool: Import data from file - MATLAB (mathworks.com)
The code it generated for your data did not look as pretty as it usually does, but seems to do the job.
Edit: noticed the import tool detected 'fixedwidth' filetype, but readtable seems to detect 'delimitedtext'. So you can specify fixedwidth in the options. The variablenamingrule option doesn't seem necessary but there is a warning without it.
opts = detectImportOptions('example_data.txt',FileType='fixedwidth',VariableNamingRule='preserve');
t1 = readtable("example_data.txt",opts)
t1.("#")=[] % remove the # column
6 Comments
AndresVar
on 20 Mar 2022
Edited: AndresVar
on 20 Mar 2022
@Lucas Wong there is an extra space at the end of the first row that messes it up. BUT looks like just specificying VariableNamesLine=1 sorta solves the problem.
opts = detectImportOptions('example_data2.txt', ...
FileType='fixedwidth',VariableNamingRule='preserve', ...
VariableNamesLine=1 );
% optional: sanitize the variable names by removing white spaces and
% replacing unwanted characters.
opts.VariableNames = strtrim(strrep(strrep(opts.VariableNames,...
'-','_'), '#',''));
t1 = readtable("example_data2.txt",opts)
figure;
plot(t1.r,t1.CAC_CAC);
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!