import txt file into matlab

4 ビュー (過去 30 日間)
zefeng yu
zefeng yu 2017 年 1 月 14 日
コメント済み: Star Strider 2017 年 1 月 15 日
Hello, everyone.
I am trying to import txt file into matlab. The txt file is generated from another software called SRIM. It contains a lot of texts in the first several paragraphs. What I want is the bottom three columns of data, namingly "target depth", "vacancies by ions", and "vacancies by recoils". Could anyone help me to obtain those data and import into matlab as a matrix? Thanks

採用された回答

Star Strider
Star Strider 2017 年 1 月 15 日
If all the files are the same format, and if you do not mind manually counting the 28 header lines in this one, this works:
fidi = fopen('VACANCY.txt','rt');
Dc = textscan(fidi, '%f%f%f', 'HeaderLines',29, 'CollectOutput',true, 'EndOfLine','\r\n');
fclose(fidi);
D = cell2mat(Dc); % ‘D’ Is A (100x3) Matrix Of Data
  2 件のコメント
zefeng yu
zefeng yu 2017 年 1 月 15 日
it works well! Thank you so much!
Star Strider
Star Strider 2017 年 1 月 15 日
My pleasure!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 1 月 14 日
Well having "header" lines both before and after certainly makes it harder to use canned routines like importdata. You can use fgetl() to look for lines that indicate exactly what line the data you want start and stops at.
clc;
% Open the file.
fid = fopen('VACANCY.TXT');
% Search for and skip past the line '----------- ----------- ------------'
textLine = fgetl(fid);
while ischar(textLine)
disp(textLine) % OPTIONAL: Echo to command window.
textLine = fgetl(fid);
if strcmp(textLine, '----------- ----------- ------------')
break;
end
end
% Now we're into the data, but bail out if the line is a blank line (short).
row = 1;
while ischar(textLine)
disp(textLine) % OPTIONAL: Echo to command window.
textLine = fgetl(fid);
if length(textLine) < 2
% Found the blank line so bail out.
break;
end
% Extract the 3 numbers from it.
data(row, 1:3) = sscanf(textLine, '%f ');
row = row + 1;
end
% Close the file.
fclose(fid);
  1 件のコメント
zefeng yu
zefeng yu 2017 年 1 月 15 日
thank you so much for your time! It worked beyond my expectation. I only need the data though. I don't need the headlines, but it is awesome that you show me how to get it. Thanks!

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

カテゴリ

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