Using coder.extrinsics conditionally - matlab

This question refers to Matlab coder extrinsic functionality. Some functions like fprintf are extrinsic in older Matlab version, and are not extrinsic in the newer ones. Is there a way to support several Matlab versions, if coder.extrinsics is allowed only at the top level, and it's not possible to put it under if statement?

You cannot conditionally make some functions extrinsic directly. One way would be to use two different functions like fprintf_old and fprintf_new. fprintf_old would have coder.extrinsic declaration and then calls fprintf. fprintf_new can call fprintf without extrinsic declaration. Now you can pick between the two calls by checking for your version with a condition that is constant during compilation. For example,
if coder.const(isOlderVersion())
fprintf_old();
else
fprintf_new();
end

In code generation, feval constructs an extrinsic call to the function named in the first argument. Since you can embed calls to feval inside of control flow, it can be used to selectively call a function extrinsically and keep the code in a single source file:
if isOlderVersion()
% Call fprintf extrinsically
feval('fprintf');
else
fprintf();
end

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!

multiple return variables from graphical function in stateflow

I'm using stateflow graphical function and one of my return parameters are not updated after exiting the function.
I'm using matlab as my default code language.
Is it possible to return more than one variable from a graphical function?
What is the correct way for returning more than one parameter from a graphical function?
Thank u
As per the documentation: The syntax for calling a function with multiple outputs is exactly the same as in MATLAB.
There are several examples of doing this in the documentation, including Multi-Output Graphical Function

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.

How can I get Matlab to use the variable value instead of name

In my code, I have a line that looks like this:
f=#(test) bf{i}(5);
where bf is a cell array with functions from str2func() stored in it, i is a variable storing an integer, and the 5 is the argument to pass to the function. How can I get matlab to evaluate the line using the current value of i? Right now when I display f it outputs:
#(test)bf{i}(5)
Lets say i=1, I want it to output:
#(test)bf{1}(5)
Although technically the bf{1} should also be replaced with whatever function is stored in bf{1}. How can I force matlab to evaluate the variables in this statement?
When you create a function handle, the workspace variables are copied and the expression is evaluated when you call the function handle (Typically not a problem in memory consumption, matlab stores only changes).
Now the problem is, to tell Matlab when to evaluate what part of the expression.
If you are aiming for a better performance, pre-evaluate all constant parts of the function. Let's say your function is #(x)(g(3).*f(x)), in this case matlab would evaluate g(3) on every call.
Instead use:
f=#(x)(x.^2)
g_3=g(3)
h=#(x)(g_3.*f(x))
Now having the constant parts evaluated, you want to see the constants instead of the variabe name. I know two ways to achieve this.
You can use the symbolic toolbox, basically converting the function handle to a symbolic function, then to a function handle again. This not only displays the constants, but also substitutes f. This is not possible for all functions.
>> matlabFunction(h(sym('x')))
ans =
#(x)x.^2.*4.2e1
Another possibility is to use eval:
h=eval(['#(x)',sprintf('%e',g_3),'.*f(x)'])
Pre-evaluating constant parts of the expressions as I did in the first step is typically recommendable, but both solutions to get the constant visible in your function handle aren't really recommendable. The first solution using matlabFunction only applies to some functions, while the second comes with all the disadvantages of eval.

When using a multiple-output matlab function, do i need to callback all variables?

When using a multiple-output matlab function, do i need to callback all variables? or can I just take the first two variables? (if so..is it not recommended?)
lets say in function.m
[a, b, c] = function( )
in main.m
[var1, var2] = function;
When calling (almost) any function in matlab you can request fewer outputs than it specifies. So, yes the example you give should work perfectly fine.
There are some clever things you can do with this, such as using nargout within a function to see how many output arguments have been requested and only calculating the values that have been requested as an optimisation trick.
It depends on the definition of the function, and exactly which of the outputs you want to get.
Not all the function allow to do it, you can find all the options for each function in the beginning of the help documentation on the specific function.
If you want only the 2nd, or 3rd outputs, and you want also to save the computation-time of the results that does not interesting, you can use ~ option, like this (for versions 2009b and later):
[~, var1, var2]=function
Many functions allow for options to passed that change how the function behaves. I used/wrote various numerical solving functions a bit and one that nice amount of option, for instance is the LSMR function(s).
Otherwise, if you can manipulate the original either introduce an input(s) to do so before or at the end with an inline subroutine to generate the outputs you want.
Or if you can't it will return as either a cell array or a vector and you can pass an anonymous function to generate the desired outputs that way.
Really, can be done many ways. Very contextual.