Simulink, matlab functions - matlab

i can't find a way to use the same matlab function in more Simulink block. I used function block but in this way each time a have to copy all code changes. What is the best way to this?

Create a file named <function name>.m that contains your function, then have the function blocks have one line that simply invokes that function.

Related

Can I pass a "coder.opaque" variable from one Matlab function to another inside a StateFlow diagram?

I have a StateFlow chart (C action language) that calls a Matlab function embedded on it. This Matlab function calls an external C function using coder.ceval, which returns a variable that is declared using coder.opaque.
I have another Matlab function embeeded on the same Stateflow that calls another external C function and that requires the coder.opaque variable returned by the previous Matlab function to be passed as an argument.
Is there a way that I can pass that coder.opaque variable from the first Matlab function block to the second one?
I would imagine that joining the two Matlab functions inside the same block (and adding a switch-case statement to execute one or the other) can work, but I would really like to have them in separate blocks.
My objective is to generate the code of the model. Thanks in advance!

Calling a function from stateflow

I am trying to call a function (can be matlab function or a simlink function) from a stateflow in one subsystem, the function must be located in another subsystem as shown below:
enter image description here
The first subsystem will have a stateflow which calls this function. It doesn't really matter if this function is a M-script, currently I do not know how to work around with matlab function (mscript) as it needs the arguments of the functions as inputs.
Is there any workaround for this problem?
ml.functionname() would solve the issue, if passing the argument in a function be sure u have them defined in model explorer for respective Stateflow.

How can I create multiple inputs for a matlab function block?

I want to restrict the variable that I use as input for a matlab function block, it must be only able to increase.
To achieve that i have tried to compare the variable and the previous sample of it in a matlab function, but i don't know how to create two inputs. To solve that i've tried to use a mux, but then i get an error. And google doesn't give me an explanation how to use a mux signal as input for a matlab function.
So that leaves me here with this low-level question.
Thanks in advance for your help and time. Cheers.
To use multiple variables in a function, you need to modify your function declaration at the first line of your function. The reference syntax is:
function [y1,...,yN] = myfun(x1,...,xM)
where x1 through xM are inputs. Your declaration with two inputs might look something like:
function [returnValue] = hasIncreased(previousSample, variable)
See the Matlab Function Documentation for more information.

matlab function parser on gui

I need to do a parser on my gui in matlab in which you can type your own function formula that will be used for computations. I came up with to make function that gets function from edit box as a string and saves it to m-file as a function. But any further calling changed file do not bring expected results if the first form of function were stored in memory. Do you have any other ideas to solve it?
Instead of writing the code to an m-file, take a look at the eval function.

Use of idss data in Level-2 S-function

I am trying to write a level-2 S-function for use in system identification live during a simulation using the n4sid function. However, it does not appear that the output of the n4sid function, an idss datatype, is supported in the output. Is there a way to output this data type from an S-function? Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code? Thanks.
Is there a way to output this data type from an S-function?
No.
Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code?
Yes.
You can create data/destroy data/call functions etc just like any other piece of MATLAB code.