Static Variables in Simulink S-Function Builder - matlab

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.

Related

What is the difference between a regular label and procedure?

Recently I have started making code on 6502-based systems, and I have used the ca65 macro assembler. However I found out that it supports procedures using .proc . So I have been wondering what is the difference between these blocks of code:
mainLabel:
jsr subroutine
subroutine:
;Code
rts
and this code:
mainLabel:
jsr procedure
.proc procedure
;Code
rts
.endproc
When I try running my programs using these 2 syntaxes I seem to get the same result. From what I can tell from the ca65 documentation, procedures prevent code from outside it from entering labels within it.
It's the same as far as the CPU is concerned. The benefit to using .proc is so that you can use common label names like loop, again, etc. locally in multiple different functions without the assembler throwing a fit that you used the same label twice. Otherwise you'd have to come up with increasingly contrived label names for every function you write. (Trust me, I've been there.) Let the computer do that for you!

Best way to initialize Matlab parameters based on the machine

I am currently in a stage where I would like to have my code modularized and following software-engineering techniques to make it reusable and understandable. In particular, I run my code either in my laptop and in an external server.
My goal is to have the main part of the code exactly the same in the laptop and the server, but different initialization parameters in the two case (in the server I will increase the # of iterations for instance and other variables). What is the common practice to do so, apart from clearly an if-else statement in the main?
I was thinking of an initialization file (like a JSON) in the laptop and the server, which is different and I just need to modify the values. Or a Matlab function which initializes the variables, but still, it would have an if-else statement.
Other suggestions? Keep in mind I might to want to extend the algorithmic part and introduce new parameters in the future.
Thanks

How to force Matlab/Simulink Coder to use the parameters created in simulink

I have a simulink model using matlab function blocks.
When i try to generate the C code from my model, the structures parameter scopes i used to represent my data are unused :
When i say unused i mean,
matlab coder creates a header file with all my structures defined.
but in the actual algorithms, when the structure should be sent as argument to a function, matlab coder just defines new variables for each of the fields of which values are hardcoded.
So something like : Function(parameter); with parameter contraining X = 5 and Y = 8 becomes Function(5,8); once generated (so the function definition creating new variables for each of the fields).
You can imagine how messy that gets once the structures get too big.
A friend of mine told me objects dont work with matlab coder. So replacing my structs with objects is not an option unless my friend was wrong.
Does anyone know how i could force matlab coder to actually use the structures i defined for him?
Or maybe there is another solution that i did not think of?
Thanks!
I have found the answer to my own question.
In the configuration file of the code generation, under "Optimization" tab, change "default parameter behaviour" from 'inlined' to 'tunable'.
I hope this will help others :)

Why Matlab Coder is slow?

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.

How to access an array of structs in simulink?

I have the problem, that I have to access a funktion form a dll in matlab/Simulink in the rtw.
This should work with a s function, but I have the needed parameters in a array of structures organized.
The question is now how I can reach them when I want to call my DLL function?
Or is there a better way (e.g. level 2 Matlab files or something similar)?
The pure simulation (without RTW) worked pretty well with level 2 m files but I am not able to write a tlc file for compiling them. I did not find much on the net and the documentation only about C sources.
Thanks
Christian
For signals in Simulink, what you are asking for is an array of buses. There is similar support for using arrays of structs for parameters. For calling an external function, you might want to look at the legacy code tool. You might also be able to use the MATLAB function block to call your external dll.
In addition to what #MikeT says:
Generating code from Level 2 M-S-Functions is problematic. Read this: http://www.mathworks.co.uk/help/toolbox/simulink/sfg/f7-67622.html#brgscav-1
Also, M-S-functions are generally slow, because they run in the MATLAB interpreter: http://blogs.mathworks.com/seth/2010/10/28/tips-for-simulation-performance/
In the end I coded the problem in C and used an array where I defined to order of the elements. Then I wrote some interface functions to access this "virtual" struct.
This is not very good coding but the easiest way I have found and it is portable.
Thanks