How to load data from and .txt file?

조회 수: 20 (최근 30일)
Benjamin Watson
Benjamin Watson 2016년 4월 15일
답변: parham kianian 2020년 9월 2일
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.

답변 (2개)

J. Webster
J. Webster 2016년 4월 15일
편집: J. Webster 2016년 4월 15일
To read a tab delimited ascii file while skipping the first row, try:
data = dlmread('Spill.txt','\t',1,0);
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 4월 15일
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 2016년 4월 15일
fid = fopen('Spill.txt', 'rt');
data = cell2mat( textscan(fid, '%f%f', 'Delimiter', '\t', 'HeaderLines', 1) );
fclose(fid)

댓글을 달려면 로그인하십시오.


parham kianian
parham kianian 2020년 9월 2일
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:

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by