Please take a minute to review and accept our Terms of Use.
Welcome to the PLECS User Forum, where you can ask questions and receive answers from other members of the community.

Many technical questions regarding PLECS are answered on the Technical Solutions page of our website. Tutorial videos, specific application examples, and pre-recorded webinars are available on our YouTube page. Please follow us on LinkedIn for the latest Plexim news.

From Matlab function to C-script block

0 votes
116 views
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
asked Apr 9 by Marzio96 (21 points)

1 Answer

0 votes
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
answered Apr 10 by Kris Eberle (478 points)
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
...