Reading in Numerical Data from a Text File

35 views (last 30 days)
Will
Will on 14 Jan 2026 at 20:55
Answered: Star Strider on 14 Jan 2026 at 22:09
I want to read in the numerical data from the data file to plot, while excluding the first 4 lines of text. I've tried using textscan for this, but haven't got it working yet. Any help would be appreciated.

Answers (2)

dpb
dpb about 3 hours ago
Edited: dpb about 3 hours ago
d=dir('*.txt'); % get long name easily
l=readlines(d.name); % read as text so
[l(1:10); l(end-9:end)] % can see what file contains
ans = 20×1 string array
"<Trace>" "276" "4-21_DCSF-eAmide_1:1HBSS/ACN_2uM" "X→Y" "250→667.7352" "252→1029.69983" "254→1376.47217" "256→3143.10059" "258→977.5746" "260→3024.39966" "786→4749.62646" "788→11072.4756" "790→24383.9629" "792→25446.3242" "794→-8370.253" "796→27464.5859" "798→28683.4668" "800→30009.2988" "</Trace>" ""
tData=readtable(d.name,'numheaderlines',3,'readvariablenames',1); % it's got variable names after three header lines
head(tData) % show beginning of what we got
X Y ___ ______ 250 667.74 252 1029.7 254 1376.5 256 3143.1 258 977.57 260 3024.4 262 944.97 264 1587
plot(tData.X,tData.Y) % plot it...
Alternatively, if don't want to use the table,
XY=readmatrix(d.name,'numheaderlines',4); % 2D array, no variable names
plot(XY(:,1),XY(:,2)) % plot the columns of the array
There appears to be one (or maybe a couple) really big outlier.
ixBig=find(tData.Y>1E6)
ixBig = 114
tData.Y(ixBig)=nan; % just ignore it for now...could interpolate
plot(tData.X,tData.Y) % plot again w/o the bum point

Star Strider
Star Strider about 2 hours ago
You can certainly use textscan for this.
I cannot determine the reason you are having probnlems getting textscan to run, since you did not post your code.
Try something like this --
filename = '4-21 »» Derive...S_ACN_2uM.txt';
type(filename) % File Content Information
<Trace> 276 4-21_DCSF-eAmide_1:1HBSS/ACN_2uM X Y 250 667.7352 252 1029.69983 254 1376.47217 256 3143.10059 258 977.5746 260 3024.39966 262 944.9659 264 1587.00012 266 579.456 268 1510.88428 270 1777.805 272 2627.87085 274 1690.26379 276 1930.78979 278 1326.50586 280 1823.25977 282 210.015137 284 458.315033 286 1192.583 288 1649.50024 290 1138.03015 292 185.725525 294 403.5759 296 1250.90894 298 779.468445 300 1139.5 302 527.1588 304 1400.3623 306 312.754028 308 1135.568 310 1427.15662 312 1072.42151 314 1040.76135 316 2191.449 318 260.106659 320 815.5458 322 -27.3571434 324 643.6141 326 884.485962 328 610.035767 330 346.10434 332 217.993729 334 334.9134 336 1033.8645 338 442.663574 340 778.2099 342 426.656616 344 749.9753 346 741.264465 348 192.113281 350 711.0884 352 184.3739 354 383.726868 356 775.0547 358 1344.949 360 748.636536 362 75.88888 364 -112.084534 366 1087.26843 368 526.1644 370 71.27033 372 158.6259 374 69.6336746 376 154.279785 378 577.7873 380 403.974243 382 150.687653 384 230.025513 386 -16.272253 388 -179.10434 390 308.0072 392 305.904633 394 1099.72473 396 698.83905 398 772.109558 400 62.6478 402 218.65242 404 -93.07676 406 217.3686 408 139.6278 410 528.736938 412 452.241821 414 451.822144 416 140.269089 418 535.254456 420 141.7861 422 1245.95593 424 1019.03455 426 -15.983264 428 -16.0397072 430 949.988831 432 471.315765 434 1530.44775 436 1052.04285 438 479.9209 440 316.003754 442 654.9701 444 1249.292 446 1178.01965 448 1187.65625 450 678.294556 452 1906.76245 454 1043.33716 456 2216.2854 458 1340.279 460 2272.49365 462 2295.09961 464 4104.083 466 4401.83936 468 3793.27026 470 3166.954 472 3491.97949 474 22240.502 476 2455524.75 478 6781.46338 480 3921.29541 482 2836.48 484 4088.76758 486 4043.87573 488 3774.782 490 5423.43652 492 10848.5488 494 11112.7637 496 19274.13 498 29915.9121 500 34331.8047 502 44327.85 504 61904.832 506 87353.04 508 113932.945 510 132665.844 512 160629.063 514 185820.109 516 229366.453 518 277049.9 520 312199.8 522 363417.6 524 412337.156 526 463137.8 528 511075.344 530 555634.8 532 623847.3 534 651911.6 536 677319.25 538 676633.8 540 678642 542 673484 544 657768.9 546 674430.063 548 646276.5 550 663173.5 552 589938.2 554 573024.4 556 503221.7 558 544584.25 560 481046.063 562 467284.625 564 436743.8 566 468255.219 568 410614.938 570 412779.719 572 375276.031 574 358278.9 576 365173.9 578 343188.563 580 332026.5 582 305734.156 584 291582.75 586 291740.75 588 277996.563 590 249747.234 592 246361.031 594 244691.672 596 239243.188 598 216143.5 600 200745.766 602 214181.953 604 177607 606 183376.625 608 179293.516 610 176023.719 612 160995.844 614 146706.266 616 146900.234 618 147309.766 620 136115.875 622 131905.109 624 106181.375 626 110407.023 628 100539.734 630 95688 632 118603.867 634 91080.0859 636 91863.55 638 78163.82 640 81195.0156 642 86930.08 644 66855.28 646 74421.28 648 74467.04 650 68421.49 652 51599.6055 654 53237.08 656 53908.8 658 57732.6875 660 61688.98 662 70093.875 664 43035.8359 666 40801.582 668 40785.1758 670 33138.9023 672 30472.666 674 55041.0078 676 56478.33 678 21661.3184 680 36966.81 682 54468.9453 684 35135.332 686 31320.7324 688 26134.1934 690 26041.0977 692 19202.0059 694 14010.0957 696 35668.3945 698 29299.207 700 28045.9434 702 19407.0547 704 12863.0469 706 21107.791 708 25285.9551 710 9640.064 712 13531.042 714 21343.1172 716 14777.873 718 15364.5469 720 16100.9248 722 8149.598 724 17503.5938 726 6578.39355 728 6894.84131 730 27725.9453 732 10230.2266 734 27440.1348 736 19907.14 738 -6765.12256 740 35107.8945 742 2721.47266 744 13321.35 746 45550.7539 748 -707.522949 750 20321.9863 752 6322.102 754 27491.2188 756 2860.30151 758 -729.478455 760 21666.8945 762 3068.32227 764 3148.045 766 3254.05957 768 -5048.577 770 3499.22241 772 17222.6426 774 13170.5605 776 13690.1982 778 29671.1211 780 9544.026 782 37602.43 784 50439.7852 786 4749.62646 788 11072.4756 790 24383.9629 792 25446.3242 794 -8370.253 796 27464.5859 798 28683.4668 800 30009.2988 </Trace>
fidi = fopen(filename);
data = textscan(fidi, '%f%f', 'HeaderLines',4, 'CollectOutput',1)
data = 1×1 cell array
{276×2 double}
fclose(fidi);
figure
plot(data{:}(:,1), data{:}(:,2))
grid
xlabel('X')
ylabel('Y')
xy = cell2mat(data)
xy = 276×2
1.0e+06 * 0.0003 0.0007 0.0003 0.0010 0.0003 0.0014 0.0003 0.0031 0.0003 0.0010 0.0003 0.0030 0.0003 0.0009 0.0003 0.0016 0.0003 0.0006 0.0003 0.0015 0.0003 0.0018 0.0003 0.0026 0.0003 0.0017 0.0003 0.0019 0.0003 0.0013
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(xy(:,1), xy(:,2))
grid
xlabel('X')
ylabel('Y')
.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!