MATLAB: How to split a big text file based on specific strings

big dataparsetext file

I am acquiring physiological data, during each routine of my experiment.
For each routine, I save the name of the routine, time, and data of 8 channels (a total of 9 columns), in a text file.
The entire text file is very big (>20Gb) and matlab cannot handle it fully. I added a sample of it below.
Each routine begins with the word "Routine", then the follwing row contains the headers and the following rows contains the data.
Could you please help me to:
  • Extract the name of each routine and its row index
  • Extract the 8 columns of values for each routine and save it in matrix ( and then in a .mat).
I tried to use textscan but I was not able to call correctly my data I guess
I tried strfind but it returned the starting indexe of each "Routine" but I did not know how to extract the rest of the line and or the values for each routine
I also looked into regexprep, fgetl etc. but I am slighlty lost on how to handle such big files as characters…
——————————-
sample of a file
Routine Signal Acq and Paramet with condition standard
Time [Val- channel1] [Val- channel2] [Val- channel3] [Val- channel4] [Tr-channel1] [Tr-channel2] [Tr-channel3] [Tr-channel4]
2.5936 -0.00054016 0.0066925 0.0017426 0.049998 0 0 0 0
4.0773 -0.00053864 0.0066956 0.0017456 0.049998 0 0 0 0
4.1159 -0.00054016 0.006694 0.0017456 0.049998 0 0 0 0
4.1469 -0.00054016 0.006694 0.0017426 0.049998 0 0 0 0
4.1511 -0.00054779 0.006694 0.0017395 0.049998 0 0 0 0
4.1531 -0.00054626 0.006694 0.0017395 0.049998 0 0 0 0
4.1553 -0.00054626 0.0066956 0.001741 0.049998 0 0 0 0
4.1584 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
4.1619 -0.00054779 0.006694 0.0017456 0.049998 0 0 0 0
4.1653 -0.00053864 0.006694 0.0017456 0.049998 0 0 0 0
4.1686 -0.00053711 0.0066925 0.0017471 0.049998 0 0 0 0
4.1713 -0.00054016 0.0066895 0.001741 0.049998 0 0 0 0
4.1751 -0.00054932 0.006694 0.0017395 0.049998 0 0 0 0
4.1779 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
4.1799 -0.00053406 0.0066925 0.0017502 0.049998 0 0 0 0
4.1827 -0.00053711 0.0066956 0.0017487 0.049998 0 0 0 0
4.1848 -0.00054016 0.0066925 0.0017471 0.049998 0 0 0 0
4.1875 -0.00054321 0.006694 0.0017441 0.049998 0 0 0 0
4.1903 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
4.1923 -0.00054016 0.006694 0.0017502 0.049998 0 0 0 0
4.1943 -0.00053864 0.0066895 0.0017426 0.049998 0 0 0 0
4.1971 -0.00054169 0.006694 0.0017395 0.049998 0 0 0 0
4.1994 -0.00054626 0.0066956 0.0017441 0.049998 0 0 0 0
4.2018 -0.00054474 0.006694 0.0017487 0.049998 0 0 0 0
4.204 -0.00056 0.0066925 0.0017456 0.049998 0 0 0 0
4.2065 -0.00055389 0.0066956 0.0017426 0.049998 0 0 0 0
4.2966 -0.00054626 0.0066925 0.001741 0.049998 0 0 0 0
4.2992 -0.00054474 0.0066895 0.0017426 0.049998 0 0 0 0
4.3013 -0.00053711 0.006694 0.0017456 0.049998 0 0 0 0
4.3045 -0.00052643 0.0066925 0.0017502 0.049998 0 0 0 0
4.3068 -0.00053253 0.0066925 0.0017502 0.049998 0 0 0 0
4.3087 -0.00053406 0.006694 0.0017471 0.049998 0 0 0 0
4.311 -0.00053711 0.0066925 0.0017517 0.049998 0 0 0 0
4.3132 -0.00054169 0.0066956 0.0017532 0.049998 0 0 0 0
4.3155 -0.00053864 0.006694 0.0017532 0.049998 0 0 0 0
4.3179 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
4.3207 -0.00052795 0.006694 0.0017532 0.049998 0 0 0 0
4.3237 -0.00052338 0.006694 0.0017532 0.049998 0 0 0 0
4.3265 -0.00053558 0.0066925 0.0017502 0.049998 0 0 0 0
4.3298 -0.00054779 0.0066925 0.0017441 0.049998 0 0 0 0
4.3319 -0.00054779 0.0066956 0.0017456 0.049998 0 0 0 0
4.334 -0.00055084 0.0066925 0.001741 0.049998 0 0 0 0
4.3358 -0.00054932 0.006694 0.0017441 0.049998 0 0 0 0
4.3378 -0.00055084 0.006694 0.0017502 0.049998 0 0 0 0
4.3398 -0.00055389 0.006694 0.0017532 0.049998 0 0 0 0
4.3426 -0.00055237 0.0066925 0.0017517 0.049998 0 0 0 0
4.345 -0.00053558 0.0066971 0.0017532 0.049998 0 0 0 0
4.3475 -0.00053101 0.0066971 0.0017441 0.049998 0 0 0 0
4.3496 -0.00053864 0.0066956 0.0017349 0.049998 0 0 0 0
4.3767 -0.00054474 0.006694 0.001738 0.049998 0 0 0 0
4.3796 -0.00054626 0.006694 0.0017426 0.049998 0 0 0 0
4.3814 -0.00053558 0.006694 0.0017456 0.049998 0 0 0 0
4.3831 -0.00052795 0.0066956 0.0017517 0.049998 0 0 0 0
4.3846 -0.00054169 0.0066971 0.0017365 0.049998 0 0 0 0
4.386 -0.00054779 0.0066956 0.0017319 0.049998 0 0 0 0
4.3876 -0.00053864 0.0066971 0.0017365 0.049998 0 0 0 0
4.3892 -0.00054016 0.0066956 0.0017441 0.049998 0 0 0 0
4.3908 -0.00054016 0.0066956 0.0017487 0.049998 0 0 0 0
4.3925 -0.00054932 0.0066956 0.0017441 0.049998 0 0 0 0
4.3943 -0.00054474 0.006694 0.0017395 0.049998 0 0 0 0
4.396 -0.00054016 0.0066956 0.0017365 0.049998 0 0 0 0
4.4216 -0.00054626 0.0066971 0.0017349 0.049998 0 0 0 0
4.4255 -0.00055237 0.0066971 0.0017288 0.049998 0 0 0 0
Routine Signal Acq and Paramet with condition standard
Time [Val- channel1] [Val- channel2] [Val- channel3] [Val- channel4] [Tr-channel1] [Tr-channel2] [Tr-channel3] [Tr-channel4]
-155750.71 -0.00054016 0.0066925 0.0017426 0.049998 0 0 0 0
-155749.23 -0.00053864 0.0066956 0.0017456 0.049998 0 0 0 0
-155749.19 -0.00054016 0.006694 0.0017456 0.049998 0 0 0 0
-155749.16 -0.00054016 0.006694 0.0017426 0.049998 0 0 0 0
-155749.15 -0.00054779 0.006694 0.0017395 0.049998 0 0 0 0
-155749.15 -0.00054626 0.006694 0.0017395 0.049998 0 0 0 0
-155749.15 -0.00054626 0.0066956 0.001741 0.049998 0 0 0 0
-155749.14 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
-155749.14 -0.00054779 0.006694 0.0017456 0.049998 0 0 0 0
-155749.14 -0.00053864 0.006694 0.0017456 0.049998 0 0 0 0
-155749.13 -0.00053711 0.0066925 0.0017471 0.049998 0 0 0 0
-155749.13 -0.00054016 0.0066895 0.001741 0.049998 0 0 0 0
-155749.13 -0.00054932 0.006694 0.0017395 0.049998 0 0 0 0
-155749.12 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
-155749.12 -0.00053406 0.0066925 0.0017502 0.049998 0 0 0 0
-155749.12 -0.00053711 0.0066956 0.0017487 0.049998 0 0 0 0
-155749.12 -0.00054016 0.0066925 0.0017471 0.049998 0 0 0 0
-155749.12 -0.00054321 0.006694 0.0017441 0.049998 0 0 0 0
-155749.11 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
-155749.11 -0.00054016 0.006694 0.0017502 0.049998 0 0 0 0
-155749.11 -0.00053864 0.0066895 0.0017426 0.049998 0 0 0 0
-155749.11 -0.00054169 0.006694 0.0017395 0.049998 0 0 0 0
-155749.1 -0.00054626 0.0066956 0.0017441 0.049998 0 0 0 0
-155749.1 -0.00054474 0.006694 0.0017487 0.049998 0 0 0 0
-155749.1 -0.00056 0.0066925 0.0017456 0.049998 0 0 0 0
-155749.1 -0.00055389 0.0066956 0.0017426 0.049998 0 0 0 0
-155749.01 -0.00054626 0.0066925 0.001741 0.049998 0 0 0 0
-155749 -0.00054474 0.0066895 0.0017426 0.049998 0 0 0 0
-155749 -0.00053711 0.006694 0.0017456 0.049998 0 0 0 0
-155749 -0.00052643 0.0066925 0.0017502 0.049998 0 0 0 0
-155749 -0.00053253 0.0066925 0.0017502 0.049998 0 0 0 0
-155748.99 -0.00053406 0.006694 0.0017471 0.049998 0 0 0 0
-155748.99 -0.00053711 0.0066925 0.0017517 0.049998 0 0 0 0
-155748.99 -0.00054169 0.0066956 0.0017532 0.049998 0 0 0 0
-155748.99 -0.00053864 0.006694 0.0017532 0.049998 0 0 0 0
-155748.98 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
-155748.98 -0.00052795 0.006694 0.0017532 0.049998 0 0 0 0
-155748.98 -0.00052338 0.006694 0.0017532 0.049998 0 0 0 0
-155748.98 -0.00053558 0.0066925 0.0017502 0.049998 0 0 0 0
-155748.97 -0.00054779 0.0066925 0.0017441 0.049998 0 0 0 0
-155748.97 -0.00054779 0.0066956 0.0017456 0.049998 0 0 0 0
-155748.97 -0.00055084 0.0066925 0.001741 0.049998 0 0 0 0
-155748.97 -0.00054932 0.006694 0.0017441 0.049998 0 0 0 0
-155748.97 -0.00055084 0.006694 0.0017502 0.049998 0 0 0 0
-155748.96 -0.00055389 0.006694 0.0017532 0.049998 0 0 0 0
-155748.96 -0.00055237 0.0066925 0.0017517 0.049998 0 0 0 0
-155748.96 -0.00053558 0.0066971 0.0017532 0.049998 0 0 0 0
-155748.96 -0.00053101 0.0066971 0.0017441 0.049998 0 0 0 0
-155748.95 -0.00053864 0.0066956 0.0017349 0.049998 0 0 0 0
-155748.93 -0.00054474 0.006694 0.001738 0.049998 0 0 0 0
-155748.92 -0.00054626 0.006694 0.0017426 0.049998 0 0 0 0
-155748.92 -0.00053558 0.006694 0.0017456 0.049998 0 0 0 0
-155748.92 -0.00052795 0.0066956 0.0017517 0.049998 0 0 0 0
-155748.92 -0.00054169 0.0066971 0.0017365 0.049998 0 0 0 0
-155748.92 -0.00054779 0.0066956 0.0017319 0.049998 0 0 0 0
-155748.92 -0.00053864 0.0066971 0.0017365 0.049998 0 0 0 0
-155748.91 -0.00054016 0.0066956 0.0017441 0.049998 0 0 0 0
-155748.91 -0.00054016 0.0066956 0.0017487 0.049998 0 0 0 0
-155748.91 -0.00054932 0.0066956 0.0017441 0.049998 0 0 0 0
-155748.91 -0.00054474 0.006694 0.0017395 0.049998 0 0 0 0
-155748.91 -0.00054016 0.0066956 0.0017365 0.049998 0 0 0 0
-155748.88 -0.00054626 0.0066971 0.0017349 0.049998 0 0 0 0
-155748.88 -0.00055237 0.0066971 0.0017288 0.049998 0 0 0 0
-155748.88 -0.00055847 0.0066971 0.0017303 0.049998 0 0 0 0
-155748.87 -0.00055237 0.0066956 0.0017303 0.049998 0 0 0 0
-155748.87 -0.00054474 0.0066925 0.001738 0.049998 0 0 0 0
-155748.87 -0.00053558 0.0066971 0.001741 0.049998 0 0 0 0
-155748.87 -0.00053711 0.0066956 0.001741 0.049998 0 0 0 0
-155748.86 -0.00054169 0.0066971 0.0017365 0.049998 0 0 0 0
-155748.86 -0.00054626 0.0066971 0.0017319 0.049998 0 0 0 0
-155748.86 -0.00054626 0.0066971 0.0017319 0.049998 0 0 0 0
-155748.86 -0.00054626 0.006694 0.0017349 0.049998 0 0 0 0
-155748.86 -0.00054321 0.0066971 0.0017349 0.049998 0 0 0 0
-155748.85 -0.00053711 0.0066956 0.001738 0.049998 0 0 0 0
-155748.85 -0.00053558 0.0066956 0.0017395 0.049998 0 0 0 0
-155748.85 -0.00054016 0.0066986 0.001741 0.049998 0 0 0 0
-155748.85 -0.00054169 0.0066971 0.001738 0.049998 0 0 0 0
-155748.84 -0.00054321 0.0066971 0.001741 0.049998 0 0 0 0
-155748.84 -0.00054169 0.0066971 0.001738 0.049998 0 0 0 0
-155748.84 -0.00053711 0.0066971 0.0017441 0.049998 0 0 0 0
-155748.84 -0.00053864 0.0066986 0.0017456 0.049998 0 0 0 0
-155748.83 -0.00054016 0.0066971 0.0017426 0.049998 0 0 0 0
-155748.83 -0.00053558 0.0066971 0.0017426 0.049998 0 0 0 0
-155748.83 -0.00053711 0.0067001 0.0017441 0.049998 0 0 0 0
-155748.83 -0.00053711 0.0066986 0.001738 0.049998 0 0 0 0
-155748.83 -0.00053711 0.0066956 0.001741 0.049998 0 0 0 0
-155748.82 -0.00053864 0.0066971 0.0017426 0.049998 0 0 0 0
-155748.82 -0.00054016 0.0066986 0.0017395 0.049998 0 0 0 0
-155748.82 -0.00053711 0.0067001 0.0017426 0.049998 0 0 0 0
-155748.82 -0.00053711 0.0067017 0.0017426 0.049998 0 0 0 0
-155748.81 -0.00053253 0.0067001 0.0017441 0.049998 0 0 0 0
-155748.81 -0.00053558 0.0066986 0.0017441 0.049998 0 0 0 0
-155748.81 -0.00052795 0.0067001 0.001738 0.049998 0 0 0 0
-155748.81 -0.0005249 0.0066986 0.001738 0.049998 0 0 0 0
-155748.8 -0.00052643 0.0066986 0.0017426 0.049998 0 0 0 0
-155748.8 -0.00053253 0.0066986 0.0017456 0.049998 0 0 0 0
-155748.8 -0.00053558 0.0067001 0.0017487 0.049998 0 0 0 0
-155748.8 -0.00053711 0.0066986 0.0017471 0.049998 0 0 0 0
-155748.8 -0.00053711 0.0066986 0.0017426 0.049998 0 0 0 0
-155748.79 -0.00053558 0.0067001 0.0017456 0.049998 0 0 0 0
-155748.79 -0.00053406 0.0067001 0.0017441 0.049998 0 0 0 0
-155748.79 -0.00053406 0.0067017 0.0017395 0.049998 0 0 0 0
-155748.78 -0.00053406 0.0067017 0.0017426 0.049998 0 0 0 0
-155748.78 -0.00053406 0.0067017 0.0017426 0.049998 0 0 0 0
-155748.78 -0.00053558 0.0067017 0.0017441 0.049998 0 0 0 0
-155748.78 -0.00053101 0.0067001 0.0017426 0.049998 0 0 0 0
-155748.78 -0.00053558 0.0067017 0.0017441 0.049998 0 0 0 0
-155748.77 -0.00053101 0.0067017 0.0017441 0.049998 0 0 0 0
-155748.77 -0.00052643 0.0067017 0.0017487 0.049998 0 0 0 0
-155748.77 -0.00052643 0.0067032 0.0017456 0.049998 0 0 0 0
-155748.77 -0.00053253 0.0067001 0.001741 0.049998 0 0 0 0
-155747.77 -0.00053711 0.0067047 0.001741 0.049998 0 0 0 0
Routine visu_exp with condition 4
Time [Val- channel1] [Val- channel2] [Val- channel3] [Val- channel4] [Tr-channel1] [Tr-channel2] [Tr-channel3] [Tr-channel4]
-199064.97 -0.00054016 0.0066925 0.0017426 0.049998 0 0 0 0
-199063.48 -0.00053864 0.0066956 0.0017456 0.049998 0 0 0 0
-199063.44 -0.00054016 0.006694 0.0017456 0.049998 0 0 0 0
-199063.41 -0.00054016 0.006694 0.0017426 0.049998 0 0 0 0
-199063.41 -0.00054779 0.006694 0.0017395 0.049998 0 0 0 0
-199063.41 -0.00054626 0.006694 0.0017395 0.049998 0 0 0 0
-199063.4 -0.00054626 0.0066956 0.001741 0.049998 0 0 0 0
-199063.4 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
-199063.4 -0.00054779 0.006694 0.0017456 0.049998 0 0 0 0
-199063.39 -0.00053864 0.006694 0.0017456 0.049998 0 0 0 0
-199063.39 -0.00053711 0.0066925 0.0017471 0.049998 0 0 0 0
-199063.39 -0.00054016 0.0066895 0.001741 0.049998 0 0 0 0
-199063.39 -0.00054932 0.006694 0.0017395 0.049998 0 0 0 0
-199063.38 -0.00054474 0.006694 0.0017426 0.049998 0 0 0 0
-199063.38 -0.00053406 0.0066925 0.0017502 0.049998 0 0 0 0
-199063.38 -0.00053711 0.0066956 0.0017487 0.049998 0 0 0 0
-199063.38 -0.00054016 0.0066925 0.0017471 0.049998 0 0 0 0
-199063.37 -0.00054321 0.006694 0.0017441 0.049998 0 0 0 0
-199063.37 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
-199063.37 -0.00054016 0.006694 0.0017502 0.049998 0 0 0 0
-199063.37 -0.00053864 0.0066895 0.0017426 0.049998 0 0 0 0
-199063.36 -0.00054169 0.006694 0.0017395 0.049998 0 0 0 0
-199063.36 -0.00054626 0.0066956 0.0017441 0.049998 0 0 0 0
-199063.36 -0.00054474 0.006694 0.0017487 0.049998 0 0 0 0
-199063.36 -0.00056 0.0066925 0.0017456 0.049998 0 0 0 0
-199063.35 -0.00055389 0.0066956 0.0017426 0.049998 0 0 0 0
-199063.26 -0.00054626 0.0066925 0.001741 0.049998 0 0 0 0
-199063.26 -0.00054474 0.0066895 0.0017426 0.049998 0 0 0 0
-199063.26 -0.00053711 0.006694 0.0017456 0.049998 0 0 0 0
-199063.26 -0.00052643 0.0066925 0.0017502 0.049998 0 0 0 0
-199063.25 -0.00053253 0.0066925 0.0017502 0.049998 0 0 0 0
-199063.25 -0.00053406 0.006694 0.0017471 0.049998 0 0 0 0
-199063.25 -0.00053711 0.0066925 0.0017517 0.049998 0 0 0 0
-199063.25 -0.00054169 0.0066956 0.0017532 0.049998 0 0 0 0
-199063.24 -0.00053864 0.006694 0.0017532 0.049998 0 0 0 0
-199063.24 -0.00053711 0.006694 0.0017471 0.049998 0 0 0 0
-199063.24 -0.00052795 0.006694 0.0017532 0.049998 0 0 0 0
-199063.24 -0.00052338 0.006694 0.0017532 0.049998 0 0 0 0
-199063.23 -0.00053558 0.0066925 0.0017502 0.049998 0 0 0 0
-199063.23 -0.00054779 0.0066925 0.0017441 0.049998 0 0 0 0
-199063.23 -0.00054779 0.0066956 0.0017456 0.049998 0 0 0 0
-199063.23 -0.00055084 0.0066925 0.001741 0.049998 0 0 0 0
-199063.22 -0.00054932 0.006694 0.0017441 0.049998 0 0 0 0
-199063.22 -0.00055084 0.006694 0.0017502 0.049998 0 0 0 0
-199063.22 -0.00055389 0.006694 0.0017532 0.049998 0 0 0 0
-199063.22 -0.00055237 0.0066925 0.0017517 0.049998 0 0 0 0
-199063.22 -0.00053558 0.0066971 0.0017532 0.049998 0 0 0 0
-199063.21 -0.00053101 0.0066971 0.0017441 0.049998 0 0 0 0
-199063.21 -0.00053864 0.0066956 0.0017349 0.049998 0 0 0 0
-199063.18 -0.00054474 0.006694 0.001738 0.049998 0 0 0 0
-199063.18 -0.00054626 0.006694 0.0017426 0.049998 0 0 0 0
-199063.18 -0.00053558 0.006694 0.0017456 0.049998 0 0 0 0
-199063.18 -0.00052795 0.0066956 0.0017517 0.049998 0 0 0 0
-199063.18 -0.00054169 0.0066971 0.0017365 0.049998 0 0 0 0
-199063.17 -0.00054779 0.0066956 0.0017319 0.049998 0 0 0 0
-199063.17 -0.00053864 0.0066971 0.0017365 0.049998 0 0 0 0
-199063.17 -0.00054016 0.0066956 0.0017441 0.049998 0 0 0 0
-199063.17 -0.00054016 0.0066956 0.0017487 0.049998 0 0 0 0
-199063.17 -0.00054932 0.0066956 0.0017441 0.049998 0 0 0 0
-199063.17 -0.00054474 0.006694 0.0017395 0.049998 0 0 0 0
-199063.16 -0.00054016 0.0066956 0.0017365 0.049998 0 0 0 0
-199063.14 -0.00054626 0.0066971 0.0017349 0.049998 0 0 0 0
-199063.13 -0.00055237 0.0066971 0.0017288 0.049998 0 0 0 0
-199063.13 -0.00055847 0.0066971 0.0017303 0.049998 0 0 0 0
-199063.13 -0.00055237 0.0066956 0.0017303 0.049998 0 0 0 0
-199063.13 -0.00054474 0.0066925 0.001738 0.049998 0 0 0 0
-199063.13 -0.00053558 0.0066971 0.001741 0.049998 0 0 0 0
-199063.12 -0.00053711 0.0066956 0.001741 0.049998 0 0 0 0
-199063.12 -0.00054169 0.0066971 0.0017365 0.049998 0 0 0 0
-199063.12 -0.00054626 0.0066971 0.0017319 0.049998 0 0 0 0
-199063.12 -0.00054626 0.0066971 0.0017319 0.049998 0 0 0 0
-199063.11 -0.00054626 0.006694 0.0017349 0.049998 0 0 0 0
-199063.11 -0.00054321 0.0066971 0.0017349 0.049998 0 0 0 0
-199063.11 -0.00053711 0.0066956 0.001738 0.049998 0 0 0 0
-199063.11 -0.00053558 0.0066956 0.0017395 0.049998 0 0 0 0
-199063.11 -0.00054016 0.0066986 0.001741 0.049998 0 0 0 0
-199063.1 -0.00054169 0.0066971 0.001738 0.049998 0 0 0 0
-199063.1 -0.00054321 0.0066971 0.001741 0.049998 0 0 0 0

Best Answer

The code below will read the txt file attached, psychData.txt (data copied from your question).
The first section uses fgetl() which very quickly scans your entire file line by line and merely stores the line numbers where a routine starts ("routineLine"). The next section creates a table for each routine in a loop. It uses textscan() to pull the routine name and stores it in "R". It then uses readmatrix() along with delimitedTextImportOptions() to pull the matrix data from each routine. The tables are stored in the cell array "T". If there are small differences between the attached txt file and your real data, you may need to tweek some of the settings in delimitedTextImportOptions(). See my text after the code for examples of how to work with the data.
%% FIND ROUTINE LINES
filename = 'psychData.txt'; %full path would be better
fid = fopen(filename); %opens file
c = 1; %line counter
routineLine = []; %stores line numbers of Routine
tline = fgetl(fid); %reads 1st line
while ischar(tline) %loop through each line
if regexp(tline,'^Routine')==1 %Determine if line begins with "Routine"
routineLine(end+1) = c; %store line number of "Routine" lines
end
tline = fgetl(fid); %reads next line
c = c+1; %increment line counter
end
%% PUT MATRIX DATA INTO TABLES
% set up option for reading the matrix data (see delimitedTextImportOptions for help)
varNames = {'Time','ch1','ch2','ch3','ch4','trCh1','trCh2','trCh3','trCh4'};
opts = delimitedTextImportOptions('NumVariables',numel(varNames), 'VariableTypes',repelem({'double'},1,9), ...
'Delimiter','\t','ConsecutiveDelimitersRule','join','ExtraColumnsRule','ignore');
T = cell(numel(routineLine),1); % will store tables
R = T; % will store routine names
routineLine = [routineLine, c-1]; % add last line of text to routineLine
% loop through each routine and extract name and matrix
for i = 1:numel(routineLine)-1
frewind(fid) % Reset to 1st row
routineTxt = textscan(fid,'%s',1,'delimiter','\n','headerlines',routineLine(i)-1); %routine text
R{i} = routineTxt{1}{1}; % store routine name
% The matrix will be 2 lines after the routine line and will end 2 line prior to next routine or end of file.
opts.DataLines = [routineLine(i)+2, routineLine(i+1)-2];
matData = readmatrix(filename,opts);
% store in table
T{i} = array2table(matData,'VariableNames',varNames);
T{i}.Properties.Description = R{i};
end
fclose(fid); %close file
How to work with the data
n = 2;
T{n} % is table number 2
R{n} % is the routine name
% The routine name is also stored in the table
T{n}.Properties.Description % routine name