Azzera filtri
Azzera filtri

How to load a .dat file?

7 visualizzazioni (ultimi 30 giorni)
Agustin
Agustin il 20 Ott 2016
Risposto: Walter Roberson il 20 Ott 2016
I'm trying to load a time series data file in Matlab but the importdata command is not working on this type of file. It only gives me the number of rows and columns and I need the whole data to plot the information. Below is a fragment of the dataset since I cannot attach the file because it is huge. I am looking forward for a soon reply.
5979
3705
x[m] -245.4 -253.4 -253.5 -234.9 -235.1 -237.4 -240.6 -240.8 -240.9 -230.4 -230.6
y[m] 1058.0 1092.4 1093.1 1033.5 1034.2 1044.4 1058.4 1059.1 1059.8 1034.5 1035.2
time [mm] [mm] [mm] [mm] [mm] [mm] [mm] [mm] [mm] [mm] [mm]
736512.025532 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
736512.046921 -0.15 -0.38 0.15 -0.16 0.31 2.23 0.12 -0.10 -0.57 0.35 0.41
736512.054051 -1.21 -0.91 -0.59 -2.24 -2.18 -0.06 -1.27 -1.01 -1.36 -2.22 -2.28
736512.061181 0.01 -0.13 0.13 -0.08 -5.10 -2.63 -0.33 0.14 0.61 0.04 -5.31
736512.068310 0.38 -0.04 0.32 0.45 -4.86 -3.59 0.26 0.52 0.24 0.24 -5.18
736512.075440 0.36 -0.44 -0.13 0.33 -5.45 -4.92 -0.46 0.04 0.13 0.66 -5.50
736512.082558 -0.52 -0.37 -0.10 -0.60 -5.78 -4.20 -0.64 -0.37 -0.52 -0.24 -5.96
736512.089688 -0.58 -0.45 0.01 -0.64 -5.42 -4.52 -0.73 -0.46 -0.70 0.13 -5.40
736512.096817 0.14 -0.37 -0.13 -0.59 -5.41 -3.64 -0.48 -0.29 -0.48 0.15 -5.52
736512.103947 0.37 0.28 0.54 0.14 -4.81 -3.42 0.23 0.46 0.17 0.94 -4.89
736512.111076 0.15 -0.20 -0.14 -1.09 -6.28 -4.35 -0.35 -0.09 0.50 -0.40 -5.90
736512.118206 -0.72 -0.31 -0.08 -1.49 -6.38 -4.42 -0.66 -0.64 -1.47 -0.69 -6.34
736512.125336 0.48 -0.15 0.18 0.03 -5.47 -5.14 0.56 0.68 0.76 0.53 -5.42
736512.132465 -0.08 -0.17 0.09 0.18 -5.15 -4.35 -0.33 -0.24 0.07 0.45 -5.26
736512.139595 -0.07 0.00 0.16 -0.05 -5.26 -4.60 -0.10 0.13 -0.08 0.38 -5.24
736512.146725 0.09 -0.20 0.20 -0.17 -5.19 -4.00 -0.21 -0.11 0.35 0.35 -5.31
736512.153854 -0.33 -0.10 0.27 -0.61 -6.06 -4.91 -0.68 -0.52 -2.05 0.08 -5.87
736512.160984 -0.03 -0.34 -0.06 -0.55 -5.79 -4.51 -0.17 -0.15 -0.87 0.02 -5.78
736512.168102 0.56 -0.08 0.26 -0.05 -5.57 -2.85 0.05 0.27 0.49 0.57 -5.31
736512.175231 -0.20 -0.19 0.21 0.19 -5.25 -3.56 -0.46 -0.33 -0.54 0.59 -5.33
736512.182361 0.41 0.07 0.47 0.11 -5.47 -4.06 0.12 0.07 -0.75 0.11 -5.42

Risposte (2)

Massimo Zanetti
Massimo Zanetti il 20 Ott 2016

Walter Roberson
Walter Roberson il 20 Ott 2016
numx = 11;
fid = fopen('YourFile.txt', 'rt');
Line1 = sscanf( fgetl(fid), '%f', [1 1]);
Line2 = sscanf( fgetl(fid), '%f', [1 1]);
xyfmt = ['%*s', repmat('%f', 1, numx) ];
x = sscanf( fgetl(fid), xyfmt, [1 numx]);
y = sscanf( fgetl(fid), xyfmt, [1 numx]);
txyfmt = repmat('%f', 1, numx+1 );
txy = cell2mat( textscan(fid, txyfmt, 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
My use of sscanf( fgetl() ) instead of fscanf() has to do with the place that sscanf() leaves the file positioned once the requested data is finished. In this file format you could get away with using fscanf directly instead right up to the point you are about to do the textscan, but then you need to know whether the HeaderLine to be skipped is going to refer to the [mm] line or if it is going to refer to the empty remainder of the line before that which is where you would be positioned by fscanf after reading the y values.

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by