extract a number from text file
42 views (last 30 days)
Show older comments
MIHYUN
on 11 Jan 2014
Answered: Walter Roberson
on 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.
0 Comments
Accepted Answer
Azzi Abdelmalek
on 11 Jan 2014
Edited: Azzi Abdelmalek
on 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)
0 Comments
More Answers (2)
Mischa Kim
on 11 Jan 2014
Edited: Mischa Kim
on 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 .
0 Comments
Walter Roberson
on 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};
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!