0
votes

I've trained a classification model (Classification Tree) using Matlab's Classification Learner App. I've exported it to the workspace and also saved it as a .mat file.

I need to embed it in a Simulink model to make predictions at every time step during simulation. I've tried using a Matlab Function block and using "load" to load the classification tree from the .mat file and then using mdl.predictFcn(myInputData) to make a prediction on new data. However, when I try to run the simulink model I get the error:

Found unsupported class for variable using function 'load'. MATLAB class 'function_handle' found at 'mdl.ClassifierTTC.predictFcn' is unsupported.

Does this mean I cannot use my classification model in Simulink? Has anybody tried to do something like this already?

Thanks in advance.

1
It means you can't use a MATLAB Function, but you can use an Interpreted MATLAB Function or an m-code S-Function.Phil Goddard
Hi Phil, thanks for your answer. I wrote an S-Function and it worked. However, my end goal is to compile the Simulink model and the compiler now needs a .tlc file for the S-Function block. How can I generate the .tlc file?Tesla.
You would need to write your own. Note that you are using functionality that fundamentally requires MATLAB to execute, and as such is not supported by Simulink Coder. Your .tlc file will need to get Simulink Coder to generate c-code (for this block) that uses MATLAB API functionality to execute your (compiled code) and hence will require MATLAB on the the target machine.Phil Goddard
I see. The target machine has MATLAB installed, so if it's possible to get the .tlc to do that it should work I guess. However, I've never written a .tlc file much less such an advanced version, do you know of any sample code I could use as an example? Would it be possible to do this with the S-function builder (I believe it generates a .tlc)?Tesla.
The s-function builder is for creating C-code S-function. Presumably you have an m-code S-function. You''ll need to write the tlc file yourself. That is non-trivial, and although the Simulink Coder doc has examples, it's not going to be a case of cutting/pasting/modifying something that already exists. But start with the TLC section of the Simulink Coder doc.Phil Goddard

1 Answers

0
votes

There is a work around for this problem without needing to write any C Code. All necessary C Code can be generated with MATLAB's code generator.

After generating a classification model, export it from the Classification Learner App to the workspace and then save it using the saveCompactModel() function. Note that some classifier models are not supported even in the latest MATLAB versions (2017a).

Write an m-file that loads and passes unseen data to the classifier and uses the predict() function to get the classification label from the classifier. Use codegen() with the dll configuration to generate C Code from the m-file, but do not let it compile. This will generate all necessary header and source files.

Now, using legacy_code() generate an S-Function to import the C Code into Simulink. Link ALL generated header and source files to the S Function and generate the S Function block and a tlc file with legacy_code().

This produces an S Function block with the classifier and the predict function embedded in the block. The legacy_code() function generates a tlc for this S Function and allows the S Function block to be compiled.