Simulink code generation skips my matlab function - matlab

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.

Related

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.

Run a continuous Simulink model with real-time input?

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.
The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

How to use a variable as parameter of a s-function in simulink

I want specify some parameters of a s-functions using a variable and not using a constant inserted manually.
I have tried to define global variables in matlab and using them as paramaters and it works fine.
Anyway, i want that a variable computed in simulink is used as parameter of an other s-fuction. Is it possible? If so, how can i do it?
One idea was to continue to use global constants in matlab e to update them from simulink but i'm not able to do it because the "to workspace" block transforms them in struct or array and i need a single value.
Edit:
To update the variable in the global variable in the workspace is not useful because it seems that simulink read the values at the beginning
The only solution I can think of would be to have the "variable" as one of the input signals to the S-function block, with the signal coming from some other part of the Simulink model where it's computed.
I have found a solution.
First of all, it's important to know that:
a s-function paramenter can be: a constant, a workspace variable, a matlab expression. In matlab expression are included calls to matlab functions.
simulink computes the value of each parameter at the onset of the simulation (it has sense!).
The solution is:
use a matlab function for computing the parameter. a matlab function can call an other simulink simulation. I tried it, even if in a very simple case, and it worked.
I know, it's a crazy solution but it is a solution. Warning: at the moment i'm not able to know if this crazy solution has some side effects on the simulation.
The best solution to this problem is to convert the paramenter in input of the s-function but i can't do it because it's a 3rd party sfunction

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.

How can I call an m file in Simulink and put it to a block in my model?

How can I call an m file in Simulink and put it to a block in my model (without using an S function)? Does anybody have an idea? I'd really appreciate it.
If you're trying to apply a user-defined MATLAB function to Simulink signals, there are a few different ways to do this, depending on your objective. All options are available within the User-Defined Functions section of the Simulink library.
Use the MATLAB function block if you intend to generate code from your model. This block does have restrictions, the entire gamut of built-in MATLAB functions is not available.
Use the Interpreted MATLAB function block if you don't care about code generation, this block can make use of any functions.
Use the Fcn block if your m-file is trivial and contains a simple expression operating on the inputs. In this case you can type the expression directly into the block dialog and reference the input / output signals as shown in the documentation.
MATLAB Fcn block is the best solution to embed M-function file into Simulink model. However, be cautious which version of MATLAB you are using, e.g., with later versions of MATLAB Function Block can be implemented with M-function file with %#codegen and C compiler need to be with your MATLAB package. Good luck