NEED TO READ A FILE AND STORE VALUES

Good morning,
I'm having trouble reading a .txt file in matlab. The file contains different data with different formats delimited from headerlines.
The file structure is as follows:
Field Time R1:X R1:Y R1:Z R2:X R2:Y R2:Z R3:X R3:Y R3:Z
1 0.0000 196.1938 98.836 986.1718 194.511 94.529488 962.833557 235.324265 89.228859 961.438660
...
Sample # Fx Fy Fz Mx My Mz Fx Fy Fz Mx My Mz EMG 1 EMG 2 EMG 3 EMG 4 EMG 5 EMG 6 EMG 7 EMG 8 BASA BASB
1 6.901305 17.359007 -439.079376 41616.441406 -5983.515625 602.935791 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 -0.044708 -0.041351 -0.043640 -0.044708 -0.045319 -0.044250 -0.042877 -0.042725 -0.004272 4.121094
.....
Force Plate 1 Force Plate 2
Sample # COP:X COP:Y COP:Z Ref:X Ref:Y Ref:Z Force:X Force:Y Force:Z Moment:X Moment:Y Moment:Z
1 244.158 161.003 0.000 231.769 254.000 0.000 -6.901 17.359 439.079 -40833.019 -5439.495 -612.130
......
I need to read all the data from the first header line and skip them until the second header line. Then i need to read all the data from the second header line and store the column BASA in an array. Then all the data from the third header line and store the columns COP:X, COP:Y and COP:Z in 3 different arrays.
Thanks in advance for the help.

2 Comments

@Sarah Pragliola: please upload a sample file by clicking the paperclip button.
@Stephen I uploaded the file and i also tried to explain better what i need.
Thank you.

Sign in to comment.

 Accepted Answer

I shortened the file a bit by removing some lines from the middle of those data blocks.
opt = {'Delimiter','\t', 'CollectOutput',true};
out = {};
fid = fopen('Trial02.txt','rt');
while ~feof(fid)
str = strtrim(fgetl(fid));
hdr = regexp(str,'\t+','split');
hdr = regexprep(hdr,'\W+','');
if numel(hdr)==2
two = hdr
str = strtrim(fgetl(fid));
hdr = regexp(str,'\t+','split');
hdr = regexprep(hdr,'\W+','');
nmh = ceil(numel(hdr)/2);
hdr(2:nmh-1) = strcat(two{1},hdr(2:nmh-1));
hdr(nmh:end) = strcat(two{2},hdr(nmh:end));
end
fmt = repmat('%f',1,numel(hdr));
out(end+1) = textscan(fid,fmt,opt{:});
hdr = matlab.lang.makeUniqueStrings(hdr);
out{end} = array2table(out{end},'VariableNames',hdr);
end
two = 1×2 cell array
{'ForcePlate1'} {'ForcePlate2'}
fclose(fid);
out{:}
ans = 3991×11 table
Field Time R1X R1Y R1Z R2X R2Y R2Z R3X R3Y R3Z _____ ____ ______ ______ ______ ______ ______ ______ ______ ______ ______ 1 0 196.19 98.837 986.17 194.51 94.529 962.83 235.32 89.229 961.44 2 0.01 195.81 100.12 985.95 194.15 96.094 962.59 234.93 90.731 961.34 3 0.02 195.44 101.58 985.7 193.79 97.46 962.42 234.67 91.869 961.24 4 0.03 195.03 102.97 985.51 193.37 99.05 962.37 234.24 93.762 960.97 5 0.04 194.66 104.54 985.29 192.89 100.61 962.14 233.98 95.191 960.76 6 0.05 194.3 106.2 985.09 192.49 101.71 962.06 233.64 96.571 960.49 7 0.06 193.88 107.57 984.87 191.92 103.13 961.95 233.17 98.114 960.22 8 0.07 193.36 109.15 984.75 191.46 104.4 961.72 232.71 99.31 960.02 9 0.08 192.97 110.55 984.51 191.17 106.12 961.36 232.18 101.12 959.74 10 0.09 192.52 111.88 984.22 190.88 108.2 961.01 231.86 102.83 959.39 11 0.1 192.08 113.75 983.96 190.5 109.96 960.69 231.39 104.5 958.98 12 0.11 191.72 115.36 983.7 190.1 111.7 960.33 230.9 106.17 958.67 13 0.12 191.22 117.2 983.38 189.58 112.95 960.06 230.35 107.31 958.55 14 0.13 190.81 118.68 983.11 188.93 114.56 959.78 229.7 108.85 958.44 15 0.14 190.22 120.18 982.82 188.49 116.1 959.7 229.35 110.15 958.27 16 0.15 189.68 121.57 982.75 188.04 117.9 959.54 228.92 112.26 957.9
ans = 8688×23 table
Sample Fx Fy Fz Mx My Mz Fx_1 Fy_1 Fz_1 Mx_1 My_1 Mz_1 EMG1 EMG2 EMG3 EMG4 EMG5 EMG6 EMG7 EMG8 BASA BASB ______ ______ ______ _______ _____ _______ ______ ____ ____ ____ ____ ____ ____ _________ _________ _________ _________ _________ _________ _________ _________ _________ ______ 1 6.9013 17.359 -439.08 41616 -5983.5 602.94 0 0 0 0 0 0 -0.044708 -0.041351 -0.04364 -0.044708 -0.045319 -0.04425 -0.042877 -0.042725 -0.004272 4.1211 2 6.9449 17.098 -439.08 41482 -5945.2 624.72 0 0 0 0 0 0 -0.039063 -0.037537 -0.040588 -0.040741 -0.041199 -0.040283 -0.037537 -0.040741 0.000153 4.0683 3 7.0102 17.164 -439.77 41473 -5858.9 637.79 0 0 0 0 0 0 -0.044403 -0.040741 -0.042114 -0.042877 -0.041962 -0.042419 -0.041351 -0.04425 -0.004578 4.025 4 6.9231 17.185 -438.99 41320 -5782.2 637.79 0 0 0 0 0 0 -0.046539 -0.04364 -0.044098 -0.043945 -0.046387 -0.046082 -0.045319 -0.044861 -0.005951 4.0153 5 7.0756 16.99 -440.28 41329 -5801.4 607.29 0 0 0 0 0 0 -0.049896 -0.045319 -0.046082 -0.045624 -0.047455 -0.045776 -0.046692 -0.045013 -0.007782 3.983 6 6.9667 16.795 -439.51 41176 -5715.1 594.22 0 0 0 0 0 0 -0.046692 -0.045166 -0.047607 -0.048828 -0.046234 -0.046082 -0.048676 -0.046997 -0.006714 4.0762 7 6.9449 16.599 -439.68 41128 -5676.7 611.65 0 0 0 0 0 0 -0.04837 -0.049133 -0.050507 -0.050201 -0.04837 -0.049286 -0.049896 -0.049744 -0.009766 3.9746 8 6.7488 16.708 -440.45 40975 -5705.5 602.94 0 0 0 0 0 0 -0.049591 -0.046844 -0.048828 -0.049438 -0.050201 -0.047913 -0.049286 -0.048523 -0.007935 4.1209 9 6.9885 16.295 -440.71 40851 -5667.2 572.44 0 0 0 0 0 0 -0.04837 -0.046692 -0.04715 -0.048676 -0.048523 -0.048981 -0.047607 -0.048218 -0.006866 4.1133 10 6.8577 16.447 -440.88 40774 -5513.8 550.65 0 0 0 0 0 0 -0.04715 -0.046082 -0.04776 -0.048981 -0.048676 -0.046997 -0.046234 -0.04776 -0.00885 4.0953 11 7.0538 16.36 -440.79 40765 -5533 520.16 0 0 0 0 0 0 -0.043945 -0.041809 -0.045471 -0.045013 -0.045166 -0.042877 -0.042114 -0.044556 -0.002594 4.0996 12 6.9449 16.013 -440.62 40564 -5485 498.37 0 0 0 0 0 0 -0.040436 -0.041504 -0.039673 -0.040741 -0.041046 -0.039673 -0.040894 -0.041199 -0.001068 4.0884 13 7.032 16.1 -441.39 40544 -5389.2 528.87 0 0 0 0 0 0 -0.042877 -0.041962 -0.041656 -0.042267 -0.044556 -0.043488 -0.043945 -0.041962 -0.005951 3.9883 14 6.727 16.078 -440.96 40420 -5513.8 480.94 0 0 0 0 0 0 -0.044403 -0.044556 -0.045471 -0.045471 -0.045471 -0.046082 -0.045624 -0.045929 -0.003357 3.9667 15 7.0102 15.861 -441.05 40324 -5389.2 524.51 0 0 0 0 0 0 -0.04715 -0.045929 -0.044556 -0.046997 -0.04715 -0.043945 -0.044403 -0.046387 -0.00946 3.9915 16 6.9231 15.796 -440.96 40286 -5274.1 524.51 0 0 0 0 0 0 -0.046692 -0.045776 -0.047302 -0.049438 -0.04776 -0.04715 -0.047913 -0.047913 -0.007172 4.1106
ans = 4429×25 table
Sample ForcePlate1COPX ForcePlate1COPY ForcePlate1COPZ ForcePlate1RefX ForcePlate1RefY ForcePlate1RefZ ForcePlate1ForceX ForcePlate1ForceY ForcePlate1ForceZ ForcePlate1MomentX ForcePlate1MomentY ForcePlate2MomentZ ForcePlate2COPX ForcePlate2COPY ForcePlate2COPZ ForcePlate2RefX ForcePlate2RefY ForcePlate2RefZ ForcePlate2ForceX ForcePlate2ForceY ForcePlate2ForceZ ForcePlate2MomentX ForcePlate2MomentY ForcePlate2MomentZ_1 ______ _______________ _______________ _______________ _______________ _______________ _______________ _________________ _________________ _________________ __________________ __________________ __________________ _______________ _______________ _______________ _______________ _______________ _______________ _________________ _________________ _________________ __________________ __________________ ____________________ 1 244.16 161 0 231.77 254 0 -6.9013 17.359 439.08 -40833 -5439.5 -612.13 0 0 0 -252.77 384 0 0 0 0 0 0 0 2 244.07 161.28 0 231.77 254 0 -6.9449 17.098 439.08 -40710 -5399.3 -633.76 0 0 0 -252.77 384 0 0 0 0 0 0 0 3 243.84 161.46 0 231.77 254 0 -7.0102 17.164 439.77 -40698 -5309.8 -646.86 0 0 0 -252.77 384 0 0 0 0 0 0 0 4 243.7 161.64 0 231.77 254 0 -6.9231 17.185 438.99 -40544 -5237.3 -646.88 0 0 0 -252.77 384 0 0 0 0 0 0 0 5 243.69 161.87 0 231.77 254 0 -7.0756 16.99 440.28 -40561 -5249.3 -616.26 0 0 0 -252.77 384 0 0 0 0 0 0 0 6 243.53 162.04 0 231.77 254 0 -6.9667 16.795 439.51 -40417 -5168.1 -603.09 0 0 0 -252.77 384 0 0 0 0 0 0 0 7 243.44 162.17 0 231.77 254 0 -6.9449 16.599 439.68 -40377 -5130.5 -620.41 0 0 0 -252.77 384 0 0 0 0 0 0 0 8 243.5 162.69 0 231.77 254 0 -6.7488 16.708 440.45 -40219 -5167.2 -611.77 0 0 0 -252.77 384 0 0 0 0 0 0 0 9 243.38 162.98 0 231.77 254 0 -6.9885 16.295 440.71 -40112 -5118.5 -581.02 0 0 0 -252.77 384 0 0 0 0 0 0 0 10 243.04 163.21 0 231.77 254 0 -6.8577 16.447 440.88 -40029 -4970.6 -559.33 0 0 0 -252.77 384 0 0 0 0 0 0 0 11 243.07 163.2 0 231.77 254 0 -7.0538 16.36 440.79 -40023 -4981.5 -528.76 0 0 0 -252.77 384 0 0 0 0 0 0 0 12 242.98 163.59 0 231.77 254 0 -6.9449 16.013 440.62 -39837 -4938.3 -506.79 0 0 0 -252.77 384 0 0 0 0 0 0 0 13 242.73 163.8 0 231.77 254 0 -7.032 16.1 441.39 -39814 -4838.3 -537.33 0 0 0 -252.77 384 0 0 0 0 0 0 0 14 243.05 163.99 0 231.77 254 0 -6.727 16.078 440.96 -39691 -4976.1 -489.42 0 0 0 -252.77 384 0 0 0 0 0 0 0 15 242.74 164.2 0 231.77 254 0 -7.0102 15.861 441.05 -39604 -4839.4 -532.84 0 0 0 -252.77 384 0 0 0 0 0 0 0 16 242.49 164.27 0 231.77 254 0 -6.9231 15.796 440.96 -39569 -4728.1 -532.81 0 0 0 -252.77 384 0 0 0 0 0 0 0

1 Comment

@StephenThank you so much for your availability and help.

Sign in to comment.

More Answers (1)

The easiest way is probably to use fileread() to read the entire file as a character vector. Then use string operations or regexp() to locate the headers, and split the single character vector at that point, into separate character vectors that each include the headers and all associated data. Then you can use textscan() to parse each of the character vectors, using appropriate format information and number of headers.
Instead of using fileread(), your style might prefer readlines() which reads the file and breaks it up into a cell array of character vectors. You can then scan through the character vectors looking for the boundaries, and use indexing to extract subsections. You would not have to use 'headerlines', as you would could use fixed offsets into the subset cell array. However, as you would have all the data one line at a time in a cell array, it would probably not be as efficient to read the data in as it could be if you were to put the data back into a continguous block and use textscan() or sscanf()

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!