Main Content


(Not recommended) Save optimization constraint description


writeconstr is not recommended. Use write instead.

writeconstr(constr) saves a description of the optimization constraint constr in a file named constr.txt. Here, constr is the workspace variable name of the constraint. If writeconstr cannot construct the file name from the variable name, it writes the description to WriteConstrOutput.txt instead. writeconstr overwrites any existing file.


writeconstr(constr,filename) saves a description of the optimization constraint constr in a file named filename.


collapse all

Create an optimization constraint in terms of optimization variables, and save its description in a file.

x = optimvar('x',3,2);
cons = sum(x,2) <= [1;3;2];

The TripleConstraint.txt file contains the following text:

(1, 1)

  x(1, 1) + x(1, 2) <= 1

(2, 1)

  x(2, 1) + x(2, 2) <= 3

(3, 1)

  x(3, 1) + x(3, 2) <= 2

Input Arguments

collapse all

Optimization constraint, specified as an OptimizationEquality object, OptimizationInequality object, or OptimizationConstraint object. constr can represent a single constraint or an array of constraints.

Example: constr = x + y <= 1 is a single constraint when x and y are scalar variables.

Example: constr = sum(x) == 1 is an array of constraints when x is an array of two or more dimensions.

Path to the file, specified as a string or character vector. The path is relative to the current folder. The resulting file is a text file, so the file name typically has the extension .txt.

Example: "../Notes/steel_stuff.txt"

Data Types: char | string


  • To obtain the writeconstr information at the MATLAB® Command Window, use showconstr.

Version History

Introduced in R2017b

expand all

Not recommended starting in R2019b