How to extract some information from a txt file

1 回表示 (過去 30 日間)
Stefania Avvedimento
Stefania Avvedimento 2021 年 3 月 4 日
コメント済み: Star Strider 2021 年 10 月 19 日
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

採用された回答

Star Strider
Star Strider 2021 年 3 月 4 日
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 件のコメント
Stefania Avvedimento
Stefania Avvedimento 2021 年 10 月 19 日
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 2021 年 10 月 19 日
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.
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by