Porblem in exporting a txt file
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 20 Okt. 2021
Since your file is a not uniform you have two options:
- hardcode the line numbers and use the skip header option
- 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
am 20 Okt. 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.
Akzeptierte Antwort
Weitere Antworten (1)
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)
optsB = detectImportOptions("extract.txt",'ReadVariableNames',true);
optsB.VariableNamesLine = 11;
optsB.DataLines = [12, 252];
B1_data = readtable('extract.txt',optsB)
% branch2
optsH.VariableNamesLine = 256;
optsH.DataLines = [257,257];
B2_info = readtable('extract.txt',optsH)
optsB.VariableNamesLine = 260;
optsB.DataLines = [261, 501];
B2_data = readtable('extract.txt',optsB)
Kategorien
Mehr zu Web Services finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!