How do I extract data from a .dat file contain multiple tables?

The example of .dat file is this one . There are various tables for varying RPM of the propellers. I want to be able to extract all the data such that I can make various plots from the data for each RPM.
A direct importdata('PER3_5X5E.dat') puts all the data in one single cell without distributing the data in any manner. This was also the case when I removed all the header text from the .dat file and the file started directly with the RPM value.

 Accepted Answer

This reads entire header into cell array hdr, all of the data tables into a cell array out, and the corresponding RPM into a numeric vector rpm. Obviously you can adapt it to your needs.
fnm = 'PER3_105x45.dat';
hdr = {};
fid = fopen(fnm,'rt');
% Read header into cell array:
while ~feof(fid)
pos = ftell(fid);
str = strtrim(fgetl(fid));
if strncmp(str,'PROP RPM',8)
fseek(fid,pos,'bof');
break
else
hdr{end+1} = str; %#ok<SAGROW>
end
end
% Read tables into cell array:
out = {};
rpm = [];
fmt = repmat('%f',1,8);
hmt = repmat('%s',1,8);
opt = {'HeaderLines',3, 'MultipleDelimsAsOne',true, 'CollectOutput',true};
while ~feof(fid)
% Read PROP RPM:
rpm(end+1) = fscanf(fid,' PROP RPM = %d'); %#ok<SAGROW>
% Read table of data:
C = textscan(fid,fmt,opt{:});
out(end+1) = C(1); %#ok<SAGROW>
end
fclose(fid);
Where the k-th table in out corresponds the k-th RPM in the vector rpm. For example:
>> rpm
rpm =
Columns 1 through 8
1000 2000 3000 4000 5000 6000 7000 8000
Columns 9 through 16
9000 10000 11000 12000 13000 13999 15000 16000
Columns 17 through 23
17000 18000 19000 20000 21000 22000 23000
>> out{end} % corresponds to rpm(end)
ans =
4.9 0.02 0.0248 0.089 0.0763 9.527 26.107 18.226
9.7 0.04 0.0515 0.0894 0.0739 9.23 25.294 18.313
14.6 0.06 0.078 0.089 0.0728 9.099 24.933 18.223
19.5 0.09 0.1073 0.0883 0.0701 8.758 23.999 18.092
24.3 0.11 0.1387 0.0876 0.0673 8.404 23.028 17.952
29.2 0.13 0.1725 0.0869 0.0643 8.037 22.022 17.796
34.1 0.15 0.2095 0.086 0.0612 7.647 20.955 17.625
39 0.17 0.249 0.0852 0.0583 7.277 19.941 17.446
43.8 0.19 0.2929 0.0839 0.0549 6.86 18.798 17.195
48.7 0.21 0.335 0.082 0.0521 6.508 17.835 16.789
53.6 0.23 0.3747 0.0796 0.0497 6.211 17.021 16.297
58.4 0.26 0.4094 0.0769 0.048 5.993 16.421 15.744
63.3 0.28 0.4402 0.0741 0.0466 5.818 15.943 15.171
68.2 0.3 0.4693 0.0711 0.0452 5.644 15.465 14.57
73 0.32 0.4975 0.068 0.0436 5.452 14.939 13.924
77.9 0.34 0.5207 0.0647 0.0424 5.29 14.497 13.259
82.8 0.36 0.5348 0.0614 0.0416 5.194 14.233 12.584
87.6 0.38 0.5579 0.0579 0.0398 4.967 13.61 11.855
92.5 0.4 0.5749 0.0537 0.0378 4.719 12.931 10.997
97.4 0.43 0.5852 0.0489 0.0356 4.444 12.176 10.013
102.3 0.45 0.5914 0.0438 0.0331 4.137 11.337 8.973
107.1 0.47 0.592 0.0386 0.0305 3.816 10.456 7.907
112 0.49 0.5845 0.0333 0.0279 3.485 9.55 6.821
116.9 0.51 0.5678 0.0279 0.0252 3.142 8.61 5.724
121.7 0.53 0.5372 0.0226 0.0223 2.791 7.649 4.619
126.6 0.55 0.4864 0.0171 0.0195 2.431 6.661 3.502
131.5 0.57 0.4012 0.0117 0.0167 2.092 5.733 2.394
136.3 0.6 0.2549 0.0062 0.0144 1.805 4.945 1.265
141.2 0.62 -0.0155 -0.0003 0.0118 1.474 4.039 -0.061
>>
tested on this file:

More Answers (1)

Read the full file into character array and then search for the RPM line; that will give you a set of sections within which to interpret the numeric values into cell arrays by RPM.
Did the importdata solution save the RPM values in a cell? If so, looks like you should be able to just find the V==0.0 data to locate the sections similarly as to the text search above and reformat the big cell array into smaller ones. If the RPM data were lost entirely, then you'd need to reconstruct that of course.
Alternatively, just start a loop and fgetl a line at a time and parse it -- throw away what you get until RPM, read it, you then know how many lines to skip to read a section and "rinse and repeat" until feof

Categories

Asked:

on 11 Jun 2017

Edited:

on 11 Jun 2017

Community Treasure Hunt

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

Start Hunting!