Porblem in exporting a txt file

1 view (last 30 days)
Hi all,
I have a .txt file (attached) that I want to extract in a matrix format. The .txt file contains the values of Chlorine Results (just ignore the Pipe Results) for only two junctions: Junc_ID 1 and Junc_ID 5 (see row 7-column 37 and row 256-column 37 respectively). The file is reduced in size so I'd like to get the results for N Junc_ID.
So lets say I want to extract the values of Chlorine results over time for EPANET column in a matrix format (let's call matrix 1) and then the values of Chlorine results over time for WUDESIM column (matrix 2) for these jucntions, how could I do?
Find attached the figure showing the format of results I'd like to get.
Anyway I tried the readtable function:
T = readtable('extract.txt','ReadVariableNames', true) and
T= readtable('extract.txt','VariableNamingRule','preserve') in MatlabR2018b but it does not working.
I'd appreciate if anyone could help me.
Thanks,
Stefania
  2 Comments
Rik
Rik on 20 Oct 2021
Since your file is a not uniform you have two options:
  1. hardcode the line numbers and use the skip header option
  2. read your file as text and do the data-extraction yourself with tools like textscan and str2double
I personally would go for option 2.
Rik
Rik on 20 Oct 2021
You could use that, but I would suggest reading your file all at once.
If you are using a new release you can use data=cellstr(readlines('extract.txt'));
If you're using R2020a or older you can use my readfile function, which you can get from the FEX (or the AddOn-manager if you are using R2017a or later).
Then you have a cell array of character vectors, allowing you to use normal for-loops.

Sign in to comment.

Accepted Answer

Voss
Voss on 20 Oct 2021
Here's one way. This will collect the first three columns of each section into a cell array called data, each element of which corresponds to one junction, so that data{i}(:,j) is the jth column of the ith junction in the file, e.g., in this case, data{2}(:,3) is WUDESIM at junction 5 and data{1}(:,1) is Time at junction 1.
fid = fopen('extract.txt','r');
str = char(fread(fid).');
fclose(fid);
idx_s = strfind(str,'ResT_WUDESIM');
idx_e = strfind(str,'*********************************************************************************************');
idx_s = idx_s+16;
idx_e = idx_e(3:end)-5;
data = {};
for i = 1:numel(idx_s)
temp = str2num(str(idx_s(i):idx_e(i)));
data{i} = temp(:,1:3);
end
  1 Comment
Stefania Avvedimento
Stefania Avvedimento on 21 Oct 2021
Thanks Benjamin, it is exactly what I wanted in a short and fast code. Could you just explain me what the following lines refer to?
idx_s = idx_s+16;
idx_e = idx_e(3:end)-5
Thanks a lot,
Stefania

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 20 Oct 2021
Here's how I would load the data using option 1. I'll let you worry about shaping the data to make the table you want.
% branch1
optsH = detectImportOptions("extract.txt",'ReadVariableNames',true);
optsH.DataLines = [8, 8];
optsH.VariableNamesLine = 7;
optsH = setvartype(optsH,1,"string");
B1_info = readtable('extract.txt',optsH)
B1_info = 1×7 table
Branch_ID Pipe_ID Junc_ID N_segments Flow_Corr Disp_Corr Rw_Corr _________ _______ _______ __________ _________ _________ _______ "B1" 1 1 1 1 1 {'1'}
optsB = detectImportOptions("extract.txt",'ReadVariableNames',true);
optsB.VariableNamesLine = 11;
optsB.DataLines = [12, 252];
B1_data = readtable('extract.txt',optsB)
B1_data = 241×6 table
Time EPANET WUDESIM Reyn_WUDESIM Pecl_WUDESIM ResT_WUDESIM ____ __________ _______ ____________ ____________ ____________ 0 0 0 266.73 4.228 72834 1 7.4449e-18 0 264.46 4.2642 73457 2 3.4526e-13 0 263.06 4.287 73850 3 6.6482e-11 0 262.69 4.293 73954 4 2.086e-09 0 261.98 4.3047 74155 5 2.0398e-08 0 262.01 4.3041 74145 6 9.8362e-08 0 264.35 4.266 73489 7 3.2544e-07 0 270.9 4.1628 71711 8 9.3243e-07 0 273.69 4.1204 70980 9 2.364e-06 0 273.35 4.1256 71070 10 4.9397e-06 0 272.98 4.1311 71165 11 8.6201e-06 0 272.63 4.1364 71257 12 1.3135e-05 0 272.58 4.1372 71269 13 1.8303e-05 0 272.8 4.1338 71212 14 2.4263e-05 0 273.02 4.1305 71154 15 3.1836e-05 0 272.17 4.1435 71378
% branch2
optsH.VariableNamesLine = 256;
optsH.DataLines = [257,257];
B2_info = readtable('extract.txt',optsH)
B2_info = 1×7 table
Branch_ID Pipe_ID Junc_ID N_segments Flow_Corr Disp_Corr Rw_Corr _________ _______ _______ __________ _________ _________ _______ "B2" 4 5 1 1 1 {'1'}
optsB.VariableNamesLine = 260;
optsB.DataLines = [261, 501];
B2_data = readtable('extract.txt',optsB)
B2_data = 241×6 table
Time EPANET WUDESIM Reyn_WUDESIM Pecl_WUDESIM ResT_WUDESIM ____ __________ __________ ____________ ____________ ____________ 0 0 0 670.46 1.5641 26944 1 5.6972e-16 0 596.61 1.7577 30279 2 1.3406e-11 0 554.02 1.8928 32607 3 1.6262e-09 0 543.24 1.9304 33253 4 3.6212e-08 0 522.72 2.0061 34559 5 2.4877e-07 0 523.75 2.0022 34491 6 9.1806e-07 0 593.04 1.7683 30461 7 2.6721e-06 0 832.07 1.2603 21710 8 8.2654e-06 0 974.14 1.0765 18544 9 2.0306e-05 0 954.02 1.0992 18935 10 3.6134e-05 0 933.7 1.1231 19347 11 5.2998e-05 0 914.85 1.1462 19746 12 6.8857e-05 0 912.35 1.1494 19800 13 8.6268e-05 0 924 1.1349 19550 14 0.00010516 0 935.92 1.1204 19302 15 0.00014593 0.00011649 891.38 1.1764 20266

Community Treasure Hunt

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

Start Hunting!