How to extract value from a txt file and replace it

2 次查看(过去 30 天)
Hello everyone,
I would like to replace three specific values ​​in a .dat file (see rectangle in picture attached). The three lines must keep their structures . This code is contained in a loop because after each change I call an .exe file. The problem is that with each loop, some lines are merged suddenly the precise format of the .dat file is changed and the .exe file crashes.
Here is my code :
pitch=[3.83 6.60 8.70 10.45 12.06 13.54 14.92 16.23 17.47 18.70 19.94 ];
filenamePitch='NRELOffshrBsline5MW_OC4DeepCwindSemi_ElastoDyn.dat';
Fid=fopen(filenamePitch);
%recherche des lignes à modifier
data=textscan(Fid,' %s','Delimiter', '\n');
searchValue= strfind([data{1}], 'BlPitch' );
Index = find(not(cellfun('isempty',searchValue)));
fclose(Fid);
%
for ii=1:3
oldvaluePitch{ii}=num2str(data{1}{Index(ii)});
fpitch{ii} = regexprep(oldvaluePitch{ii},'\d+(\.)?(\d+)',num2str(pitch(idx)));
end
% newvaluePitch=num2str(pitch(idx));
% %
% fpitch = regexprep(fpitch,oldvaluePitch,newvaluePitch);
Fid=fopen(filenamePitch,'r+');
for j=1:29
line=fgetl(Fid);
end
line=fgetl(Fid);
fprintf(Fid,'%s',fpitch{1});
line=fgetl(Fid);
fprintf(Fid,'%s',fpitch{2});
line=fgetl(Fid)
fprintf(Fid,'%s',fpitch{3});
fclose(Fid)
end
Thank you in advance for help.

采纳的回答

Mathieu NOE
Mathieu NOE 2021-10-4
hello again
this is a first trial
It will change the numerical value before BlPitch on ach of the 3 lines
but I think we have to improve the code if the balnks and tab must be fully respected , which is not the case yet (already when we do the file reading, so maybe there is a new option to pass to textscan)
at least , the number of blanks (in these 3 lines) between the numerical value and the char array BlPitch are the same between the original file and the new file
also here I don't know how you want to change the values so I simply picked the first one of the new value array. that also has to be updated if needed
pitch=[3.83 6.60 8.70 10.45 12.06 13.54 14.92 16.23 17.47 18.70 19.94 ];
filenamePitch='File.txt';
Fid=fopen(filenamePitch);
%recherche des lignes à modifier
data=textscan(Fid,' %s','Delimiter', '\n');
data=data{1};
newdata=data;
searchValue= strfind(data, 'BlPitch' );
Index = find(not(cellfun('isempty',searchValue)));
fclose(Fid);
idx = 1; % ?? logic of data replacement
%
for ii=1:length(Index)
tmp = data{Index(ii)};
ind = findstr(tmp,'BlPitch');
% focus on first segment
tmp2 = tmp(1:ind-1);
% finding blanks (to keep them)
indbl = findstr(tmp2,' ');
% new char array
nca = [num2str(pitch(idx)) tmp2(indbl) tmp(ind:end)];
% replace cell content
newdata{Index(ii)} = nca;
end
% export
writecell(newdata, 'Fileout.txt',"QuoteStrings",0);
  4 个评论
TOM SALIC
TOM SALIC 2021-10-5
It's work perfectly I just change the last part by :
Fid = fopen(filenamePitch,'w');
fprintf(Fid,'%s\n',newdata{:});
fclose(Fid);
Thank you
Mathieu NOE
Mathieu NOE 2021-10-5
Hi Tom
find some alternatives for reading (using the attached readfile from FEX : readfile - File Exchange - MATLAB Central (mathworks.com)) and exporting with fprintf
try it and let me know
clc
clearvars
pitch=[3.83 6.60 8.70 10.45 12.06 13.54 14.92 16.23 17.47 18.70 19.94 ];
filenamePitch='File.txt';
data=readfile(filenamePitch);
newdata=data;
searchValue= strfind(data, 'BlPitch' );
Index = find(not(cellfun('isempty',searchValue)));
idx = 1; % ?? logic of data replacement
%
for ii=1:length(Index)
tmp = data{Index(ii)};
ind = findstr(tmp,'BlPitch');
% focus on first segment
tmp2 = tmp(1:ind-1);
% finding blanks (to keep them)
indbl = findstr(tmp2,' ');
% new char array
nca = [' ' num2str(pitch(idx)) tmp2(indbl) tmp(ind:end)]; % now includes first tab as in original file
% replace cell content
newdata{Index(ii)} = nca;
end
% export
C = newdata.';
fid = fopen('Fileout4.txt', 'wt');
fprintf(fid, '%s\n', C{:});
fclose(fid);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by