How to extract specific rows & columns from a text file
Afficher commentaires plus anciens
I need to extract all the net names and toggle rates from a text file. total 179456 nets. Below screenshot is the few row and columns shown as example

3 commentaires
Akira Agata
le 25 Mar 2020
Seems that readtable function can do that. If possible, could you upload a small example of your text data?
Farhan K
le 26 Mar 2020
Farhan K
le 26 Mar 2020
Réponse acceptée
Plus de réponses (1)
Akira Agata
le 26 Mar 2020
Modifié(e) : Akira Agata
le 26 Mar 2020
Thank you for uploading an example. How about the following?
% Read from text data
T = readtable('Example.txt','HeaderLines',4,'Format','%s%f%f%f%f');
T.Properties.VariableNames = {'Net','NetLoad','StaticProb','ToggleRate','SwPower'};
% Extract NetLoad and ToggleRate
T = T(:,{'Net','ToggleRate'});
4 commentaires
Akira Agata
le 26 Mar 2020
To find out a root-cause, more details is needed. Is it possible to upload your original data file or more large sample file (such as ~1000 lines) ?
Farhan K
le 26 Mar 2020
Farhan K
le 26 Mar 2020
Akira Agata
le 26 Mar 2020
Modifié(e) : Akira Agata
le 26 Mar 2020
Thank you for attaching the data.
OK. Looking at your data, I found some irregular lines and needs to some pre-processing.
How about the following?
% Read from text data
C = readcell('switching_prob_report.txt','Delimiter','\r\n','NumHeaderLines',612);
% Delete the last 3 lines (because they don't contain data)
C(end-2:end) = [];
% Detect irregular lines (which does not end with number)
idx = ~endsWith(C,compose('%d',0:9));
% Delete characters at the end of the detected lines
C(idx) = regexprep(C(idx),'\s*[^\d]$','');
% Split the line
D = split(C);
% Extract 1st and 4th column
Net = D(:,1);
ToggleRate = str2double(D(:,4));
Catégories
En savoir plus sur Text Detection and Recognition 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!