How to extract some information from a txt file
1 view (last 30 days)
Show older comments
Stefania Avvedimento
on 4 Mar 2021
Commented: Star Strider
on 19 Oct 2021
Hi all,
I am facing with an issue on how to extract some information from a txt file in a matrix format. Find attached the txt file.
So lets say I want to extract the values of cNH3 over time in a matrix format (let's call matrix 1) and then the values of cNH2CL over time (matrix 2) for nodes ID Node 10, Node 40,Node 265 (not consecutive nodes), how could I do?
Actually I need these information for more than three nodes, lets say for i ID nodes.
Thanks in advance,
Stefania
0 Comments
Accepted Answer
Star Strider
on 4 Mar 2021
Try this:
fidi = fopen('net3 report.txt');
for k = 1:26
getline = fgetl(fidi);
end
k1 = 1;
while ~feof(fidi)
NN = regexp(fgetl(fidi),'\d*', 'match');
if (isempty(NN)) || (numel(NN) > 1)
break
else
NodeNr(k1,:) = str2double(NN{:});
secread{k1,:} = textscan(fidi,'%s%f%f', 'HeaderLines',4, 'CollectOutput',1);
secread{k1,:}{1} = datetime(secread{k1}{1}, 'InputFormat','mmm:ss','Format','mmm:ss');
k1 = k1 + 1;
end
end
fclose(fidi);
.
10 Comments
Star Strider
on 19 Oct 2021
I am lost.
I am not certain what you want, and I do not understand ‘example results.png’.
The data only imported for ‘Chlorine’ anyway, and for whatever reason readtable did not import the ‘Pipe results’ columns. (Those would require a different appproach to read.)
So in the end, ‘Time_r’, ‘EPANET_r’, and ‘WUDESIM_r’, are all for ‘Chlorine’. The ‘Junc_ID’ then represents each column of the resulting matrices. That code would be essentially the same regardless, because the results would be essentially the same. To get the matrices for the column data to be row vectors instead, simply transpose them.
With respect to 'VariableNamingRule', either substitute that with 'ReadVariableNames',true, or just omit it. Not all versions of readtable support those options.
.
More Answers (0)
See Also
Categories
Find more on Data Import and Export 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!