Clear Filters
Clear Filters

How can I change specific value located in a line in a text file incrementally.

2 views (last 30 days)
I have a value needs to be changed many times. it can be consider as a force and I need to change this force incrementally. I know the line of it(120). For each value I have to run the software with this file ,then go on to the next value. For example the force is 100 KN I need to change this value 10 times. line (120)= Set-20-,3,value.
Many thanks
  1 Comment
Star Strider
Star Strider on 8 Jun 2015
It would help if you attached your code (use the ‘paperclip’ or ‘staple’ icon, then ‘Choose file’ and ‘Attach file’) so we can see what ‘line 120’ is. Without that information it is very difficult help you.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Jun 2015
The response is the same as for your earlier question about updating text files many times. There has been no magic MATLAB call or magic MS Windows call developed in the last week that would allow a text file to be updated in place with text of a potentially different size.
This is a file system limitation on all file systems on all operating systems that MATLAB has ever worked on (except that it could in theory have been done under VMS when MATLAB was just getting started.)
  3 Comments
Walter Roberson
Walter Roberson on 8 Jun 2015
A = regexp( fileread('first.inp'), '\n', 'split');
for Value = linspace(0,20,15) %new values to put in
A{120} = sprintf('%s,%d,%d,%f','Set-', 22, 2, Value); %format new content
fid = fopen('test2.inp', 'w');
fprintf(fid, '%s\n', A{:});
fclose(fid);
%then call the external routine
system(......)
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!