Generate Code for Dictionaries - MATLAB & Simulink (original) (raw)

Main Content

You can use dictionary objects in MATLABĀ® code for code generation. A dictionary is a data structure that stores data as values that you can access using the corresponding keys. This type of data structure is also known as a hash map. For more information about dictionaries in MATLAB, see Map Data with Dictionaries.

When using dictionaries in MATLAB code for code generation, certain considerations apply. See Dictionary Limitations for Code Generation.

Dictionary Functions Supported for Code Generation

Code generation supports all MATLAB dictionary functions except keyHash and keyMatch. See the Extended Capabilities section of each function reference page for usage notes and limitations specific to code generation.

Example: Generate Code to Calculate a Fibonacci Number Using a Dictionary

This example shows how to generate code for a MATLAB function that uses a dictionary to calculate and return the value of the specified position in the Fibonacci sequence.

Define MATLAB Function

Define the function fibonacci, which iteratively calculates the _n_th Fibonacci number and uses a dictionary to store the intermediate results. Using a dictionary can improve performance by avoiding repeated calculations of the previous Fibonacci numbers. In the fibonacci function, use an arguments block to specify that the input is a scalar double.

function out = fibonacci(n) arguments n (1,1) double end d = dictionary(1,1,0,0); maxKey = 1; while maxKey < n d(maxKey+1) = d(maxKey)+d(maxKey-1); maxKey = maxKey+1; end out = d(n); end

Test the function in MATLAB with a sample input value.

Check for Issues Using MEX Function

Generate MEX code for the fibonacci function at the command line using the codegen command. Because you specify input types in an arguments block, you do not need to specify input arguments.

Code generation successful.

Test the MEX function using the same value that you used to test the MATLAB function.

Generate C++ Code

Generate standalone C++ code for the fibonacci function at the command line. Use the -config:lib and -lang:c++ options to generate a C++ static library.

codegen -config:lib -lang:c++ fibonacci

Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because Embedded Coder is not installed, this might cause some Embedded Coder features to fail.

Code generation successful (with warnings): To view the report, open('codegen/lib/fibonacci/html/report.mldatx')

Examine the generated C++ function.

type(fullfile("codegen","lib","fibonacci","fibonacci.cpp"))

// // File: fibonacci.cpp // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 23-Jan-2025 02:24:50 //

// Include Files #include "fibonacci.h" #include "dictionary.h"

// Function Definitions // // Arguments : double n // Return Type : double // double fibonacci(double n) { coder::dictionary d; double out; d.init(); for (out = 1.0; out < n; out++) { double b_d; b_d = d.parenReference(out) + d.parenReference(out - 1.0); d.parenAssign(b_d, out + 1.0); } return d.parenReference(n); }

// // File trailer for fibonacci.cpp // // [EOF] //

See Also

codegen | arguments