Clear Filters
Clear Filters

How to extract value from a txt file and replace it

2 views (last 30 days)
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 Comments
Mathieu NOE
Mathieu NOE on 1 Oct 2021
hello Tom
could you share the dat file as well ? or just that fraction of interest ?

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Oct 2021
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 Comments
TOM SALIC
TOM SALIC on 5 Oct 2021
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 on 5 Oct 2021
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);

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!