Open a non empty file, go to a specific row and column and edit(overwrite)

조회 수: 2 (최근 30일)
Yash
Yash 2014년 7월 14일
답변: Andrew 2014년 7월 14일
Hello, I want to open a file (from command line), say 'cylds.bc', then navigate to a specific line and edit that line. These are the contents of the file:
# BC_SLIPW
PATCH 1 1
NAME SolidWall
#
say i wanted to go to second line and change the number 1 1 to 2 1 . How can I do that?

답변 (1개)

Andrew
Andrew 2014년 7월 14일
you could do something with textscan and strncmp
for instance:
fid = fopen('cylds.bc','r+'); %open file for reading and writing
data=textscan(fid,'%s') %read file as string
data=data{1}; %for some reason maybe someone can explain you need to do this to get to the data
data(strncmp('PATCH',data,1)) = sprintf('%5s %5i %i','PATCH',2,1);
fprintf(fid,'%s',data); %write the data to the file
fclose(fid); %close file
Please note that I have not tested this so there may be a typo

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by