필터 지우기
필터 지우기

problem to edit a text file

조회 수: 1 (최근 30일)
sareh
sareh 2014년 5월 20일
댓글: sareh 2014년 5월 21일
Dear all,
I am having some difficulties to edit my text file. I have an input file which I need to modify certain lines (11844 lines) in each iteration. The input file is a mixture of text, numbers. the numbers are the coordinates of my object in 3D space. So I open the file and rewrite the new coordinates but when it writes it down it pushes the have of previous coordinates and just replace half of it with my new data. So at the end I have correctly started place for my new points but then at the end I see some residue of the previous coordinates.
find_line = '*Node';
InputFID=fopen([file,'.inp'],'r+');
j = 0; count=0;
while j==0
count = count+1;
tline = fgetl(InputFID);
j = strcmp(tline, find_line);
end
n = 1;
format = ' %5.0f, %.4f, %.4f, %.4f \n';
fseek(InputFID,0, 'cof');
for n = 1:size(newdata,1)
m = [newdata(n,1), newdata(n,2), newdata(n,3), newdata(n,4)];
fprintf(InputFID,format,m);
end
fclose(InputFID);
I will appreciate any idea, Thanks Sareh
  댓글 수: 3
per isakson
per isakson 2014년 5월 20일
My first idea is
str = fileread( [file,'.inp'] );
str = regexprep( str, ...
and hope there are keywords in the file to locate the coordinates of interest
sareh
sareh 2014년 5월 21일
Dear all,
Thank you very much, I made it work by:
A = regexp( fileread('data.txt'), '\n', 'split');
then copy and paste the rows I wanted to change in this array then wrote it out:
fid = fopen('data.txt', 'w');
for i = 1:size(A1,2)
fprintf(fid, '%s\n', A{i});
end
fclose(fid);
Thank you very much, My best regards, Sareh

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by