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.
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.
Related
I have a matlab function in Simulink which has one input and no output. I get some data from inport into this function and dump those variables into global variables that already exists in the workspace. However, Simulink code generation completely skips this matlab function and I'm not sure how to force it to go through those assignments.
Goal: I'm trying to update some workspace variables that are used throughout the model using inports.
In Matlab I have created a single-output multiple-input fitted model as an idpoly object. This is by using the system identification toolbox and the model is a Box-Jenkins or Transfer Function model. I now want to see what the model does if I put in different inputs (same amount) and no output (model should estimate the output given the input). Yet I did not find a method in Matlab that can do so.
Is there any way I can use an idpoly object (model) in Matlab and use only input to obtain an output? I have tried the command "sim" but it does not do the job.
Use the command "forecast" and specify the input values for the forecast.
Is there an alternative function to the sim() command oder a direct way executing a Simulink Model / a compiled version of it from Matlab without setting coder.extrinsic?
I want to execute a simulink model inside of an iterative Matlab-function. (Hence, speed matters dramatically). The used Simulink model contains a Dymola interface and hence, I cannot model it directly in Matlab. Another main Simulink model starts and iterates the Matlab functions and hence, simulating it leads the coder trying to compile it efficiently. By setting coder.extrinsic, of course I can use the sim command, but it is way too slow for its purpose. I thought about compiling the Simulink model as an alternative, but do not know if this would be a good approach
The structure looks as follows:
Simulink main model -> matlab functions -> simulink model
sim command needs MATLAB. So you need coder.extrinsic. There is no direct alternate way without coder.extrinsic to simulate a model. You can generate code from the model and call the generate code using coder.ceval functions. But if your goal is only to get more speed instead of getting stand-alone code you can set your model to run in accelerator mode and see whether that improves speed.
I'm using libsvm in matlab, and it seems there no existing method to save the model formed from svmtrain. Instead, the functions provided forces me to retrain everytime. Just saving the svmtrain model variable in a .mat does not work. What should I be doing?
The nice thing about SVM, unlike NN is that training is fast and in some cases can be even done online. I have a multiclass SVM and I save the training vectors, classes and the kernel parameters into a txt file.
Simulink has a module called "Matlab Function," which allows you to create a custom function in a Simulink flow diagram.
I implemented a simple function in a Simulink Matlab Function module. My function contains a call to Matlab's built-in rms(). When I run the Simulink model, I get the following error:
The function 'rms' not supported for standalone code generation
If I remove rms from my Matlab Function in the Simulink model, the error goes away and the model runs flawlessly.
Questions:
Is there a way to use Matlab's rms in Simulink?
Are there many other native Matlab calls that can't be used inside Simulink?
I just wanted to clarify and expand upon some points made in learnvst's answer.
Even if you are simply trying to simulate a model containing a MATLAB Function block and are not explicitly attempting to perform code generation, you will still get the not supported for standalone code generation error.
As learnvst indicated, there are multiple restrictions on functions that can be used with code generation. However, if you just want to simulate your model, Simulink allows you to do this if you signify these "black-listed" functions as being extrinsic. This lets Simulink know that the functions will be used for simulation purposes only and won't be part of code generation.
In your particular case, add the following line of code somewhere before your call to rms:
coder.extrinsic('rms');
Declaring a function as extrinsic in a MATLAB Function is often useful even when you are performing code generation. For example, you may want to visualize your data using the plot command during simulation, but obviously would not need the plot command to be part of generated code.
Refer to this doc for more info on declaring functions to be extrinsic.
The not supported for standalone code generation part of the error suggests to me that you are trying to use a product like Matlab Coder to make an executable or native code. If this is the case, there are many naive calls that cannot be used directly in both core Matlab and the toolboxes. The coder products only support a subset of the language. More information can be found here . . .
http://www.mathworks.co.uk/products/matlab-coder/description2.html
As for your call to rms, it is only calculating the root of the mean of the squares. Try creating an alternative using something like . . .
sqrt(mean(x.^2))
...where x is the signal.