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

6 views (last 30 days)
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?

Accepted Answer

Stephen
Stephen on 25 Nov 2021
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 Comment
Roberto Palmeggiani
Roberto Palmeggiani on 27 Nov 2021
Thank you so much! This is exactly what I was looking for

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!