Import text file with header

Hi, I have a text file with header, I need to import the file to matlab and to use numeric data. The numeric data in a line is separated by a ",". The file is as following Thanks Adelynne
base shear
Analysis Series = cyclic2 (cyclic2)
Load Case = [2] = [1] + cyclic
0,-2.980232E-08
0.1,-24.82325
0.2,-87.0501
0.3,-75.9906
0.4,86.35868
0.5,281.4871
0.6,417.2569
0.7,521.8102
0.8000001,622.5663
0.9,711.8201
1,785.7328
1.1,838.6344
1.2,863.1584
1.3,857.4628
1.4,817.3618
1.5,700.1636

 Respuesta aceptada

Jing
Jing el 26 de Feb. de 2013

1 voto

The functions that would be most helpful are 'fopen' and 'textscan'.
fid=fopen('yourtext.txt');
cdata=textscan(fid,'%f %f','delimiter',',');
fclose(fid);

5 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 26 de Feb. de 2013
you should run fgetl three times
fid=fopen('file.txt');
fgetl(fid);fgetl(fid);fgetl(fid);
cdata=textscan(fid,'%f %f','delimiter',',')
fclose(fid);
Jing
Jing el 26 de Feb. de 2013
just use textscan, and tell it you have how many header lines using HeaderLines options. Read Isakson's answer below...
Adelyn Luu
Adelyn Luu el 26 de Feb. de 2013
thank guys, but according to suggestion from Jing, I will get cdata with the numeric data. However, how can I use the numer from cdata, for exemple, plot a graph between cdata(1,1) and cdata(1,2)?
Jing
Jing el 26 de Feb. de 2013
just use textscan, and tell it you have how many header lines using HeaderLines options. Read Isakson's answer below...
per isakson
per isakson el 26 de Feb. de 2013
Editada: per isakson el 26 de Feb. de 2013
cdata is a cell array. Read on cell array in the on-line help. And try
plot( cdata{1}, cdata{2} )

Iniciar sesión para comentar.

Más respuestas (4)

per isakson
per isakson el 26 de Feb. de 2013

4 votos

... but don't forget the header
fid=fopen('yourtext.txt');
cdata=textscan(fid,'%f%f','delimiter',',', 'HeaderLines', 3 );
fclose(fid);
Azzi Abdelmalek
Azzi Abdelmalek el 26 de Feb. de 2013

1 voto

fid = fopen('file.txt');
line1 = fgetl(fid);
res={line1};
while ischar(line1)
line1 = fgetl(fid);
res{end+1} =line1
end
fclose(fid);
res(end)=[]
data=res(4:end)
a=cellfun(@(x) regexp(x,',','split'),data,'un',0)
x1=str2num(char(cellfun(@(x) x{1},a,'un',0)'))
x2=str2num(char(cellfun(@(x) x{2},a,'un',0)'))
Muthu Annamalai
Muthu Annamalai el 26 de Feb. de 2013

0 votos

z=textread('filename.dat','%s','delimiter','\n')
z=z(4:end)
q=regexp(z,',','split')
data = reshape(str2double([q{:}]),2,16)'
Miroslav Balda
Miroslav Balda el 26 de Feb. de 2013

0 votos

The simplest way to read such data is in using the function txt2mat fro the File Exchange:
www.mathworks.com/matlabcentral/fileexchange/18430
Should the name of your data be Adely.txt , the following statement does the task:
A = txt2mat('Adelyn.txt','InfoLevel',0);

Preguntada:

el 26 de Feb. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by