Overview

This guide is for algorithm developers coming from MATLAB who want to get their algorithm running inside a CoreEngine Model as fast as possible: generate C++ from your MATLAB code, expose its inputs/outputs as ICD elements so you can monitor and inject them from DIPanel, and wire the algorithm's lifecycle to the model's.

1. From MATLAB to C++

Use MATLAB Coder to generate C++ from your MATLAB function or System object. For an algorithm with persistent state, MATLAB Coder typically generates a C++ class with a constructor/destructor and a step method that takes a plain input struct and writes into a plain output struct — for example:

Still iterating on the algorithm in MATLAB or Python and don't want to compile it to C++ yet? The External Model Driver calls a live MATLAB engine or an imported Python module directly from a stream — see How to Set Up Streams. Switch to the MATLAB Coder approach below once the algorithm is stable and you need it running at full real-time performance.
namespace FM {
struct struct0_T {          // generated input struct — plain fields, fixed-size arrays
    double simTime;
    double AP_DelCmd[4];
    double Target_Output_Att[3];
    bool   Flag_isInit;
};

class cFM {
public:
    cFM();
    ~cFM();
    void convertFlightModel(const struct0_T *In, struct1_T *Out);
};
} // namespace FM

Drop the generated files into your model's folder (e.g. Models/<ModelName>/algo/) and add them to the model's build. The generated struct fields are exactly what you'll mirror in the ICD in the next step.

Generated input/output structs use plain scalar fields and fixed-size arrays, not dynamically-sized containers — that's exactly the shape an ICD block needs, and it's also what keeps the algorithm allocation-free at runtime (see Performance).

2. Create the ICD CSV for Your Structures

Mirror the generated input struct as one ICD block, and the output struct as another, with matching field names, types, and array sizes — see DBSimGenerator for the full CSV format. For the struct0_T example above:

BlockName, FieldName, Type, Size, Description
FMInputs, simTime, R8, 8, Simulation time
FMInputs, AP_DelCmd, R8, 4, Autopilot delta command [4]
FMInputs, Target_Output_Att, R8, 3, Target output attitude [3]
FMInputs, Flag_isInit, U1, 1, Init flag

Once generated, every field shows up as an element you can watch or inject live from DIPanel — input elements to drive the algorithm by hand, output elements to watch its results, with no extra code.

3. Structs for Streams and Other MATLAB Models

If the algorithm's output needs to leave the process (to hardware, another machine, or a recorder) or be consumed by another model — including another MATLAB-Coder-generated model running as its own CoreEngine model — it goes through the same mechanisms as any other model:

  • To send data out over a physical/network interface, wire the output block into a stream in streams.json, the same as any other block — see Drivers and Streams.
  • To feed another model directly, that model simply reads the ICD elements of your output block — no stream required for in-process model-to-model data flow.
  • Register a stream trigger if the consuming model should run only when your algorithm produces new output.

4. Connection Functions: Reset, Init, Run, Stop

Map your algorithm's lifecycle onto the model's own lifecycle callbacks:

Algorithm stepModel callbackWhat happens
ResetOnReset()Construct (or reconstruct) the algorithm instance. Called first at start-up and on every Reset. This is where any one-time allocation happens — never in OnRun().
InitOnInit()Any one-time setup the algorithm needs before its first real step (e.g. loading tables, priming state). Called at start-up after OnReset() and on every Init.
RunOnRun()Copy ICD input elements into the algorithm's input struct, call its step method, copy the output struct back into the ICD output elements.
StopOnStop()Pause only: keep the instance. OnStop() is also called at start-up between OnReset() and OnInit(), and a Run after a Stop continues without a Reset, so releasing it here would leave OnRun() with a null pointer. The std::unique_ptr frees it when the model is destroyed.
class CLSUSeekerModel : public CModelBase
{
    std::unique_ptr<FM::cFM> algo;   // owns the generated algorithm instance

    void OnReset() override {
        algo = std::make_unique<FM::cFM>();   // one-time construction, not in OnRun
    }

    void OnRun() override {
        CLRUsProj::LRU_Seeker->Raw2Eng();      // pull in latest raw data

        FM::struct0_T in{};
        in.simTime = pFMInputs->simTime;
        // ... copy the rest of the input elements ...

        FM::struct1_T out{};
        algo->convertFlightModel(&in, &out);

        // ... copy fields from out into the output block's eng elements ...

        CLRUsProj::LRU_Seeker->Eng2Raw();      // publish results
    }

    void OnStop() override {
        // keep algo: OnStop() also runs at start-up (after OnReset, before OnInit)
        // and a Run after a Stop continues with it; the unique_ptr frees it with the model
    }
};

5. Performance

OnRun() is the real-time hot path — the same rules that apply to any model apply doubly to an embedded algorithm:

  • Never allocate during Run. Construct the algorithm instance once in OnReset(); don't new/malloc, resize a std::vector, or build a std::string inside OnRun().
  • In MATLAB Coder's code generation settings, prefer fixed-size arrays over variable-size/dynamic memory where your algorithm allows it — the generated struct fields shown above are exactly this style, and they map directly onto ICD elements with no copying overhead.
  • Avoid logging or console output (printf, std::cout) inside OnRun() — use lightweight profiling instead if you need timing data.
  • Keep the struct copy in/copy out steps simple memcpy-style field assignments — avoid per-field conversions or branching where possible.
  • Profile the step call itself if it's a candidate for the critical path — see Profiling in the Models guide.
These are the same rules documented for any model's OnRun() — see Getting Started's "Work Section" rules and How to Build an APIC System for Software Developers for the full picture.