To parse a text file in MATLAB, you can use the fscanf
or textscan
functions. Here is a simple example showing how to use textscan
to read in comma-separated values (CSV) from a text file:
main.m377 chars15 lines
In this example, myfile.txt
is assumed to have headers on the first line and data in subsequent lines, with each value separated by commas. The formatSpec
string specifies the data format (three floating-point numbers in this case), and the Delimiter
option tells textscan
to use commas as the separator between values.
Note that textscan
returns the data as a cell array. To extract the data, you can convert it to a matrix or other data structure as needed.
You can also use regular expressions (regex) with textscan
to handle more complex file formats. For example, to parse a file with both numeric and string data separated by arbitrary whitespace, you could use a format string like this:
main.m27 chars2 lines
This would match a string, followed by a floating-point number, followed by another string, etc. To use this format string with textscan
, you would need to specify a regular expression that matches the whitespace between values:
main.m76 chars2 lines
The Delimiter
option is set to an empty string to indicate that there is no fixed delimiter between values (since there could be arbitrary whitespace). The Whitespace
option specifies a regex pattern that matches tab, newline, carriage return, and space characters, which are the most common whitespace characters.
gistlibby LogSnag