From Matlab function to C-script block

Hello to everyone,

As the title states I want to shift from a matlab function to a c-script block. It’s so difficult for me write a code in C and for this reason I used the Matlab Coder.

Do you think that it could work? The Matlab Coder generates in addition to the main file also some other files but I don’t know how to put them (in the documentation is not explained well).

In the attachment you can find the matlab function and the c code generated.

Thank you

sortingQTu.rar (718 KB)

sortingQTu.m (2.32 KB)

Hello,

The PLECS C-Script block can be used to link to C library files or you can manually port the code from your embedded files into the appropriate functions, but in both cases, this may be cumbersome. You might consider to generate a DLL block that represents your logic, or the State Machine block, which is a bit more visually intuitive to work with than the C-Script block.

But looking at your .m file, you might just want to include this code as is (or with slight modifications) in the initialization commands of a subsystem. Out of curiosity, why are you trying to port it from MATLAB code to C code?

Regards,

Kris

Hello Kris,

Thank you for your answer. I want to port this function from Matlab to C because I want to reproduce the Nearest Level Control that I have used yet in Matlab. I need it because I’m using Plecs for a thermal calculation and it works with a C-script.

The code that I want to use in Plecs is exactly the same of the Matlab function, the inputs and the outputs are the same in both case.

What do you suggest?

Thank you

Hello,

Can you not continue to use the control code in MATLAB while using PLECS for thermal calculations though? Another option would be to try our processor-in-the-loop tool that directly links to the embedded target so you can fully test the control code. Do you already have the C code for your controller?

Best,

Kris

Hi, have you found the solution to this problem please? I’m having the same problem at the moment and I can’t seem to make any progress. If so, please help me.

Hi, please see the additional discussion below- is this helpful at all?