How can I update multiple SVM classifier models in generated code without having to regenerate code in MATLAB R2025b?

7 views (last 30 days)
I have 6 classifier models in MATLAB R2025b trained and saved as "ClassificationSVM" MATLAB objects. I generate a single DLL file that encompasses all 6 models using MATLAB coder.  
However, every time I want to retrain and update the models, I need to regenerate the DLL, which is not practical for their deployment requirements. I need to be able to update the classifier models without regenerating the DLL during runtime.
Based on the learnerCoderConfigurer documentation, it is possible to update models without regenerating code using the "learnerCoderConfigurer" object and its update function. However, the documented workflow only demonstrates how to update a single model and only when the generated code output is a mex function, not a DLL. 
How can I generate a single DLL of my entire software and then update the set of classifiers models within the software without regenerating the DLL? Generating separate DLLs for each model is not viable for my workflow.  

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Dec 2025 at 0:00
There are a few potential workarounds that depend on the workflow. They are all based around swapping the parameters of the present model to the parameters of the model you want to update, then updating the present model. To update the other models, you would swap out the parameters again. 
  1. Using "learnerCodeConfigurer" in MATLAB:
    1. If the classifier models are inherently solving the same problem, meaning they all use the same kernel function and have the same model parameters (same classes, number of parameters, etc.), then it is possible that to generate code for one model, swap parameters from the other models, and update them using the workflow shown in the learnerCoderConfigurer documentation. This is possible even if the size of the support vectors varies from model to model. 
    2. In the documentation, code is generated such that we allow 250 support vectors with the line:
      configurer.SupportVectors.SizeVector = [250 34];
      In your case, you can set this number to the maximum value for support vectors among all 6 models. 
    3. You can then follow the documented workflow to generate code for one of the models, designating it as the initial model. Then, you can use "learnerCoderConfigurer" utilities to extract the struct representation of the other models' parameters and pass one of them to the update function for which code is generated in order to swap the parameters of the current model that is deployed without regenerating code. 
    4. Notably, the documented example shows the workflow being done in MATLAB using MEX functions, not DLLs. learnerCoderConfigurer's "validatedUpdateInputs" method that compares one model to the other and extract the tunable parameters in struct form can't be easily applied to a DLL.  Instead, you can use the "validatedUpdateInputs" method for all 6 models, get 6 structs and copy them to the embedded target. There, you can pass these structs to the generated code for updates. This workflow may not be trivial and there's no established protocol achieve this, but it is an option if you can figure how to do it for your target.
  2. Using "learnerCodeConfigurer" in Simulink:
    1. The parameter swapping workflow described in the first workaround can be done in Simulink as well. This has the same prerequisites as the first workaround.
    2. You can call the entry point function "generateFiles" generated from "learnerCoderConfigurer()" and then use them in a MATLAB Function block. The inputs to the MATLAB function block would be the prediction data, and a struct that represents the model parameters.
    3. The model would initially have the parameters for the initial SVM model. The structs for all models, representing the parameters of each model, can be represented as Simulink buses in Simulink as constants, and they can be passed into the MATLAB function block.
    4. You would have to come up with your own logic for when the model needs to be updated. When a model needs to be updated, the update function inside MATLAB Function block would be called with the incoming struct for the model you want to update.
  3. You can first create an DLL for each SVM. Then you would need to write a custom wrapper in C/C++ that does parameter swapping for the six models. You could then wrap everything inside another DLL, which would allow parameter swapping for that SVM via "learnerCoderConfigurer" within that one DLL.
  4. If the SVMs use linear kernels and if you can use "fitclinear" or "fitrlinear", instead of "fitcsvm" and "fitrsvm", then you can follow the published example shown on the In-Place Model Update documentation. that shows how parameter swapping can be done for these types of models. You will have to wire up six linear predict blocks in Simulink model, but the benefit is you likely won't have to write a custom C/C++ wrapper like in the third workaround. Instead, you can do all the design in Simulink and then generate a single DLL. 

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!