setvartype

Sets the data type of variables in import options.

Category: Language Fundamentals > Data Types

Syntax

opts = setvartype(opts, vars, type)

Reads the variables vars as type.

opts = setvartype(opts, type)

Sets the type of every variable.

Description

The detected types are only a guess from the first rows. setvartype overrides them before the file is read, for example to read an identifier column as text so that leading zeros are kept, or a number column as single to save memory.

Input arguments

opts
Import options from detectImportOptions.
vars
Variable name, cell array of names, or indices.
type
Type name such as 'double', 'single', 'int8', 'string', 'categorical' or 'datetime'.

Output arguments

opts
The updated options. opts.VariableTypes lists the types.

Example

fid = fopen('v.csv', 'w');
fprintf(fid, 'id,score\n1,2.5\n2,3.5\n');
fclose(fid);
opts = detectImportOptions('v.csv');
opts = setvartype(opts, {'id','score'}, 'single');
opts.VariableTypes
Output
ans = 1×2 cell

    {'single'}    {'single'}

Notes

In MatLivre setvartype works on import options only, not on tables; for tables use convertvars.

See also

setvaropts detectImportOptions readtable convertvars