Effacer les filtres
Effacer les filtres

Include libsvm in simulink, s function

4 vues (au cours des 30 derniers jours)
Jan Kappen
Jan Kappen le 5 Mai 2014
Hello guys.
I'm wondering how complicated it would be to call libsvm [ http://www.csie.ntu.edu.tw/~cjlin/libsvm/ ] from a simulink model. In addition it would be nice, if code generation would be supported.
In the libsvm package there already is the source code for the purpose of generating a mex file that can be used in matlab. This works:
mex svmpredict.c ../svm.cpp svm_model_matlab.c
the function then is called svmpredict:
>> svmpredict
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
[predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
model: SVM model structure from svmtrain.
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
-q : quiet mode (no outputs)
predicted_label: SVM prediction output vector.
accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.
prob_estimates: If selected, probability estimate vector.
svm_model_matlab.c is a wrapper to transform the
The file svm_model_matlab contains only a wrapper to transform the 'model' (the svm itself) to a matlab struct.
svmpredict.c has the mexFunction(...) entry point to call the function:
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
My big question is: How to include these files in simulink?
I tried:
  • embedded matlab function: does not work since it only supports matlab scripts, not mex files
  • interpreted matlab function: does not work, only 1dim, and i didn't find a way to input parameters (ie the svm model, that has been trained before). Additionally it does not support code generation afaik
  • s-function: this is quite more complicated... I thought, I could easily use the source code for the mex generation, but the mechanisms are different, right? I used the s function template (matlabroot/simulink/src/sfuntmpl_basic.c) and set the ports width etc. But I don't know how to combine the
static void mdlOutputs(SimStruct *S, int_T tid)
function (which seems to be called each time the model updates?) with the mexFunction from the svmpredict.c file. If I try to compile everything (svmpredictSimulink.c is the s function) this happens:
>>mex svmpredictSimulink.c svmpredict.c ../svm.cpp svm_model_matlab.c
svmpredict.obj : error LNK2005: mexFunction already defined in svmpredictSimulink.obj
So there seems to be an additional mexFunction defined implicitly, because the function is not present in the svmpredictSimulink.c file. Is it created in the background? Where? The file consists only of the template, no output calculations or so. Only the definition of the parameters and portnumbers and -widths is done.
Is the way to create a S-function the way I should go, or is there any other? I've seen the legacy code tool, but it seems to be for easier functions with direct return values and so?
There is also a raw c file to create the prediction (with main.c etc), but I thought if it's already ported to matlab, it should be simpler to start there?
Thank you very much, Jan

Réponses (2)

Friedrich le 5 Mai 2014
Modifié(e) : Friedrich le 5 Mai 2014
you can use LIBSVM in a embedded matlab function block but you need to declare those functions as extrinsic (See coder.extrinsic function, or in the case you use on older MATLAB see eml.extrinsic). However doing this, you cannot generate Code out of your Simulink model but you can still simulate it. So e.g.
  2 commentaires
Jan Kappen
Jan Kappen le 5 Mai 2014
Thank you, this could work, but as you mentioned not with code generation.
The even bigger problem is, that libsvm makes heavy use of sparse matrices. So far there is no support, right?
If I convert them via full(...), matlab crashes because of memory access violation.
Friedrich le 6 Mai 2014
Right, sparse matrices arent supported by simulink but they are supported in MATLAB Function Blocks. You could send a sparse matrix as (i,j,value) vectors and assemble it later on. E.g. lets say you have
>> a = sparse(eye(3))
a =
(1,1) 1
(2,2) 1
(3,3) 1
You cannot send a as signal but you could do
[i,j,val] = find(a)
and then use i,j,val as 3 signals which you can later assemble in a MATLAB function block again using sparse
>> a = sparse(i,j,val)
Or consider switching to MATLAB complety (see ode45 and friends to solve your system over time).

Connectez-vous pour commenter.

Ricardo Gualavisi
Ricardo Gualavisi le 27 Juin 2016
Have you got any solution for this problem ? It would be very helpful for me. Thanks a lot!!


En savoir plus sur Startup and Shutdown dans Help Center et File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by