Generate ST code from a look-up table with CONSTANT attribute

Hi,
I am using the 'Direct Lookup Table (n-D)' block in Simulink to load a matrix and extract the values given a row and a column number as inputs.
The Simulink setup is shown in the attached figure "lookup-table.png".
With this setup a PLC function block in structured text is generated from MATLAB/Simulink and everything is working as expected. However, the look-up table is defined within the function block as "VAR". I would like, if possible, to define it as "VAR_CONSTANT" instead. I have tried setting the in the lookup table attribute "StorageClass" type to "Const" but that has not worked.
Any idea/suggestion on how to store this table as "VAR_CONSTANT"?
Thanks in advance.

 Akzeptierte Antwort

Desiree
Desiree am 22 Mai 2025

1 Stimme

Hello Adrian,
Simulink PLC Coder does support CONSTANT attribute only in the context of VAR_GLOBAL, not as VAR_CONSTANT.
You can try postprocessing to change VAR to VAR_CONSTANT, but the question would be what is the exact requirement to have CONSTANT attribute for this VAR? Can you clarify?

4 Kommentare

Hi Desiree, Thank you for the reply. The created lookup table, if defined as a VAR within the function block, uses more than 128 kB of PLC application space which is more than the maximum usable size of the controller. Hope this answers your question. Regards, Adrian
Hello Adrian,
can you try manually changing the code and putting the VAR as VAR_CONSTANT? What difference does it then make in terms of PLC application space? What PLC IDE are you generating the code for?
As explained before you can also automate the process of marking a VAR as CONSTANT and generating it as such by using custom postprocessing.
Another question: would generating the Lookip Table Parameter as a global variable (Simulink.Parameter object with Const storage class) work for you as well? This would generate VAR_GLOBAL CONSTANT.
Best regards,
Desiree

Hi Desiree,

Yes, the process could be done manually, but I would rather have an automated solution. I did not come up with the idea of a post-processing via callback function as you suggested, so will definitely try that approach.

I tried defining the lookup table as a parameter with the "Const" storage class but it generates the table as a VAR_GLOBAL instead (without the CONSTANT attribute).

PS. I am using the Generic Target IDE PLC Coder.

Regards, Adrian

Hi Adrian,
we do not support VAR_GLOBAL CONSTANT for the generic targets "Generic" and "PLCopen XML". However you can activate it by adding path to the custom generic target:
addpath([matlabroot '\toolbox\plccoder\plugins\generic'])
savepath
plccoderpref('plctargetidepaths','default')
Then restart the MATLAB session.
Which IDE are you generating code for though? It might make more sense to directly generate for this IDE if it is supported. If not you could try following this workflow to adapt the code to you target IDE:
Hope this helps.
Best regards,
Desiree

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink PLC Coder finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Gefragt:

am 16 Mai 2025

Kommentiert:

am 3 Jun. 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by