Define Import Options for Table in MATLAB

In this tutorial, we will define various import options for table in MATLAB. The import options allow us to bring data into a table or another data format. For example, we can utilize this option to read data from a given spreadsheet file "sample.xlsx" into MATLAB in the form of a table.

By using the import options, we have the ability to open the designated file, select the desired output format and date range, and then save our selections. Upon clicking the "Import Selection" option, the specified data can be easily integrated into the MATLAB workspace.

Additionally, the tabular data can be not only imported into the MATLAB Workspace but also into the SimBiology Model Analyzer app. This versatile option supports a wide range of file extensions, including Excel files (.xls, .xlsx), SAS XPORT files (.xpt), and text files (.csv, .txt). Also, we have the option to explicitly indicate the file format from which the data originates. Throughout the import process, the columns are interpreted based on the NONMEM definitions that ensures accurate alignment and representation of data.

In MATLAB, to start the data import process, find and select the "Import Data" option within the "Variable" group of the Home tab.

Alternatively, we can select an alternate method by right-clicking on the file name within the Current Folder browser and then choosing the "Import Data" option.

Define Import Options for Tables in MATLAB

In MATLAB, the process of importing tables involves the utilization of the ‘readtable’ function that is a built-in function in MATLAB. However, some situations require a higher degree of supervision over the data import process for the tabular data. For example, there could be a situation where selecting specific variables becomes mandatory to import rows with absent or problematic data.

Hence, in order to control the import procedure, the creation of an import option entity becomes essential. The attributes of this entity can be altered to match with the exact requirements of the data import procedure.

Create an Import Option in MATLAB


In MATLAB, to create import options for a sample dataset named "tutorialspointcourses.csv", we can utilize the "detectImportOptions" function. This function creates a "DelimitedTextImportOptions" object align with the characteristics of the given text file.

opts = detectImportOptions('tutorialspointcourses.csv');

Customize Table-Level Import Options

In MATLAB, the properties of the import options object can be modified to control the import procedure. Some properties apply to the entire table, while others apply to specific variables. Properties that relate to the entire table provide regulations for managing missing or problematic data. For example, erroneous data that causes import errors can be managed by using the ImportErrorRule to "omit row".


To replace any missing values, assign the MissingRule as "fill" The value of the FillValue property acts as a replacement for the missing values. For example, NaN (Not-a-Number) can be used to replace the missing attributes.

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

Customize Variable-Level Import Options


To access and configure options for specific variables, we can use the bult-in functions "getvaropts", "setvartype", and "setvaropts". For example, to view the current options applicable to variables "CourseID", "CourseName", "Tutor", and "CourseFee", employ the getvaropts function.

getvaropts(opts,{'CourseID', 'CourseName', 'Tutor', 'CourseFee'});


We can use the "setvartype" function to change the data types of the variables. For example, change the data type of the variables "CourseID", "CourseName", and "Tutor" to 'char', as their values may not be numerical figures.

opts = setvartype(opts,{'CourseID', 'CourseName', 'Tutor', 'CourseFee'}, {'char', 'char', 'char', 'double'});

To change additional properties of the object, we can use the "setvaropts" function.

For example, for the "CourseID" variable, remove any preceding whitespace from the text by assigning the "WhiteSpaceRule" property to "trimleading".


For the "CourseFee" variable, fill fields containing 0 or NA with the designated value in the "FillValue" property by configuring the "TreatAsMissing" property.

opts = setvaropts(opts, 'CourseID', 'WhitespaceRule', 'trimleading');
opts = setvaropts(opts, 'CourseFee', 'TreatAsMissing', {'0', 'NA'});

Import and Show the Table


Finally, after specifying the desired variables and importing the table using the "readtable" function, display the first 10 rows of the table.

opts.SelectedVariableNames = {'CourseID', 'CourseName', 'Tutor', 'CourseFee'}; 
T = readtable('tutorialspointcourses.csv', opts);
T(1:10, :)


ans = 10×4 table
CourseID		CourseName	Tutor		CourseFee
_________		___________	______	        _________
{'1001'}		MATLAB		Manish	        $10
{'1002'}		C#		Imran		$10
{'1003'}		AI		Taabish	        $10
{'1004'}		Public Speak	Dragana	        $10
{'1005'}		Psychology	Vikash	        $10
{'1006'}		ChatGPT		Nfada		$10
{'1007'}		Focus 5		Ram		$10
{'1008'}		Finance		CA N Raja	$10
{'1009'}		English		Dava		$10
{'1010'}		Money Matters	CA N Raja	$10


This is all about defining import options for table in MATLAB. In this tutorial, we have covered all the concepts related to import options for table in MATLAB.

Updated on: 04-Sep-2023


Kickstart Your Career

Get certified by completing the course

Get Started