setvaropts

Changes the import options of one or more variables.

Category: Language Fundamentals > Data Types

Syntax

opts = setvaropts(opts, vars, Name, Value)

Sets properties of the selected variables, such as 'Type', 'FillValue' or 'TreatAsMissing'.

Description

Import options describe how each column of a file is read. setvaropts edits the per-variable part: the type, the fill value, which text means missing, prefixes and suffixes to strip. The options are then passed to readtable so that the data arrive in the form you want. getvaropts shows the current settings.

Input arguments

opts
Import options from detectImportOptions.
vars
Variable names or indices.
'Type'
Type to read the variable as, for example 'string'.
'FillValue'
Value used for empty fields.

Output arguments

opts
The updated import options.

Example

fid = fopen('s.csv', 'w');
fprintf(fid, 'id,name\n1,a\n2,b\n');
fclose(fid);
opts = detectImportOptions('s.csv');
opts = setvaropts(opts, 'name', 'Type', 'string');
T = readtable('s.csv', opts);
class(T.name)
Output
ans = 'string'

Notes

In MatLivre setvaropts works on import options only, not on tables.

See also

setvartype getvaropts detectImportOptions readtable preview