Filter löschen
Filter löschen

extract a number from text file

23 Ansichten (letzte 30 Tage)
MIHYUN
MIHYUN am 11 Jan. 2014
Beantwortet: Walter Roberson am 11 Jan. 2014
There are contents of a text file , which take the following form.
---------
# unique survivor object: RWL
# total count of objects: 4
# COIC lon[deg],lat[deg]: 30.831 43.061
# COIC epoch & time[UTC]: 2004/01/01 00:10:56.918
#
# __central_track__ __position_wrt_COIC__ ___P[-]/ds[km]_of_1D_swath_with_PD=fct(lat)___ _P[-]/ds[km]_of_2D_swath_with_PD=fct(lon,lat)_
# lon[deg] lat[deg] __ds[km]_ __dt[sec]_ ___P.imp__ ___Pc.min__ ___Pc.avg__
43.040 30.926 8.110 -39.030 1.200E-01 1.367E-06 1.367E-06
43.041 30.923 8.020 -38.957 1.739E-03 1.982E-08 1.982E-08
43.063 30.818 -1.060 -0.194 4.659E-01 5.308E-06 5.308E-06
I want to save as a text file by extracting only the numbers.(maintain number format) So , I do not know what you want to save by using the fscanf function. I will appreciate to tell me what I should do. Thanks in advance.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 11 Jan. 2014
Bearbeitet: Azzi Abdelmalek am 11 Jan. 2014
FID = fopen('file.txt');
form='%f %f %f %f %f %f %f'; % we have 7 columns, then use 7 %f
n = 7; % numbers of lines representing a text
out = textscan(FID, form,'headerlines', n)
fclose(FID)
cell2mat(out)

Weitere Antworten (2)

Mischa Kim
Mischa Kim am 11 Jan. 2014
Bearbeitet: Mischa Kim am 11 Jan. 2014
I would probably use the importdata function:
filename = 'myData.txt';
delimiterIn = ' ';
headerlinesIn = 7;
A = importdata(filename,delimiterIn,headerlinesIn);
A.data
ans =
43.0400 30.9260 8.1100 -39.0300 0.1200 0.0000 0.0000
43.0410 30.9230 8.0200 -38.9570 0.0017 0.0000 0.0000
43.0630 30.8180 -1.0600 -0.1940 0.4659 0.0000 0.0000
Once you have the data you can save them again in a text file, e.g. fprintf .

Walter Roberson
Walter Roberson am 11 Jan. 2014
Are the "4" and "30.831" and so on to be extracted? Or only the lon, lat, and so on?
headerlines = 7;
numfields = 7; %the same only by chance
fmt = repmat('%f', 1, numfields);
fid = fopen('YourFile.txt', 'r');
datacell = textscan(fid, fmt, 'HeaderLines', headerlines, 'CollectOutput', 1);
fclose(fid);
dataarray = datacell{1};

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