Extracting column and specific sorted row data from a large text file
Afficher commentaires plus anciens
Hello everybody,
I have a large text file of 4Gb. I wanted to import the file into MATLAB and extract certain column and row.
It is required to extract the data corresponding to Time=86400. For e.g., Line 5 should be skipped and Line 17 should be included.
In Line 17, the cell number (1) and slope angle (14.6) along with Z corresponding to the lowest positive value of P (in several cases, a set of both positive and negative values of P are seen) should be selected.
I have attached a sample file.
The final output will look like:
Cell_number Slope_Angle Z P
1 14.6 0.10000E-02 0.93631E-03
2 14.4 0.10000E-02 0.93631E-03
Thanks a lot !!
Réponse acceptée
Plus de réponses (1)
Are Mjaavatten
le 11 Déc 2017
fid = fopen('FILE_MATLAB.txt');
Cell_number = [];
Slope_Angle = [];
Z = [];
P = [];
for i = 1:4 % Skip header lines
tline = fgetl(fid);
end
while ~feof(fid)
s = textscan(fid,'%f',4);
d = textscan(fid,'%f %f %f %f %f %f',11,'CollectOutput', 1);
time = s{1}(4);
if abs(time-86400) < 1e-5
Cell_number = [Cell_number;s{1}(1)];
data = d{1};
[~,rowno] = min(data(:,2));
Slope_Angle = [Slope_Angle;s{1}(2)];
Z = [Z;data(rowno,1)];
P = [P;data(rowno,5)];
end
end
fclose(fid);
fprintf('%12s %12s %12s %12s\n','Cell_number','Slope_Angle','Z','P');
for i = 1:length(Z)
fprintf('%12d %12g %12.2e %12.4e\n',Cell_number(i),Slope_Angle(i),...
Z(i),P(i));
end
1 commentaire
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!