How to extract some information from a txt file

1 view (last 30 days)
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

Accepted Answer

Star Strider
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
Stefania Avvedimento
Stefania Avvedimento on 19 Oct 2021
Hi Star,
sorry if I could not explain the aim of my work. To give you the idea I attach below a figure in which I show you how I'd like to get the results. So, starting from the extract.txt file (which is reduced in size and contains only 2 branch_IDs, let's imagine I have n branch_ID) I would like to get two matrices:
-Mtx1 matrix containing the values of Chlorine (mg/L) over time of EPANET for all Junc_ID
-Mtx2 matrix containing the values of Chlorine (mg/L) over time of WUDESIM for all Junc_ID
I am not interest in getting the Pipe results.
Is there any way to develop a code to automate the process of extraction?
Anyway, when I run T1 = readtable('extract.txt', 'VariableNamingRule','preserve') Matlab gives me this error: Invalid parameter name: VariableNamingRule.
Thanks,
Stefania
Star Strider
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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!