Reading an Array in scientific notation from a txt

26 Ansichten (letzte 30 Tage)
Ignacio
Ignacio am 27 Mai 2015
Kommentiert: Ignacio am 27 Mai 2015
Hi, I am trying to read the data from a txt file but I have been so far unable to manage to extract it using fscanf or textread.
the file is formatted as follows:
"Title1"
"Title2"
1.00000e-03 4.36579e-02
2.00000e-03 -8.85146e-02
3.00000e-03 -8.97553e-02
4.00000e-03 -7.65414e-02
5.00000e-03 -5.80485e-02
6.00000e-03 -4.28242e-02
7.00000e-03 -2.93374e-02
I am unable to read the numbers from the same line separately, and interpret them as numbers. Any help? Thanks!

Akzeptierte Antwort

dpb
dpb am 27 Mai 2015
d=textread('yourfile.dat','headerlines',2);
While textread has been deprecated to 'red-haired stepchild' status, it's very useful for some purposes such as this. Alternatively,
d=cell2mat(textscan(fid,'%f %f','collectoutput',1, ...
'headerlines',2));
textscan takes an additional fopen to get a valid file handle and then a cell2mat to cast to an ordinary array as it will return only a cell; hence the comment above that textread still has advantages/uses in some instances.
An explicit E or decimal will override the '%f' or you can use '%e' if you really, really want. You don't need to match any specific field width on input, the i/o scanning routines will deal with what they find as long as it's numeric and delimited field.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by