Clear Filters
Clear Filters

How to overwrite matrix in txt file with another matrix?

1 view (last 30 days)
I would like to replace the matrix with the title "//Event" with the variable "Events". My initial solution was to import the txt file and make separated tables so I could overwrite them. Therefore, I tried the following solution https://nl.mathworks.com/matlabcentral/answers/784276-import-several-tables-from-one-txt-file-in-matlab?s_tid=srchtitle with the code below.
var = load(Events.mat);
str = fileread('temp.txt');
tkn = regexp(str,'^/[^\n]([^;]*)([^/]*)','lineanchors','tokens');
I would like to detect with the code above the following tables:
First table named "Title" containing the string
Network defintion and parameters
Second table named "Parameters" with header and numbers:
nrTimePeriods LengthTim LTimeStep ScaleFlow ScaleCap ScaleSpeed DemandPar %header
21 900 10 1.14 1.00 1.00 1 %number
Third table named "Links" with header and numbers:
linknr nettype length nrlanes satflow speed type CTR nrSG Signal(s) nrCL ConfLinks %header
(m) (veh/hr) (km/hr)
1 0 653.8 5 25000 60 0 %number
2 0 580.6 5 25000 50 0 %number
3 0 271.7 5 25000 60 0 %number
4 0 708.5 5 25000 50 0 %number
5 0 496.2 5 25000 60 0 %number
Fourth table named "Events" with header and numbers:
begintime endtime linknr nrlanes satflow vfree type %header
(%) (%)
0 18900 2150 -4 0 100 2 %number
0 18900 2151 -3 0 100 2
0 18900 2149 -3 0 100 2
0 18900 379 -3 0 100 2
0 18900 358 -3 0 100 2
0 18900 349 -3 0 100 2
0 18900 2143 -3 0 100 2
0 18900 2144 -3 0 100 2
  2 Comments
Jan
Jan on 24 Feb 2023
Please explain, why you think, that this is not matching. Share this important information with us. Otherwise it is hard to guess, what you call "better".
Tessa van Kol
Tessa van Kol on 27 Feb 2023
I adjusted my post. I hope this will give a more clear understanding of what I am hoping to achieve.
Refering to your question about what I define as a "better" solution. I thought a better solution would be somthing similar as described in the following post How to change a specific line in a text file?. However, taking into account that I perhapse have to change multiple tables in the text file I think using regexp as stated in my post is a better solution to my problem.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 27 Feb 2023
Assuming that the //Events block always occurs last in the text file. Lets start by looking at the content of the two files:
type temp.txt
//Title Network definition and parameters //Parameters ;nrTimePeriods LengthTim LTimeStep ScaleFlow ScaleCap ScaleSpeed DemandPar 21 900 10 1.14 1.00 1.00 1 //Links ;linknr nettype length nrlanes satflow speed type CTR nrSG Signal(s) nrCL ConfLinks ; (m) (veh/hr) (km/hr) 1 0 653.8 5 25000 60 0 2 0 580.6 5 25000 50 0 3 0 271.7 5 25000 60 0 4 0 708.5 5 25000 50 0 5 0 496.2 5 25000 60 0 //Events ;begintime endtime linknr nrlanes satflow vfree type ; (%) (%) 0 18900 2150 -4 0 100 2 0 18900 2151 -3 0 100 2 0 18900 2149 -3 0 100 2 0 18900 379 -3 0 100 2 0 18900 358 -3 0 100 2 0 18900 349 -3 0 100 2 0 18900 2143 -3 0 100 2 0 18900 2144 -3 0 100 2
S = load('Events.mat');
M = S.Events
M = 2×7
900 1500 2150 -4 0 100 2 900 1500 2151 -3 0 100 2
Now we replace the last data block with that numeric matrix:
L = readlines('temp.txt');
X = find(strncmp(L,';',1),1,'last');
[fid,msg] = fopen('temp_new.txt','wt');
assert(fid>0,msg)
fprintf(fid,'%s\n',L{1:X});
fprintf(fid,'%6g%11g%10g%8g%6g%10g%7g\n',M.');
fclose(fid);
and now lets check the content of the new file:
type temp_new.txt
//Title Network definition and parameters //Parameters ;nrTimePeriods LengthTim LTimeStep ScaleFlow ScaleCap ScaleSpeed DemandPar 21 900 10 1.14 1.00 1.00 1 //Links ;linknr nettype length nrlanes satflow speed type CTR nrSG Signal(s) nrCL ConfLinks ; (m) (veh/hr) (km/hr) 1 0 653.8 5 25000 60 0 2 0 580.6 5 25000 50 0 3 0 271.7 5 25000 60 0 4 0 708.5 5 25000 50 0 5 0 496.2 5 25000 60 0 //Events ;begintime endtime linknr nrlanes satflow vfree type ; (%) (%) 900 1500 2150 -4 0 100 2 900 1500 2151 -3 0 100 2
  1 Comment
Tessa van Kol
Tessa van Kol on 27 Feb 2023
Edited: Tessa van Kol on 8 Mar 2023
Thank you for your solution. I wrote the following code with your solution to overwrite multiple tables in the txt file.
clear;
clc;
dirinf = dir('*.mat');
nfiles = length(dirinf);
L = readlines('temp.txt');
for n = 1:nfiles
infile = dirinf(n).name; % extract one file name from struct
M{n} = importdata(infile); % load the data
infile = infile(1:end-4);
lstring = strlength(infile);
X(n) = (find(strncmp(L,strcat('//',infile),lstring)))+2;
[fid,msg] = fopen('temp_new.txt','wt');
assert(fid>0,msg)
fprintf(fid,'%s\n',L{1:min(X)});
fprintf(fid,'%6g%11g%10g%8g%6g%10g%7g\n',M{n}');
end
However, the results are not stored correctly in the 'temp_new.txt' file due to the following line of code:
fprintf(fid,'%s\n',L{1:min(X)});
@Stephen23 how can I rewrite this line of code that it is stored correctly.

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!