| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
tline = fgets(fileID)
tline = fgets(fileID, nchar)
tline = fgets(fileID) reads the next line of the specified file, including the newline characters. fileID is an integer file identifier obtained from fopen. tline is a text string unless the line contains only the end-of-file marker. In this case, tline is the numeric value -1. fgets reads characters using the encoding scheme associated with the file. To specify the encoding scheme, use fopen.
tline = fgets(fileID, nchar) returns at most nchar characters of the next line. tline does not include any characters after the newline characters or the end-of-file marker.
Read and display the file fgets.m. Because fgets keeps newline characters and disp adds a newline character, this code displays the file with double-spacing:
fid = fopen('fgets.m');
tline = fgets(fid);
while ischar(tline)
disp(tline)
tline = fgets(fid);
end
fclose(fid);
Compare these results to the fgetl example, which replaces the calls to fgets with fgetl.
fclose | ferror | fgetl | fopen | fprintf | fread | fscanf | fwrite
![]() | fgetl (serial) | fgets (serial) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |