フィルターのクリア

How to use sscanf to read data file with two delimiter

19 ビュー (過去 30 日間)
Rafael
Rafael 2011 年 2 月 15 日
コメント済み: David 2022 年 1 月 21 日
Hi, I am trying to read the following text (.txt) file using sscanf
YYYY-MM-DD HH:MM MOD UNMO PRESS TEM RH BATT
UTC /hr /hr mb C % V
2011-01-21 00:02 1608 1008 1001.2 12 98 11.7
2011-01-21 01:02 1602 1010 999.4 8 100 11.7
.
.
This is part of the script I am trying to run
fid1 = fopen('counts.txt');
% Skip first two rows
line = fgets(fid1);
line = fgets(fid1);
while ~feof(fid1)
line = fgets(fid1); %# read line by line
A = sscanf(line,'%i4,%i2,%i2,%i2,%i2,%i4,%i4,%f6.1,%i3,%i3 %f4.1')
.
.
It is only giving me 2011. I would like to know how to deal with the two delimiters (dash and colon) in each line.
Thank you
  1 件のコメント
David
David 2022 年 1 月 21 日
The sscanf documentation says:
Literal Text to Ignore
sscanf ignores specified text immediately before or after the conversion specifier.
Example: Level%u reads 'Level1' as 1.
Example: %uStep reads '2Step' as 2.
So maybe:
line = "2011-01-21 00:02 1608 1008 1001.2 12 98 11.7"
A = sscanf(line,'%i-%i-%i %i:%i %i %i %f %i %i %f');

サインインしてコメントする。

回答 (1 件)

Oleg Komarov
Oleg Komarov 2011 年 2 月 15 日
I suggest the following approach:
fid = fopen('C:\Users\Oleg\Desktop\trial.txt');
data = textscan(fid,'%s%s%f%f%f%f%f%f','HeaderLines',2,'MultipleDelimsAsOne',true);
fid = fclose(fid);
% Convert dates
[y,m,d,HH,MM] = datevec(strcat(char(data{1}),'-',char(data{2})),'yyyy-mm-dd-HH:MM');
data = [y m d HH MM data{:,3:end}]
data =
2011 1 21 0 2 1608 1008 1001.2 12 98 11.7
2011 1 21 1 2 1602 1010 999.4 8 100 11.7
Oleg

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by