Use Sim() command without coder.extrinsic in Matlab - matlab

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.

Related

Should Simulink if-else block run faster than calling equivalent Matlab function?

I've been modeling different possibilities of obtaining a decision in form of an Enum to compare them, expecting library functions of Simulink performing better than implementing the same functionality calling Matlab functions.
According to this Matlab article interpreted Matlab functions usually perform worse than their library counterparts due to passing through multiple software layers.
For my tests I modeled two systems calculating an output value based on an input signal and ran them for the same number of time steps with a Profiler attached.
The implementation using Matlab function took 16s to execute, while the implementation using library blocks took 566s.
Why does the simulink model using the if-else block run significantly slower? Or is there something wrong with my profiling approach?
I'd be grateful for any insights.

Loading Classification Model in Simulink

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.

Does Simulink convert models/block diagram to code before simulation?

I've read that I can generate code from Simulink models/block diagrams. I am curious whether Simulink always converts a model to (c/c++/java) code prior to running a simulation in the Simulink software, and then execute that code? I mean, whenever I'm running a simulation is Matlab converting the block diagram to (c/c++/java) code and running that code behind the scene. In this case, simulation in Simulink directly depends on running some code; this information is important to me in some way.
Generating and running code for a complete model seems plausible, as we can write s-functions using C/Matlab code and use them as custom blocks. So simulating a model involves running code in some degree. Again, since we can write Matlab code as well, simulation may involve interpreting Matlab code in some environment. It makes me curious whether these information are available - how tightly running a simulation in Simulink depends on executing native code in user's machine.
I did some search before posting and found this SO question: How does simulation engine work? Discussion in this question does not answer my question directly.
The answer depens on which mode you chose.
In the normal mode Simulink will run the model as it is primarily using the MATLAB execution engine. No code is generated. Native implemented parts (e.g. S-Functions) are used as individual binaries called by the MATLAB interpreter.
In the accelerator mode Simulink generates model code. This means your full model (except parts where code generation is impossible) is generated and compiled into one binary.
In the rapid accelerator mode not only your model but also the solver is generated and compiled into one binary, now running in a separate process.
For more details refer to the official documentation

Generate equation from Simulink Model

I have a large simulink model with many source and sink blocks, many with only elementary arithmetic operations in between. I have been asked to document the equations behind the model. I am currently doing this manually and I am finding it rather slow and there is a relatively high chance of errors in the process.
Is there any way for Simulink to generate the equations (in MATLAB syntax for example) automatically?
There is no utility in MATLAB/Simulink that can do exactly what you are looking for (and I personally don't know of any third-party tools that can do this, either).
However, I think that your best bet might be to make use of Simulink Coder. This will allow you to convert your Simulink model to C code. From that code, you may be able to extract the equivalent equations more easily than you can by analyzing the Simulink model by hand.
The catch, though, is that Simulink Coder is an add-on package to base Simulink, so you may or may not have this tool available to you.

RMS not supported in Matlab function inside Simulink

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.