Read txt file with no header

2 views (last 30 days)
Iacopo
Iacopo on 22 Apr 2022
Edited: Les Beckham on 22 Apr 2022
Hi all, I have attached one of the txt I need to extract only the numeric data from, skipping thr headers. it works for that file but I want to extens the code to files with a different (unknown) number of columns. Can anyone help me to modify it?
The piece of code I used is:
fid=fopen('text.txt');
data=textscan(fid,'%f %f %f %f %f %f %f %f %f %f','HeaderLines','4');
data=cell2mat(data);
fclose(fid);
Thanks

Accepted Answer

Les Beckham
Les Beckham on 22 Apr 2022
Edited: Les Beckham on 22 Apr 2022
This should work for any number of columns and it automatically detects the number of header rows and the variable names.
t = readtable('text.txt')
t = 117×10 table
Time ACC_1 ACC_2 ACC_3 ACC_4 ACC_5 ACC_6 ACC_7 ACC_8 ACC_9 _____ _________ _________ _________ ________ _________ _________ _________ _________ _________ 0 3.2e-05 -0.000295 -0.000697 0.000717 -8.5e-05 -0.000843 0.000929 0.00148 -0.006464 0.001 -0.000753 0.000458 0.001528 0.000549 -0.000275 -0.000939 -5.2e-05 0.002073 -0.002605 0.002 -0.000595 0.000573 -0.000489 0.000726 -0.000119 -0.00064 -0.00026 0.001742 -0.003561 0.003 4.3e-05 -0.000719 0.001813 0.000657 -0.000238 -0.000428 -0.001735 0.001223 -0.004032 0.004 -0.000225 0.001419 0.003651 0.000727 -0.000119 0.00025 -0.00042 0.001535 -0.001738 0.005 -0.00073 -0.000429 0.000806 0.000678 -0.000298 0.000347 0.000873 0.0005 -0.001163 0.006 -0.000213 0.000734 -0.002273 0.000648 -0.000196 9.1e-05 -0.000439 0.001612 -6e-06 0.007 -0.000604 0.000529 -0.001316 0.000861 -0.00017 -0.000177 -0.000728 0.001672 -0.001878 0.008 -0.000435 -0.000243 -0.001504 0.000382 -0.000244 2.5e-05 -0.002853 0.000286 -0.001026 0.009 0.000431 0.000914 0.003813 0.000468 -0.000298 -0.000616 -0.00167 -0.000636 0.001024 0.01 -0.000783 -0.000246 0.001083 0.000518 -0.000137 -0.000794 -0.001339 -0.000854 0.002666 0.011 0.000343 0.000399 -0.004288 0.000282 0.000135 -0.000301 -0.001061 -0.000619 0.001085 0.012 -0.00032 0.000461 -0.003572 0.000316 -4.9e-05 0.000245 -0.001238 0.000664 0.001116 0.013 0.000325 0.000451 -0.002961 0.000287 -0.00013 0.000175 0.000807 0.002159 0.00257 0.014 0.000374 0.000273 0.00214 0.000309 8.1e-05 0.000358 0.000328 0.002289 0.003796 0.015 0.000417 0.000755 0.003267 0.000255 8.3e-05 0.001191 0.000446 0.000832 0.003408
data = table2array(t); % convert to a plain numeric array
whos data
Name Size Bytes Class Attributes data 117x10 9360 double

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!