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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposte (1)

Andrew
Andrew il 14 Lug 2014
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

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by