MATLAB: Copy a line from a txt to other txt

txt tline fgets copy

Hello:
I have this code witch copy all lines of a file(fid01)to other file(fid02)
tline = fgetl(fid01);
while ischar(tline)
disp(tline)
fprintf(fid02,'%s\r\n',tline);
tline = fgetl(fid01);
end
fclose(fid01);
fclose(fid02);
How I can change this code to copy only the third line of fid01 to fid02???
Thanks

Best Answer

Hi!
count = 1;
tline = fgetl(fid01);
while ischar(tline)
count = count + 1;
disp(tline)
if count == 3
fprintf(fid02,'%s\r\n',tline);
end
tline = fgetl(fid01);
end
fclose(fid01);
fclose(fid02);
You can do as well
for n = 1:3
tline = fgetl(fid01);
end
fprintf(fid02,'%s\r\n',tline);
fclose(fid01);
fclose(fid02);