Open In App

Define Import Options for Table in MATLAB

Last Updated : 22 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The import tool lets you import into a table or another type of data. Take into consideration reading data from the sample spreadsheet file patients.xls into MATLAB as a table. Open the file with the Import Tool, select the output format and date range, and save. After you click the Import Selection button, the data will be imported into the MATLAB workspace. Tabular data can be imported into MATLAB Workspace and the SimBiology Model Analyzer app. Supported file extensions include Excel® files (.xls,.xlsx), SAS XPORT files (.xpt), and text files (.csv,.txt). You can also specify that the data come from a file formatted. During the import process, the NONMEM definitions are used to interpret the columns.

Import Tool in MATLAB:

Click Import Data in the Variable section of the Home tab. A different option is to right-click the file name in the Current Folder browser and select Import Data. Import Tool starts up.

Define Import Options for Table:

Importing tables requires using the read table function. However, there are occasions when importing tabular data calls for additional control over the import process. You might want to select the variables that will be used to import rows with data that is missing or causes errors. To control the import process, you can create an import options object. The object’s properties can be changed to meet your import requirements.

Create an import options object for the Buildings.csv test data set using the detectImportOptions function. The DelimitedTextImportOptions object is produced by the detectImportOptions function for this text file. The complete list of properties for the import options object is available on the detectImportOptions reference page.

Example 1:

Matlab




opts = detectImportOptions('Bulidings.csv');


  

Customize Table-Level Import Options:

The import options object’s properties can be changed to control the import process. Several of the properties are applicable to the entire table, while others are only applicable to particular variables. Examples of properties that affect the entire table include rules for handling data that is missing or causes errors. For instance, you can eliminate rows containing data that cause import errors by setting the ImportErrorRule to “omit row.”For any missing values to be replaced, set the MissingRule to “fill.”The worth of the FillValue property replaces the missing qualities. NaN, for example, can replace missing qualities.

Matlab




opts.'omitrow' is the ImportErrorRule.
opts.MissingRule = 'fill';


Customize Variable-Level Import Options:

To get and set options for particular variables, use the functions getvaropts, setvartype, and setvaropts. To see the options that are currently available for the variables FlightNum, Origin, Dest, and ArrDelay, for example, use the getvaropts function.

Matlab




getvaropts(opts,{'BuildingsNum','HallsName in Buldings',
'Towers near Buildings','skyscraper Buildings'});


Using the setvartype function, modify the data types for the variables:

  1. Change the variable FlightNum’s data type to char since its values are flight identifiers rather than numbers.
  2. Change the data type of the variables Origin and Dest to categorical since they represent a limited set of text values that repeat.

Matlab




opts = setvartype(opts,{'BuildingsNum',
'HallsName in Buldings','Towers near 
Buildings','skyscraper Buildings'},..
{'char','categorical','categorical','single'});


  

Using the setvaropts function, modify additional properties:

  1. Set the WhiteSpaceRule property of the BuildingsNum variable to trim leading to eliminate any leading white spaces from the text.
  2. Set the TreatAsMissing property for the skyscraper Buildings variable to the value specified in the FillValue property to replace fields that contain 0 or NA.

Matlab




opts = setvaropts(opts,'BuildingsNum',
'WhitespaceRule','trimleading');
opts = setvaropts(opts,'skyscraper 
Buildings','TreatAsMissing',{'0','NA'});


Import Table

Display the first eight rows of the table after specifying the variables to be obtained and importing them with read table.

reading

Output:

         

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads