Replacing one line (or one part of a line) in a text file
Hello. I have a config file that contains 1 specific line that I want to change just the number (in this case from 1000 to e.g. 235)
This is my code so far:
%Get current Value
FilePath=’C:Program FilesTeledyne DALSASaperaCamFilesUsermyCamera.ccf’
Filedata = readlines(FilePath)
TF = contains(Filedata,"Time Integrate Duration=")
k = find(TF,1);
expStr=Filedata(TF);
ReportMessage(app,expStr); %My report function that reports to a UITextarea
oldLine=Filedata(k)
Filedata(k) ="Time Integrate Duration=235" %this is the new line I want
[fid, msg] = fopen(‘FilePath’, ‘w’)
if fid < 1;
error(‘could not write output file because "%s"’, msg);
end
fwrite(fid, strjoin(Filedata, ‘n’));
fclose(fid);
But it doesn’t seem to be changing the file
I also enclose the config file (text)Hello. I have a config file that contains 1 specific line that I want to change just the number (in this case from 1000 to e.g. 235)
This is my code so far:
%Get current Value
FilePath=’C:Program FilesTeledyne DALSASaperaCamFilesUsermyCamera.ccf’
Filedata = readlines(FilePath)
TF = contains(Filedata,"Time Integrate Duration=")
k = find(TF,1);
expStr=Filedata(TF);
ReportMessage(app,expStr); %My report function that reports to a UITextarea
oldLine=Filedata(k)
Filedata(k) ="Time Integrate Duration=235" %this is the new line I want
[fid, msg] = fopen(‘FilePath’, ‘w’)
if fid < 1;
error(‘could not write output file because "%s"’, msg);
end
fwrite(fid, strjoin(Filedata, ‘n’));
fclose(fid);
But it doesn’t seem to be changing the file
I also enclose the config file (text) Hello. I have a config file that contains 1 specific line that I want to change just the number (in this case from 1000 to e.g. 235)
This is my code so far:
%Get current Value
FilePath=’C:Program FilesTeledyne DALSASaperaCamFilesUsermyCamera.ccf’
Filedata = readlines(FilePath)
TF = contains(Filedata,"Time Integrate Duration=")
k = find(TF,1);
expStr=Filedata(TF);
ReportMessage(app,expStr); %My report function that reports to a UITextarea
oldLine=Filedata(k)
Filedata(k) ="Time Integrate Duration=235" %this is the new line I want
[fid, msg] = fopen(‘FilePath’, ‘w’)
if fid < 1;
error(‘could not write output file because "%s"’, msg);
end
fwrite(fid, strjoin(Filedata, ‘n’));
fclose(fid);
But it doesn’t seem to be changing the file
I also enclose the config file (text) readlines, contains, fwrite MATLAB Answers — New Questions