Coder: Prevent code optimization replacing a variable with its constant value

12 views (last 30 days)
Hello
I'm using Matlab coder to code generate. Example:
N = 31;
for 1:N
%calculating things
end
So my variable N has a constant value, but is used everywhere in my code to define lengths, loops etc. Now when I code generate the variable N is substituted for the value 31 everywhere in the generated C code. I want to prevent this as N, even though defined as a constant in the code, we do change often and I would like to be able to just open up the c file and change the value there and not having to code generate all over again. It can neither be an input. Is there a way to tell Matlab Coder to keep the variable N and not optimize it away?
Best Regards MC

Answers (1)

Owen Claxton
Owen Claxton on 21 Sep 2021
Hi Michel C,
After an indepth dive into the documentation, I stumbled upon this link: https://au.mathworks.com/help/coder/ref/codegen.html
Hopefully this works for your C code, however note that I am working on the problem in the context of ROS and C++.
By defining a global variable T in my code, like this:
function test
global T;
A = uint8(2);
out1 = test_helper(A);
fprintf('\tA: %u\n', out1);
function output = test_helper(input)
if input > 3
output = uint8(2) + T;
else
output = uint8(1) + T;
end
end
end
and editting my codegen line to:
%% Some extra junk relating to what I'm working on. Hopefully it works
%% without, but just in case:
cfg = coder.config('exe');
cfg.Hardware = coder.hardware('Robot Operating System (ROS)');
cfg.HardwareImplementation.ProdHWDeviceType = 'ARM Compatible->ARM Cortex';
cfg.GenCodeOnly = true;
cfg.Hardware.BuildAction = 'None';
cfg.PreserveVariableNames = 'UserNames';
cfg.Verbosity = 'Verbose';
% Codegen:
globals_def = {'T', 1};
codegen -globals globals_def test -args {} -config cfg
MATLAB generated some definitions for the variable within the src, in a file called 'test_initialise.cpp':
//
// Academic License - for use in teaching, academic research, and meeting
// course requirements at degree granting institutions only. Not for
// government, commercial, or other organizational use.
// File: test_initialize.cpp
//
// MATLAB Coder version : 5.2
// C/C++ source code generated on : 21-Sep-2021 18:06:42
//
// Include Files
#include "test_initialize.h"
#include "test_data.h"
// Function Definitions
//
// Arguments : void
// Return Type : void
//
void test_initialize()
{
T = 10.0;
isInitialized_test = true;
}
//
// File trailer for test_initialize.cpp
//
// [EOF]
//
As you can see, T is there and defined. Furthermore, in the actual 'test.cpp':
//
// Academic License - for use in teaching, academic research, and meeting
// course requirements at degree granting institutions only. Not for
// government, commercial, or other organizational use.
// File: test.cpp
//
// MATLAB Coder version : 5.2
// C/C++ source code generated on : 21-Sep-2021 18:06:42
//
// Include Files
#include "test.h"
#include "test_data.h"
#include "test_initialize.h"
#include <cmath>
#include <stdio.h>
// Function Definitions
//
// Arguments : void
// Return Type : void
//
void test()
{
double d;
unsigned char u;
if (!isInitialized_test) {
test_initialize();
}
d = std::round(T + 1.0);
if (d < 256.0) {
if (d >= 0.0) {
u = static_cast<unsigned char>(d);
} else {
u = 0U;
}
} else if (d >= 256.0) {
u = MAX_uint8_T;
} else {
u = 0U;
}
printf("\tA: %u\n", u);
fflush(stdout);
}
//
// File trailer for test.cpp
//
// [EOF]
//
You can see T is indeed contained within the script.
Best of luck!

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!