How to extract specific rows & columns from a text file
Ältere Kommentare anzeigen
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 Kommentare
Akira Agata
am 25 Mär. 2020
Seems that readtable function can do that. If possible, could you upload a small example of your text data?
Farhan K
am 26 Mär. 2020
Farhan K
am 26 Mär. 2020
Akzeptierte Antwort
Weitere Antworten (1)
Akira Agata
am 26 Mär. 2020
Bearbeitet: Akira Agata
am 26 Mär. 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 Kommentare
Akira Agata
am 26 Mär. 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
am 26 Mär. 2020
Farhan K
am 26 Mär. 2020
Akira Agata
am 26 Mär. 2020
Bearbeitet: Akira Agata
am 26 Mär. 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));
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!