Hi Bartlomiej ,
MATLAB only supports exporting deep learning models (such as those created with the Deep Learning Toolbox) directly to ONNX format. Classic machine learning models, typically saved as Mdl objects (like decision trees, SVMs, or ensembles), cannot be exported to ONNX from MATLAB itself. If you want to convert such models to ONNX, the most practical solution is to transfer your model parameters or training data to Python, reconstruct or retrain the model using a library like scikit-learn, and then use the skl2onnx package to export the model to ONNX format. MATLAB Drive can help you move files between MATLAB and Python, but it doesn't provide any additional conversion capabilities.
% Train a classification tree and save it
Mdl = fitctree(X, Y);
saveCompactModel(Mdl, 'treeModel');
% Or save your training data:
save('trainingData.mat', 'X', 'Y');
For python code:
# Load your training data (exported from MATLAB)
import scipy.io
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
mat = scipy.io.loadmat('trainingData.mat')
X = mat['X']
y = mat['Y'].ravel() # Ensure y is 1D
# Train a similar model in Python
clf = DecisionTreeClassifier()
clf.fit(X, y)
# Convert to ONNX
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
onnx_model = convert_sklearn(clf, initial_types=initial_type)
with open("tree_model.onnx", "wb") as f:
f.write(onnx_model.SerializeToString())
