Borrar filtros
Borrar filtros

How to extract value from a txt file and replace it

6 visualizaciones (últimos 30 días)
TOM SALIC
TOM SALIC el 1 de Oct. de 2021
Comentada: Mathieu NOE el 5 de Oct. de 2021
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 comentarios
Mathieu NOE
Mathieu NOE el 1 de Oct. de 2021
hello Tom
could you share the dat file as well ? or just that fraction of interest ?
TOM SALIC
TOM SALIC el 1 de Oct. de 2021
Hello Mathieu,
Yes I can share the file

Iniciar sesión para comentar.

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 4 de Oct. de 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 comentarios
TOM SALIC
TOM SALIC el 5 de Oct. de 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 el 5 de Oct. de 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);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by