How can I read dat file into table/cell?

9 次查看(过去 30 天)
Hello,
how can I import the attached file into MATLAB workspace to have a "normal" table and/or cell?
THANKS,
Mark

回答(2 个)

Walter Roberson
Walter Roberson 2022-6-2
fileread() the entire file. textscan() the character vector with 'headerlines' to extract the number table. After that use string processing methods to pull out whatever header information you need (regexp named tokens are useful)

Star Strider
Star Strider 2022-6-2
The first 16 lines will have to be dealt with separately (this textscan call works to produce a (16x1 ) cell array of them) if they are necessary. The data will need to be parsed from them separately. (The fopen and related functions do not work with the onlilne Run feature so I cannot demonstrate them here.)
fidi = fopen('moose_file.txt', 'rt')
First16 = textscan(fidi,'%s', 16, 'Delimiter','\r\n');
fclose(fidi);
The readtable function with the 'HeaderLines' option works for the table data.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1019580/moose_file.txt', 'HeaderLines',17)
T1 = 893×9 table
Wavelengths Spectrum Target TargetIT RefData RefIT Stray StrayIT Theoretical ___________ ________ _______ ________ _______ _______ ___________ _______ ___________ 235.64 0.48305 0.59027 0.58757 0.58857 0.5855 3.2421e-05 0.58819 0.48341 236.45 0.48301 0.6062 0.60342 0.60386 0.60117 -2.2783e-05 0.60295 0.48293 237.26 0.48331 0.62087 0.61757 0.61849 0.61541 -4.233e-05 0.61719 0.48315 238.07 0.48446 0.63206 0.62885 0.62971 0.62701 -1.3849e-05 0.62821 0.48379 238.88 0.48609 0.64724 0.64368 0.6436 0.64045 2.3137e-05 0.64171 0.4858 239.69 0.48797 0.66477 0.66131 0.65946 0.6564 4.6013e-06 0.65753 0.48791 240.49 0.4903 0.68036 0.67608 0.67664 0.67291 -8.3519e-06 0.67405 0.49011 241.3 0.49289 0.69538 0.69251 0.69455 0.69066 -1.6692e-05 0.69194 0.49294 242.11 0.49603 0.71343 0.71034 0.71084 0.70764 6.4975e-07 0.70877 0.49607 242.92 0.4993 0.73058 0.72625 0.72868 0.72558 2.8269e-05 0.72647 0.49905 243.73 0.50242 0.74576 0.74229 0.74318 0.73967 -1.7437e-05 0.74092 0.50207 244.54 0.50567 0.76828 0.76449 0.76278 0.75932 -1.1169e-05 0.76112 0.50546 245.35 0.50934 0.7855 0.78101 0.78037 0.77701 1.6867e-05 0.77787 0.50888 246.16 0.51291 0.80344 0.79939 0.79927 0.7956 -7.5975e-06 0.79684 0.51233 246.97 0.51646 0.8274 0.82259 0.82383 0.82051 -2.4694e-05 0.82154 0.51593 247.77 0.51969 0.84981 0.84615 0.84642 0.84313 -2.7151e-05 0.84396 0.51964
.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by