How to extract specific rows & columns from a text file
Show older comments
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 Comments
Akira Agata
on 25 Mar 2020
Seems that readtable function can do that. If possible, could you upload a small example of your text data?
Farhan K
on 26 Mar 2020
Farhan K
on 26 Mar 2020
Accepted Answer
More Answers (1)
Akira Agata
on 26 Mar 2020
Edited: Akira Agata
on 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 Comments
Akira Agata
on 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
on 26 Mar 2020
Farhan K
on 26 Mar 2020
Akira Agata
on 26 Mar 2020
Edited: Akira Agata
on 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));
Categories
Find more on Text Detection and Recognition in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!