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

1 view (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

Stephen23
Stephen23 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' }

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!