extract a number from text file
17 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Azzi Abdelmalek
2014-1-11
编辑:Azzi Abdelmalek
2014-1-11
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 个评论
更多回答(2 个)
Mischa Kim
2014-1-11
编辑:Mischa Kim
2014-1-11
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 个评论
Walter Roberson
2014-1-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!