How to load data from and .txt file?

17 views (last 30 days)
Benjamin Watson
Benjamin Watson on 15 Apr 2016
Answered: parham kianian on 2 Sep 2020
Hi All,
Ive been trying to load data from the .txt file that I've attached. What I've got so far is:
Spill = importdata('Spill.txt');
T = Spill(:,1);
C = Spill(:,2);
but I get an error "Index exceeds Matrix Dimensions". Can some please help me out. I can't change to ".data" file either as I have to extract the columns of data directly out of the .txt file. Much appreciated in advance.

Answers (2)

J. Webster
J. Webster on 15 Apr 2016
Edited: J. Webster on 15 Apr 2016
To read a tab delimited ascii file while skipping the first row, try:
data = dlmread('Spill.txt','\t',1,0);
  2 Comments
Walter Roberson
Walter Roberson on 15 Apr 2016
It depends on that MATLAB release you are using. Text anywhere in a file is not supported in R2014b or earlier, and possibly not in R2015a either (we have not gone back to figure out exactly when the change was made.)
Walter Roberson
Walter Roberson on 15 Apr 2016
fid = fopen('Spill.txt', 'rt');
data = cell2mat( textscan(fid, '%f%f', 'Delimiter', '\t', 'HeaderLines', 1) );
fclose(fid)

Sign in to comment.


parham kianian
parham kianian on 2 Sep 2020
I think it is much easier to use textread function. Following link describe in full detail how to use this function to import data from a text file:

Categories

Find more on Data Import and Export 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!