- 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
Porblem in exporting a txt file
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Stefania Avvedimento
le 20 Oct 2021
Commenté : Stefania Avvedimento
le 21 Oct 2021
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 commentaires
Rik
le 20 Oct 2021
Since your file is a not uniform you have two options:
I personally would go for option 2.
Rik
le 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.
Réponse acceptée
Voss
le 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
Plus de réponses (1)
Cris LaPierre
le 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)
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)
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!