How to export tab delimitted data from a txt file into MATLAB for analysis?

2 vues (au cours des 30 derniers jours)
JoBrr
JoBrr le 3 Nov 2019
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.

Réponses (1)

Walter Roberson
Walter Roberson le 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 commentaires
JoBrr
JoBrr le 4 Nov 2019
There wasn't any error messages. But anyway have managed to do it now with another code used in this video: https://www.youtube.com/watch?v=sRzSiA946ng
Thanks for your help @Walter Roberson
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Use COM Objects in MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by