This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Variable description for bayesopt or other optimizers


Create variables for optimizers.



variable = optimizableVariable(Name,Range)
variable = optimizableVariable(Name,Range,Name,Value)



variable = optimizableVariable(Name,Range) creates a variable with the specified name and range of values.


variable = optimizableVariable(Name,Range,Name,Value) sets properties using name-value pair arguments. For example, optimizableVariable('xvar',[1 1000],'Type','integer') creates an integer variable from 1 to 1000. You can specify multiple name-value pair arguments. Enclose each property name in quotes.


expand all

Variable name, specified as a character vector. The name must be unique, meaning different from those of other variables in the optimization.


  • There are two names associated with an optimizableVariable:

    • The MATLAB® workspace variable name

    • The name of the variable in the optimization

    For example,

    xvar = optimizableVariable('spacevar',[1,100]);

    xvar is the MATLAB workspace variable, and 'spacevar' is the variable in the optimization.

    Use these names as follows:

    • Use xvar as an element in the vector of variables you pass to bayesopt. For example,

      results = bayesopt(fun,[xvar,tvar])
    • Use 'spacevar' as the name of the variable in the optimization. For example, in an objective function,

      function objective = mysvmfun(x,cdata,grp)
      SVMModel = fitcsvm(cdata,grp,'KernelFunction','rbf',...
      objective = kfoldLoss(crossval(SVMModel));

Example: 'X1'

Data Types: char

Variable range, specified as a 2-element finite increasing real vector, or as a cell array of names of categorical variables:

  • For real or integer variables, Range gives the lower bound and upper bound of that variable.

  • For categorical variables, Range gives the possible values.

Example: [-10,1]

Example: {'red','blue','black'}

Data Types: double | cell

Variable type, specified as 'real' (real variable), 'integer' (integer variable), or 'categorical' (categorical variable).


The MATLAB data type of both 'real' and 'integer' variables is the standard double-precision floating point number. The data type of 'categorical' variables is categorical. So, for example, to read a value of a categorical variable named 'colorv' in a table of variables named x, use the command char(x.colorv). For an example, see the objective function in Custom Output Functions.

Example: 'Type','categorical'

Data Types: char

Transform applied to variable, specified as 'none' (no transform) or 'log' (logarithmic transform).

For 'log', the variable must be 'real' or 'integer' and positive. The variable is searched and modeled on a log scale.

Example: 'Transform','log'

Data Types: char

Indication to use variable in optimization, specified as true (use the variable) or false (do not use the variable).

Example: 'Optimize',false

Data Types: logical


You can use dot notation to change the following properties after creation.

  • Range of real or integer variables. For example,

    xvar = optimizableVariable('x',[-10,10]);
    % Modify the range:
    xvar.Range = [1,5];
  • Type between 'integer' and 'real'. For example,

    xvar.Type = 'integer';
  • Transform of real or integer variables between 'log' and 'none'. For example,

    xvar.Transform = 'log';

You can use this flexibility, for example, to tweak an optimization that you want to continue. Update the range or transform using dot notation and then call resume.

Object Functions

bayesoptSelect optimal machine learning hyperparameters using Bayesian optimization


collapse all

Real variable from 0 to 1:

var1 = optimizableVariable('xvar',[0 1])
var1 = 
  optimizableVariable with properties:

         Name: 'xvar'
        Range: [0 1]
         Type: 'real'
    Transform: 'none'
     Optimize: 1

Integer variable from 1 to 1000 on a log scale:

var2 = optimizableVariable('ivar',[1 1000],'Type','integer','Transform','log')
var2 = 
  optimizableVariable with properties:

         Name: 'ivar'
        Range: [1 1000]
         Type: 'integer'
    Transform: 'log'
     Optimize: 1

Categorical variable of rainbow colors:

var3 = optimizableVariable('rvar',{'r' 'o' 'y' 'g' 'b' 'i' 'v'},'Type','categorical')
var3 = 
  optimizableVariable with properties:

         Name: 'rvar'
        Range: {'r'  'o'  'y'  'g'  'b'  'i'  'v'}
         Type: 'categorical'
    Transform: 'none'
     Optimize: 1

Introduced in R2016b

Was this topic helpful?