importdata
Loads data from a file, choosing the loader by file type.
Category: Data Import and Analysis > Data Import and Export
Syntax
A = importdata(filename)
Returns the numeric data when the file holds only numbers.
[A, delimiter, nheaderlines] = importdata(...)
Also returns the detected delimiter and header line count.
Description
importdata is a convenience wrapper: it looks at the file, finds the delimiter and the header lines, and returns the numbers. It is handy for a quick look at a data file; for repeatable scripts, readmatrix or readtable give you explicit control.
Input arguments
filename- Name of a text, CSV, image, audio or MAT file.
Output arguments
A- A numeric matrix, or a struct with
data,textdataandcolheaderswhen the file has headers.
Example
Numbers with and without a header
fid = fopen('n2.txt', 'w'); fprintf(fid, '1 2 3\n4 5 6\n'); fclose(fid);
A = importdata('n2.txt')
fid = fopen('h.csv', 'w'); fprintf(fid, 'name,age\nAnn,30\nBob,25\n'); fclose(fid);
D = importdata('h.csv')
Output
A = 2×3
1 2 3
4 5 6
D = struct with fields:
data: [2×2 double]
textdata: {'name,age'}
colheaders: {'name' 'age'}