how to scan text

14 ビュー (過去 30 日間)
Kobi
Kobi 2014 年 12 月 2 日
コメント済み: Star Strider 2014 年 12 月 2 日
on those files:
on the 5th line there [are these] data:
# Number of frequencies : 8 (2, 4, 6, 8, 10, 12, 14, 16 GHz)
# Number of frequencies : 4 (4, 8, 12, 16 GHz)
i need to create a vector with those values i tried to locate the number 8 or 4 using textscan and to make a for loop n times to get those integers, but it didn't work, maybe there is something with the (,)
any suggestions?

採用された回答

Star Strider
Star Strider 2014 年 12 月 2 日
The frequencies are integers in the third column. This code works for the first file and should work for both:
fidi = fopen('rectTE_8f.exp');
d = textscan(fidi, '%d%d%d%f%f%f%f', 'Headerlines',10, 'Delimiter','\n','CollectOutput',1);
frqs = d{1}(:,3);
The ‘frqs’ variable is your vector of frequencies.
If you want a vector of only the frequencies on the 5th line, you can get them easily enough with:
ufrqs = unique(frqs);
It will give you a vector of the unique frequencies in the vector.
  2 件のコメント
Kobi
Kobi 2014 年 12 月 2 日
thank you Star Strider, that was very helpful.
Star Strider
Star Strider 2014 年 12 月 2 日
My pleasure!

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

その他の回答 (2 件)

Guillaume
Guillaume 2014 年 12 月 2 日
You have to use low level io functions if you want to parse the header:
fid = fopen('rectTE_8f.exp', 'rt');
for l = 1:4
fgetl(fid); %skip first four lines
end
numfreq = fscanf(fid, '# Number of frequencies : %d (');
frequencies = fscanf(fid, '%d, ', [1 numfreq]);
fgetl(fid); %get rest of line
for l = 6:10
fgetl(fid); %skip rest of header
end
arr = textscan(fid, ...); %get rest of file with textscan
fclose(fid);

dpb
dpb 2014 年 12 月 2 日
When you get to that line, either by headerlines or previously parsing if you need some of those data, then
n=cell2mat(textscan(fid,'# Number of frequencies : %d (')); % read the number of freq's
Now you can build a format string using that value as--
fmt=['(' repmat('%d,',1,n-1) '%d GHz)'];
f=cell2mat(textscan(fid,fmt));

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by