1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| % This example differs from Example 3 in that it tries to
% squeeze out any extra characters if the new
% line is shorter than the line it is replacing.
% Opening the file to both read and write
fid = fopen('applicate.m','r+');
% Can change 'loc' to insert data at any line in file.
% When loc=1, data will inserted at line 2
loc = 1;
for i = 1:loc
% Used FGETL to move file pointer a whole line at a time:
% See FGETL section below for more information
temp_line = fgetl(fid);
if ~ischar(temp_line), break, end
end;
if ischar(temp_line)
p = ftell(fid)
temp_line = fgetl(fid);
if ischar(temp_line)
% Read in the rest of the file after line of interest
% with FREAD
c = fread(fid,inf,'uchar');
% Place the internal pointer back to the line of interest
fseek(fid,p,'bof');
% Print the last_line data to the current position
fprintf(fid,'%s','function out=applicate(fem)');
% Write the rest of the data in the file with FWRITE
fwrite(fid,c,'uchar');
else
fprintf(fid,'%s','function out=applicate(fem)');
end
else
fprintf('Not enough lines in file to skip. Nothing appended...\n');
end
fclose(fid); |
Partager