How can I import a file.txt and extract data?

1 回表示 (過去 30 日間)
Roberto Palmeggiani
Roberto Palmeggiani 2021 年 11 月 25 日
コメント済み: Roberto Palmeggiani 2021 年 11 月 27 日
I want to create a function.
The function has to read the file and create a matrix: the first column must contain the first column of numbers (the numbers after HIP). In the second and third coulmn i need the numbers into the square parenthesis (they are coordinates).
How can I do?

採用された回答

Stephen23
Stephen23 2021 年 11 月 25 日
opt = {'CollectOutput',true};
[fid,msg] = fopen('prova2.txt');
assert(fid>=3,'%s',msg)
hdr = fgetl(fid);
out = textscan(fid,'HIP%d[%d,%d]%[^\n\r]',opt{:});
fclose(fid);
out{:}
ans = 10×3
112971 449 10 146738 485 52 1189928 478 117 116671 414 111 114368 396 192 16453 378 279 49064 386 451 700734 416 564 53644 686 468 15624 657 267
ans = 10×1 cell array
{'Gamma Piscium' } {'Kappa Piscium' } {'Lambda Piscium' } {'Iota Piscium' } {'Omega Piscium' } {'41 Piscium' } {'Epsilon Piscium'} {'Mu Piscium' } {'Eta Ceti' } {'Iota Ceti' }
  1 件のコメント
Roberto Palmeggiani
Roberto Palmeggiani 2021 年 11 月 27 日
Thank you so much! This is exactly what I was looking for

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by