Hello, I have the following script,
%C = readlines('TARGETOLD.txt');
C=readlines('TARGETOLD.txt');
str=char(C);
raw_data = textscan(str,'%[^\n]');
DATA=raw_data{1, 1};
Nrow=numel(DATA);
for i=1:Nrow
idx = find(contains(C,';TIME_ELAPSED:'), 1);
%A=convertStringsToChars(C(1:idx));
%writelines('_modified.txt', A);
fid = fopen('_modified.txt','w');
fprintf(fid,'%s\n',C{1:idx});
D = readlines('HeadScript.txt');
fprintf(fid,'%s\n',D);
%E = readlines('HeadScript.txt');
%fprintf(fid,'%s\n',E);
F = readlines('BottomScript.txt');
fprintf(fid,'%s\n',F);
end
in particular I would like some advice regarding the rows
for i=1:Nrow
idx = find(contains(C,';TIME_ELAPSED:'), 1);
...
end
using idx I go to recognize the string "TIME_ELAPSED" only the first time I encounter it, while I would like to recognize it several times since it appears several times in the text file. as you can see, I thought to insert a for loop on all the number of lines (Nrow) and to make idx depend on the index i, but it doesn't work!
thank you for the advice

Antworten (2)

dpb
dpb am 29 Okt. 2022
Bearbeitet: dpb am 29 Okt. 2022

0 Stimmen

No loop needed, at least initially...
C=readlines('TARGETOLD.txt');
idx=contains(C,';TIME_ELAPSED:');
E=C(idx);
Now, what do you want/need to do with those records?
More than likely if you would provide a sample of the input data file and explain your overall objective someone can provide a much more elegant solution than reading the file in as the string array...

6 Kommentare

Federico Paolucci
Federico Paolucci am 29 Okt. 2022
ok thanks, in practice I have to recognize and then save on the empty text file "_modified.txt" all the lines of the text file "TARGETOLD.txt" included between the various strings '; TIME_ELAPSED;' (they repeat in the targetold file) and insert at the end of each time elapsed "HeadScript" and "BottomScript" always inside _modified.txt. only that, by also inserting the lines you kindly provided me, only the strings '; TIME_ELAPSED;' are saved in the _modified , without the lines that should be present between them, and the scripts head and bottom I find them only at the end of the file and not at the end of each '; TIME_ELAPSED;'
I am attaching the file that comes to me as a result, but it is wrong as I said
dpb
dpb am 29 Okt. 2022
Well, of course E contains only the lines containing the looked-for string -- that's what you're initial question said you were looking for, so that's what you got. :)
As said, if we had an example of the content of the ORIGINAL "targetold.txt" file to look at and then a description of what to do with it, then we would have a chance of solving the actual problem.
Without the original, the description given makes no sense at all...we don't know what all those other things are nor in what form they're contained in the file.
Make a small(ish) example file that illustrates to it is possible to examine and provide what the output for the specific file should be is the simplest way to get other folks who don't "know nuthink!" about what your objective is. The size of the example file has no bearing on the logic as long as it is representative of the whole file.
Federico Paolucci
Federico Paolucci am 30 Okt. 2022
Yes, exactly, maybe I didn't explain myself well at the beginning. anyway I attach the source file
What I would like as a result is to copy, from this "target old" file into another empty text file called "_modified.txt", all lines up to "; TIME_ELAPSED:". At this point, immediately after this string, my intent is to insert the contents of the "HeadScript" and "BottomScript" files. I want to apply this process every time I encounter the string"; TIME_ELAPSED:" in the original text file, as this string repeats several times in the file itself
dpb
dpb am 30 Okt. 2022
"mmediately after this string, my intent is to insert the contents of the "HeadScript" and "BottomScript" files."
You mean that literally? Then those two might as well be one file if they are inserted together...there isn't anything between the two?
Federico
Federico am 30 Okt. 2022
Yes I mean that literally, there's nothing between them
dpb
dpb am 30 Okt. 2022
Bearbeitet: dpb am 30 Okt. 2022
In that case, this is "more better" suited for a filter approach...see the new Answer for code...

Melden Sie sich an, um zu kommentieren.

dpb
dpb am 30 Okt. 2022
Bearbeitet: dpb am 31 Okt. 2022

0 Stimmen

fidI=fopen('TARGETOLD.txt'); % Input file
fidO=fopen('_modified.txt','w'); % Output file
fid=fopen('HeadScript.txt'); % header file
HDR=fread(fid,'*char'); % suck up the header
fid=fclose(fid);
fid=fopen('BottomScript.txt'); % ditto for the trailer
TLR=fread(fid,'*char');
fid=fclose(fid);
% now do the work...
while ~feof(fidI)
l=fgets(fidI); % read a line
fwrite(fidO,l); % and put in output
if contains(l,';TIME_ELAPSED:') % the magic phrase
fwrite(fidO,HDR); % add the new stuff
fwrite(fidO,TLR);
end
end
fclose all
clear fid*

Kategorien

Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Gefragt:

am 29 Okt. 2022

Bearbeitet:

dpb
am 31 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by