How to export tab delimitted data from a txt file into MATLAB for analysis?
1 view (last 30 days)
Show older comments
I am currently trying to import some tab delimitted data from a txt file into MATLAB R2018a.
The data has 11900 lines of values.
The data from the txt file looks like this (not exact data just a description):
_____________________________________________________________________
Title of 1st column Title of 2nd column
Data from 1st column Data from 2nd column
_____________________________________________________________________
I would like to read this data and sort it to the correesponding columns and then plot it.
The code that I am using is as follows:
D = importdata('File_Name'.txt','\t',1); % read tab delimited data with 1 header line
Col1 = D.data(:,1) % extract the first column into the array Col1
Col2 = D.data(:,2) % extract the second column into the array Col2
_____________________________________________________________________
This code only seems to read the first column and not the second, while the title of the column also is not displayed.
I would like to know how I may read both columns of data along with displaying the corresponding column titles.
Any form of help would be appreciated. Thank you in advance.
0 Comments
Answers (1)
Walter Roberson
on 3 Nov 2019
D = readtable('File_Name.txt', 'headerlines', 2, 'readvariablenames', 0);
Col1 = D{:, 1};
Col2 = D{:, 2};
It is possible and not difficult to use textscan instead of readtable.
4 Comments
Walter Roberson
on 4 Nov 2019
When I look back at your original description, I see that possibly you have only one headerline. It is not clear from your description whether the header line is in the form of reasonable MATLAB variable names; if it is, then possibly just readtable('Data.txt') would be enough.
See Also
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!