Import Tool - Import data from file - MATLAB (original) (raw)
Description
The Import Tool lets you interactively select and import MAT, text, spreadsheet, media (image, audio, and video), and scientific data (HDF5 and netCDF) files. You can also generate MATLAB® code to import similar files.
Open the Import Tool
- MATLAB Toolstrip: On the Home tab, in theVariable section, click Import Data.
- MATLAB command prompt: Enter
uiimport
oruiimport(filename)
, wherefilename
is a string scalar or character vector specifying the name of a file.
Examples
Interactively import the data from a text or spreadsheet file as a table using the Import Tool.
The Import Tool automatically detects details about the data to be imported. You can adjust these fields to customize the import options. Then, click the Import Selection button. The new table appears in your workspace.
For example, import the data in the text file textdata.txt
as a table.
The Import Tool recognizes that the column delimiters are commas, the first row contains variable names, and the columns have numeric, text, and categorical types.
Interactively import the image data from a graphics file as an array using the Import Tool.
The Import Tool displays the name, size, class, and value fields and a preview of the image data to import. Then, to import the image data, clickImport. The array appears in your workspace.
For example, import the data in the image filepeppers.png
.
Interactively import the variables in a MAT file using the Import Tool.
The Import Tool displays the variables in the MAT file. Select the Select all check box to import all three variables in the MAT file. Alternatively, to import only a subset of the variables in the MAT file, select the check boxes in the table for the variables to import. Then, to import the variables, clickImport. The variables appear in your workspace.
For example, import the variables in the MAT fileseamount.mat
.
Interactively import data into fields of a scalar structure using the Import Tool.
By default, the Import Tool imports data as individual variables. Instead, to import data into the fields of a structure, open the Import Tool by specifying an output argument for the uiimport
function. Then, to create the structure, click Import. The structure appears in your workspace.
For example, import the x
, y
, andz
variables in the MAT file seamount.mat
as fields in a scalar structure.
S = uiimport("seamount.mat")
S =
struct with fields:
x: [294×1 double]
y: [294×1 double]
z: [294×1 double]
Import spreadsheet data from the clipboard using the Import Tool.
In addition to importing spreadsheet data from a file, you can also import spreadsheet data form the clipboard. First, select and copy your spreadsheet data. Then, open the Import Tool for the data on the clipboard by specifying thepastespecial
option for the uiimport
function.
Interactively generate a function using the Import Tool to import data from multiple text files.
When you import data from a file using the Import Tool you can generate a function that represents the import operation. You can call the function to import data from other files. For example, import a data from a set of text files.
First, open the Import Tool and choose one text file to import from the file selector. Next, click Import Selection > Generate Function. The Import Tool generates a function similar to this excerpt and opens the function in the Editor.
function textdata = importfile(filename,dataLines) %IMPORTFILE Import data from a text file ...
Save the function. Then, you can call the function to import data from a similar file. For example, create a cell array containing data from similar text files.
numFiles = 25; startRow = 2; endRow = inf; myData = cell(1,numFiles);
for fileNum = 1:numFiles fileName = sprintf('myfile%02d.txt',fileNum); myData{fileNum} = importfile(fileName,startRow,endRow); end
Note
For some file types, such as image, audio, and video files, code for the import operation is automatically generated in the Command Window.
Related Examples
- Read Text File Data Using Import Tool
- Read Spreadsheet Data Using Import Tool
- Import Images, Audio, and Video Interactively
Programmatic Use
uiimport
opens a dialog to interactively preview and import data from a file or the clipboard.
uiimport(filename)
opens the specified file in the Import Tool. Specify filename
as a string scalar or character vector.
If the file is not on the MATLAB path, specify the full path. For example,uiimport("C:\Documents\textdata.txt")
opens the specified file in the Import Tool.
uiimport -file
presents the file selection dialog first.
uiimport -pastespecial
presents the clipboard contents first.
S = uiimport(___)
stores the resulting variables as fields in a scalar structure.
Limitations
- The Import Tool does not support importing data from HDF5 files that use the Family driver, the Multi driver, or the Split driver. To import data from a file that uses one of these three drivers, use either the MATLAB high-level HDF5 functions (for example, h5read or h5readatt) or the MATLAB low-level HDF5 functions.
Tips
- To interactively select and import a file within a function, use the
uiimport
function with an output argument to open the Import Tool. This imports the data into the function workspace, rather than importing the data into the base workspace.
Version History
You can import timestamped data from a file as a timetable using the Import Tool. If the tool detects timestamps in the first variable of the data to import, the tool uses the timetable
data type by default.
You can no longer drag a file into the Import Tool to import its data. To import data from a file using the Import Tool, right-click the file in theFiles panel and select Import Data.
The Import Tool imports all text as a string array, except for cell arrays. Previously, you could choose to import text as either a string array or a cell array of character vectors.
You can import HDF5 data in MATLAB Online™. Using this tool, you can:
- Visualize the structure and contents of HDF5 data interactively.
- Search for specific groups, datasets, and attributes.
- Graphically select and deselect groups, datasets, and attributes to import.
- Generate reusable MATLAB code to import HDF5 data.
You can import netCDF data in MATLAB Online. Using this tool, you can:
- Visualize the structure and contents of netCDF data interactively.
- Search for specific variables and attributes.
- Graphically select and deselect variables and attributes to import.
- Generate reusable MATLAB code to import netCDF data.
Import Tool no longer supports importing HDF4 files. To programmatically import HDF4 or HDF-EOS files, use the hdfread function instead.