MATLAB Answers

Reading and Replace lines in input data for many lines

2 views (last 30 days)
Ivan Mich
Ivan Mich on 12 Apr 2020
Commented: Stijn Haenen on 13 Apr 2020
Hello,
I have a problem with a code.
Imagine that I have File3.txt, File2.txt and one .exe file (lets call it my.exe). File3.txt has my data, File2.txt is the input of my.exe(input that is needed in order to execute my.exe).
I would like from file3 to take only one line and put in a specific line of the file2(input). After putting this line in file2 I can run my exe file. The problem is that File 3 obtain many lines. I want to use o for loop in order to run my .exe file for all the lines separately of file3 (I mean I would like to take the first line of 2nd and 3rd column from file 3, put this line to file2, run my.exe file and take my results. After that I would like to take the second line of 2nd and 3rd column from file 3, put this line to file2, run my.exe file and take my results. After all this repeatability I would like to the last line of 2nd and 3rd column of file 3 put this line to file2, run my.exe file and take my results.)
I have to mention that I want after each repeatability the name of input file (File2.txt) does not change, inspite of the different content.
%for
% for j= 1: size{lines_to_replace_with,1}
%
% line_to_replace_withnew = sprintf('%g\t%g',lines_to_replace_with(j));
% end
t3 = readtable('Files3.txt', 'readvariablenames', true);
nrow = size(t3,1);
lines2 = regexp(fileread('File2.txt'), '\r?\n', 'split') .';
for K = 1 : nrow
line_to_replace_with = sprintf('%g\t%g',t3{K,2:3});
replaced_lines = [lines2(1:2); line_to_replace_with; lines2(4:end)];
newfilename = sprintf('newfile%d.txt', K);
fid = fopen(newfilename, 'wt');
fprintf(fid, '%s\n', replaced_lines{:});
fclose(fid);
end
system('my.exe <File2.txt')
Could you please help me?

  3 Comments

Rik
Rik on 12 Apr 2020
How would you write this code to insert 1 line from files3 and run your program (and how can you retrieve the results so they don't get overwritten)? Then we can more easily help you to write the complete loop.
Ivan Mich
Ivan Mich on 13 Apr 2020
I have a subroutine that making the calculations I want. I don;t know if I could do this by not overwritte every results by each line.
Rik
Rik on 13 Apr 2020
What code would you write for one line from files3? Then it should be relatively easy to extend it.
t3=function_to_read_to_cell;
data=cell(size(t3))
for n=1:numel(t3)
function_to_write_file2(t3{n});
system('my.exe <File2.txt')
data(n)=function_to_read_back_results;
end

Sign in to comment.

Answers (1)

Stijn Haenen
Stijn Haenen on 13 Apr 2020
What about creating a new txt file in every loop and overwriting the existing file2.txt instead of replacing lines?

  2 Comments

Ivan Mich
Ivan Mich on 13 Apr 2020
How I could do this? Could you explain me please?
Stijn Haenen
Stijn Haenen on 13 Apr 2020
Here is an example:
text_example='test';
number_example=10;
txt_file=fopen(sprintf('File2.txt'),'wt');
fprintf(txt_file,'%s\n','test0'); %writing text, \n means new rule
fprintf(txt_file,'%s\n',text_example);
fprintf(txt_file,'%g\n',3); %writing number
fprintf(txt_file,'%g\n',number_example);
fclose(txt_file);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!