Textscan Reads Only the first 3 numbers

2 次查看(过去 30 天)
Hello,
I´m trying to read this .txt (attached) file and store the values (54 columuns).
This is the code that I´m using
Name_1 = 'fffff.txt';
fid = fopen(Name_1,'r');
cell_data1= textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','Delimiter','','headerLines',6,'CollectOutput',1);
fclose(fid);
But unfortunately the code is messing with the first two columns (data and time).
This is what it stores.
How can I skip those two and just store the remaining 52 columns?
Thanks in advance
  2 个评论
Star Strider
Star Strider 2022-9-26
Look at the file —
C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1136490/fffff.txt')
C1 = 11×3 cell array
{'Run' } {[ 279]} {1×1 missing } {'Comment' } {1×1 missing } {1×1 missing } {'Runmap' } {'K_30_01_QS_yaw-sweep-vmax' } {1×1 missing } {'Customer' } {'Unk' } {1×1 missing } {'Date' } {'Time→Yaw→Lean→Vair→Pa→Ptr-P0→Temp→Hr→Rho→Fx→Fy→Fz→Mx→My→Mz→P1→P2→P3→P4→P5→P6→P7→P8→P9→P10→P11→P12→P13→P14→P15→P16→P17→P18→P19→P20→P21→P22→P23→P24→P25→P26→P27→P28→P29→P30→P31→P32→Ffront wheel→Tin→Tout→Waterflow→Cooling power→Specific cooling power'} {1×1 missing } {'[°]→[°]→[m/s]→[Pa]→[Pa]→[°C]→[%]→[kg/m3]→[N]→[N]→[N]→[Nm]→[Nm]→[Nm]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[mbar→[mbar]→[mbar]→[mbar]→[mbar]→[mbar]→[N]→[°C]→[°C]→[L/min]→[W]→[W/K]'} {1×1 missing } {1×1 missing } {'02.08.2022→08' } {[ 58]} {'46.808→-0.0→0→42.451419→97440.000000→2747.840000→23.944000→48.167000→1.125151→190.029430→-1.040025→24.963842→5.668323→225.180367→-8.632680→4.973469→5.276467→6.254003→5.457921→4.365939→5.734513→6.224498→5.820748→1.492454→1.477490→2.035428→1.939299→2.402537→2.555415→3.348971→2.868781→6.134119→5.032489→5.684587→5.497022→5.423880→5.844911→7.126764→6.452095→3.440309→3.477351→3.658881→3.652628→3.494377→3.958303→3.761011→4.198308→0.000000→0.000000→0.000000→0.182638→0.000000→0.000000' } {'02.08.2022→08' } {[ 59]} {'52.358→-5.0→0→42.611887→97441.200000→2764.940000→24.322000→47.327000→1.123642→210.721356→-76.001284→26.071040→88.740052→252.588305→12.095614→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→0.000000→0.000000→0.000000→0.139043→0.000000→0.000000' } {'02.08.2022→09' } {[ 0]} {'57.896→-10.0→0→42.414539→97441.000000→2736.540000→24.640000→46.733000→1.122474→249.700103→-179.193928→34.067972→210.375328→299.671509→40.124381→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→NaN→0.000000→0.000000→0.000000→0.102659→0.000000→0.000000' } {'02.08.2022→09' } {[ 2]} {'16.453→5.0→0→42.469382→97440.000000→2740.790000→24.925000→46.182000→1.121315→210.274708→94.749321→27.919850→-99.283288→250.877907→-28.952380→4.254837→4.685409→5.643184→4.738926→3.920298→5.085000→5.891468→5.425929→1.222925→1.214788→1.716948→1.624163→2.045858→2.206987→2.927361→2.459074→6.248071→5.028299→5.852960→5.512444→5.526658→5.891837→7.146380→6.558506→3.254459→3.327796→3.516305→3.564396→3.464628→3.915232→3.770194→4.187859→0.000000→0.000000→0.000000→-0.029766→0.000000→0.000000' } {'02.08.2022→09' } {[ 3]} {'21.990→10.0→0→42.100788→97436.300000→2692.300000→25.098000→45.822000→1.120848→247.107995→207.510397→32.681409→-224.593804→296.968280→-47.728579→3.479251→4.014095→4.788371→3.987725→3.399686→4.275092→5.493334→4.896998→0.873039→0.861312→1.301504→1.230452→1.618516→1.763496→2.358921→1.973357→6.018073→4.736682→5.671217→5.442419→5.308567→5.637929→6.974420→5.938056→2.850111→2.953910→3.135825→3.192652→3.177201→3.663082→3.501401→3.934163→0.000000→0.000000→0.000000→0.004815→0.000000→0.000000'}
There does not appear to be anything in the file (other than a few dates and some integer values). There are 6 header lines, then a date (best read as a string or as a date — see Nonnumeric Fields in the formatSpec section of the documentation) and a single integer.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2022-9-26
@Fabio Taccaliti - perhaps instead you can use readtable to read the table from file and then extract the data you need. For example,
filename = 'fffff.txt';
tableData = readtable(Name_1,'Delimiter','\t','headerLines',6);
data = tableData(:,3:end);
  4 个评论
Fabio Taccaliti
Fabio Taccaliti 2022-9-26
编辑:Fabio Taccaliti 2022-9-26
I tried but I have this strange error
Error in untitled (line 10)
data = table2array(tableData);
Caused by:
Error using datetime/horzcat (line 1398)
All inputs must be datetimes or date/time character vectors or date/time strings.
Geoff Hayes
Geoff Hayes 2022-9-26
There must be some invalid data in one of your date or time columns. You may need to glance through the data to figure out which field is invalid. Or exclude those columns altogether (which I thought is what you wanted to do?).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by