MATLAB Answers

How to extracts the data from .dat extension and create necessary equations

2 views (last 30 days)
Prem
Prem on 16 May 2021
Commented: Prem on 20 May 2021
Dear All,
Could you please advice how can I extract the data in separate column (as matrix) from .dat extension files.
As I couldn’t attached .dat format I changed the format to .txt and attached here.
I wanted to extract the data in different columns, so that I can create necessary equations related to the data.
Appreciate your help on this. Thank you.
Example:
I wanted to create a equation like,
X=current height *(channel (1,1)-offset (1,1)) *Gains(1,1)
//
Current Time: 09:58:45.550,084 on Sat, May 2021
Test ID = 1
Specific Gravity = 2.710
Initial Height = 299.196 mm
Current Height = 299.198 mm
External Radius = 75.936 sq mm
Internal Raduis = 50.659 sq mm
Gains: 71.0580 72.3243 72.2046 11.2518 11.2134 -135.2120 -1284.9134 10.2718 20.6370 428.3664 09:58:45.553,471
Offsets: -214.9100 -207.4050 -298.4060 2627.5940 488.1109 467.1530 480.3340 707.9620 1736.2213 -576.9120 09:58:45.554,955
Pore Pressure Ext Pressure Int Pressure Sample volume Inner volume Vertical Load Torque Vert Displ Tors Disp Extra LoadCell
Channels: 1 0 2 3 4 5 6 7 8 9 09:58:45.554,453
[09:58:45.608,266] Starting Saturation [B Value] Test
0.000 -164.8305 124.8016 42.3991 2627.5940 488.1109 441.8004 497.5942 707.9620 1736.2213 -574.4476 09:58:45.611,070
90.934 168.0934 483.5434 395.7570 2626.9658 494.2830 439.5142 500.3204 707.8145 1736.1221 -573.5703 09:59:04.008,055
94.728 499.9898 828.8778 742.8513 2627.4058 478.9302 432.6477 502.5279 707.5679 1736.1730 -571.3425 09:59:26.128,921
95.726 833.0510 1170.2550 1084.3226 2627.2964 478.9098 424.4029 505.2058 707.4814 1736.0662 -568.9087 09:59:43.848,887
96.691 1170.4737 1511.9120 1424.9725 2626.6200 478.5995 415.2883 508.7967 707.1889 1736.3230 -565.9917 10:00:00.872,776
98.190 1516.1540 1859.7844 1775.7950 2626.9531 478.5690 408.2413 510.9558 706.9753 1736.3790 -563.8784 10:00:18.984,704
98.118 1865.2623 2208.3308 2123.7513 2626.5717 479.2531 399.8210 514.0152 706.9473 1736.3459 -561.4421 10:00:36.176,606
98.359 2213.1856 2556.2973 2472.3129 2627.0574 479.4973 391.1209 516.3600 706.6472 1736.4273 -558.7769 10:00:54.087,494
98.421 2559.2168 2901.1332 2816.4444 2626.2970 479.6295 384.7504 518.2266 706.6701 1736.5875 -556.4702 10:01:11.601,527
98.791 2910.2173 3251.6836 3167.7628 2626.3479 479.6524 377.4643 519.1193 706.3344 1736.5417 -554.3493 10:01:30.967,275
97.739 3297.4777 3638.6668 3554.9596 2626.9252 480.5374 366.2033 519.7245 706.3192 1736.6028 -550.6872 10:01:57.663,818
[10:02:13.034,122] Saturation [B Value] Test Done
//

Accepted Answer

Asvin Kumar
Asvin Kumar on 19 May 2021
The import tool should do the job for you. The linked page shows examples on how to use the tool.
Here's another doc page which shows how to import data from text files.
I was able to verify this for your data. I used space as the delimiter and under delimiter settings I enabled "Treat multiple delimiters as one". I also set the output type to Numerical Matrix. Here's what the generated script looks like. Feel free to explore the tool and add custom options to suit your needs.
%% Import data from text file
% Script for importing data from the following text file:
%
% filename: DATA.txt
%
% Auto-generated by MATLAB on 19-May-2021 15:08:15
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 20);
% Specify range and delimiter
opts.DataLines = [16, 26];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["HCT", "Data", "Acquisition", "Version", "VarName5", ...
"VarName6", "VarName7", "VarName8", "VarName9", "VarName10", "VarName11", ...
"Var12", "Var13", "Var14", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20"];
opts.SelectedVariableNames = ["HCT", "Data", "Acquisition", "Version", ...
"VarName5", "VarName6", "VarName7", "VarName8", "VarName9", "VarName10", "VarName11"];
opts.VariableTypes = ["double", "double", "double", "double", "double", ...
"double", "double", "double", "double", "double", "double", "string", ...
"string", "string", "string", "string", "string", "string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Specify variable properties
opts = setvaropts(opts, ["Var12", "Var13", "Var14", "Var15", "Var16", "Var17", ...
"Var18", "Var19", "Var20"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var12", "Var13", "Var14", "Var15", "Var16", "Var17", ...
"Var18", "Var19", "Var20"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "HCT", "TrimNonNumeric", true);
opts = setvaropts(opts, "HCT", "ThousandsSeparator", ",");
% Import the data
DATA = readtable("DATA.txt", opts);
%% Convert to output type
DATA = table2array(DATA);
whos DATA
Name Size Bytes Class Attributes DATA 11x11 968 double
%% Clear temporary variables
clear opts
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!