...

/

Read and Write Text Files

Read and Write Text Files

Learn to read and write a text files in MATLAB and Python.

Reading data from a text file

To read a text file in MATLAB, we can use the fscanf() function. We need to set the file with the format specifier %c, which reads characters individually.

Press + to interact
main.m
data.txt
% Open a file for reading
f_obj = fopen('data.txt', 'r');
% Read data from file with actual new lines
data = fscanf(f_obj, '%c');
% Display file data
disp(data);
% Close file
fclose(f_obj);
...