Main Content

ccode

C code representation of symbolic expression

Description

example

ccode(f)returns C code for the symbolic expressionf.

example

ccode(f,Name,Value)uses additional options specified by one or moreName,Valuepair arguments.

Examples

collapse all

Generate C code from the symbolic expressionlog(1+x).

syms x f = log(1+x); ccode(f)
ans = ' t0 = log(x+1.0);'

Generate C code for the 3-by-3 Hilbert matrix.

H = sym(hilb(3)); ccode(H)
ans = ' H[0][0] = 1.0; H[0][1] = 1.0/2.0; H[0][2] = 1.0/3.0; H[1][0] = 1.0/2.0; H[1][1] = 1.0/3.0; H[1][2] = 1.0/4.0; H[2][0] = 1.0/3.0; H[2][1] = 1.0/4.0; H[2][2] = 1.0/5.0;'

Because generated C code initializes only non-zero elements, you can efficiently initialize arrays by setting all elements to0directly in your C code. Then, use the generated C code to initialize only nonzero elements. This approach enables efficient initialization of matrices, especially sparse matrices.

Initialize the 3-by-3 identity matrix. First initialize the matrix with all elements set to0in your C code. Then use the generated C code to initialize the nonzero values.

I3 = sym(eye(3)); I3code = ccode(I3)
I3code = ' I3[0][0] = 1.0; I3[1][1] = 1.0; I3[2][2] = 1.0;'

Write C code to the fileccodetest.cby specifying theFileoption. When writing to a file,ccodeoptimizes the code by using intermediate variables namedt0,t1, and so on.

syms x f = diff(tan(x)); ccode(f,'File','ccodetest.c')
t0 = pow(tan(x),2.0)+1.0;

Include the comment已经rsion: 1.1in the file by using theCommentsoption.ccodeuses block comments.

ccode(f,'File','ccodetest.c','Comments','Version: 1.1')
/* Version: 1.1 */ t0 = pow(tan(x),2.0)+1.0;

Input Arguments

collapse all

Symbolic input, specified as a symbolic expression.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:ccode(x^2,'File','ccode.c','Comments','V1.2')

文件写入指定为一个特征向量or string. When writing to a file,ccodeoptimizes the code by using intermediate variables namedt0,t1, and so on.

Comments to include in the file header, specified as a character vector, cell array of character vectors, or string vector. Becauseccodeuses block comments, the comments must not contain/*or*/.

Tips

  • To generate optimized C or C++ code from a symbolic expression, especially for a large expression, you can use theMATLAB®Coder™app instead of using theccodefunction. This way, the generated code is better integrated into the MATLAB ecosystem. First, convert the symbolic expression to a deployable MATLAB function usingmatlabFunction. Then, generate C or C++ code from the MATLAB function using theMATLAB Coderapp. For an example, seeGenerate C Code from Symbolic Expressions Using the MATLAB Coder App.

已经rsion History

Introduced before R2006a