How to extract value from a txt file and replace it

조회 수: 1(최근 30일)
TOM SALIC
TOM SALIC 2021년 10월 1일
댓글: Mathieu NOE 2021년 10월 5일
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.
  댓글 수: 2
TOM SALIC
TOM SALIC 2021년 10월 1일
Hello Mathieu,
Yes I can share the file

댓글을 달려면 로그인하십시오.

채택된 답변

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
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개)

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by