Why Matlab Coder is slow? - matlab

I'm trying to build a Mex function in Matlab-r2015a using Matlab Coder. The entry point function I want to convert is alg.m which is called by main.m.
Following the procedure, I'm at the step in which I'm asked to "define the type of each input for every entry point function". I choose the automatic procedure and enter main.m
My problem is: in order to define the type of each input, the Matlab Coder takes a very long time; the same problem appears at the next step, when I have to check whether there are issues in the Matlab code. Is that because Matlab has to execute the whole main.m+alg.m?
I suspect this should be the case because when I impose values of parameters that make the computation faster, the input types and issue checks are done immediately. Anyway, I would like to have some more explanations and, if any, suggestions to solve the problem.

You are correct, both steps Define Input Types and Check for Run-Time Issues run main.m which will in turn run alg.m.
If the input data types for the entry-point function don’t change, two test-benches (namely two versions of your main.m) can be written – a shorter one that invokes the entry-point once for defining input types, and a more comprehensive one that thoroughly exercises alg.m. The former can be used to quickly define input types, and the latter should be used when checking for run-time issues.

Related

Function which one must not shadow in Matlab

I am writing some helper functions for unit testing in Matlab. For that I need to overload Matlab functions, built-ins etc. In the documentation is written, that one shall not overload the function builtin. I also found out that one must not overload the lt operator, since it seems to get called by the UI all the time.
Is there a list of functions which one shall not overload, or do you know of any particular problems from overloading some specific functions?
Further Information:
My use case: I want to do mutation-testing and faul-injection.
in ther words, sometimes the called functions shall return wrong results or throw an error. If I overload the Matlab functions, I do not have to change the source code to do that.

Static Variables in Simulink S-Function Builder

I am currently working on the implementation of some C-Code in a Simulink model using the S-Function Builder block.
The code uses various timers and counters, which are defined as static variables to enable the access to the data in following simulation steps.
However, if I start the simulation MATLAB crashes without error message ('Fatal Exception'). To test I defined the variables without the 'static' statement. The Simulation works in this case, however with (logically) wrong results of the S-Function.
Has anybody else faced similar issues or knows how to declare static variables in Simulink?
P.S.
I know I could use Work Vectors, which I do not intend to do, since it would result in huge efforts in adopting the function to do so.
Furthermore I could simply build a feed-back loop in the model using a memory block. For approximately 100 variables this solution would also be pretty impractical.
Not a solution, but a possible workaround is to use the coder.ceval functionality. I have used this to wrap a C-function with similar (static variables used as counters) function. The coder.ceval call is then placed in an embedded matlab block. Possibly some definitions of the interfaces must also be made (structures / bus objects).
Check coder.ceval, coder.rref and coder.wref for the call structure.
It seems like it was a bug in Simulink or the MinGW Compiler. However I tore down the code, ending up with it crashing with the call of one specific variable. I renamed the variable, since I could not find any error in the syntax. Now everything works fine...
The variable name had various underscores and capital letters - in case anyone makes similar experiences.

How to see the type of a matlab variable

I'm looking at a rather large and poorly written Matlab program. One of the things that makes understanding the code tricky is the variables don't show their type. In searching I only found explanations how to do this while debugging code(the whos and class commands). I'm looking for a way to view type information in the editor itself.
For example in the following code I would like to know the type of A and B:
classdef Data
properties
B;
function obj = Data(A)
obj.B = A.B;
end
Or is the type not determined until the function is called, and A could be any class with a B parameter?
As I mentioned in the comments, unfortunately there isn't any way I know of to do this in the IDE without entering the debugger because MATLAB is not statically typed. You can also trace through the function and see what is calling the methods/functions/etc. in question and the variables used.
Your ending sentence is correct. Looking at your example solely in the eyes of the IDE A could be any data type, even one where the dot notation isn't valid (and thus would throw an error). It's up to the user to add input validation for functions that are not built in.
Usually numeric variables are defined as doubles, you can ask if a variable belongs or not to a specific data type, here are some ways to do it.

Use Run-time Parameters in a Matlab-code S-Function

I have to write a Level-2 S-Function in Matlab code (C is not viable) which takes in some parameters and uses them alone and in combination.
First I tried tunable parameters, but unfortunately I haven't succeded in tuning them (the documentation doesn't tell how to set_param for this purpose).
Then I have bumped into documentation of Run-Time parameters that says
The value of a run-time parameter that corresponds to multiple dialog
parameters is typically a function of the values of the dialog
parameters.
So I deduced that a runtime parameter would suit well, but the same documentation doesn't say how to tune and access them from a Matlab S-function, only from a C one.
Also, in in this page is documented a RuntimePrm object that seems similar to DialogPrm, but trying to write the values result in an error.
Is there any way to "cache" parameter combinations without using DWorks, which would affect subsequent linearization with fake states?

How to override the help a function in matlab?

I have noticed that when a function is overridden the help fcn is not. I created a custom function using the same name as a built in function. Trying:
which fcn
Yields the right\function.m, but when I use the help function
help fcn
I get the help for the built in function. However, if I instead type
help fcn.m
I get the correct help. This is ambiguous and disturbing and may lead to confusion about which file is the used one. That is why I wonder: is it possible to override the help as well?.
I know that many does not recommend overriding in matlab, but there are times when it may be worth. For this occation it means that I does not need to change all legacy code where the function is used and also it is not good to have too many version of a function (mycdfplot is already taken and some may have local versions as well).
Short answer: NO.
(sorry)
To develop a bit:
If you overload your own function, you can force the help to point to a specific function by playing with the function precedence order and the order of the path. But for Matlab built-in function, they always take precedence over your custom functions.
The only way for the help to reach your custom function is how you described, by specifying .m
From Matlab (2013b) help:
Note: When multiple programs have the same name, the help command
determines which help text to display by applying the rules described
in Function Precedence
Order.
However, if a program has the same name as a MathWorks function, the
Help on Selection option in context menus always displays
documentation for the MathWorks function.