Coder: issue replacing variables with constant values

6 views (last 30 days)
I am experiencing an issue with Coder. If I create a structure 'V' with a field 'A' and assign it a value of 3, and later call V.A in the m-code, the Coder output will not call v.a - it will replace it with "3". A basic example of the current code layout is below:
function [x]=helloworld(y)
persistent myconfig
if isempty(myconfig)
myconfig =loadmyconfig();
end
[x]=addnums(myconfig)
within loadmyconfig.m:
function [Data]= loadmyconfig ()
Data = struct(junk,double(1),...
A,1,...
B,41)
within addnums.m:
function [y]= addnums (myconfig)
y = myconfig.A+ myconfig.B;
would become, when compiled as an executable: within addnums.c:
%function [y]= addnums (myconfig) y = 1+ 41;
I was unable to locate anything in the online help that addresses this issue. Does anyone have any suggestions?

Answers (2)

Arnab De
Arnab De on 26 Apr 2013
Edited: Arnab De on 26 Apr 2013
If an expression (such as myconfig.A) has a constant value in all executions of the program, MATLAB Coder tries to use that constant value in place of the expression. This optimization makes the generated code efficient. Why do you consider this to be a problem?
  4 Comments
A.H.T.Eranga De Silva
A.H.T.Eranga De Silva on 12 Jan 2021
Hi Arnab and Jared,
Could you please let me know the solution for this issue?
I also encountered with the same issue in MATLAB
Thank you...
Eranga
Adam Danz
Adam Danz on 14 Jan 2021
@A.H.T.Eranga De Silva The order of the answers change according to votes and which answer is accepted. He's referencing the other answer on this page.

Sign in to comment.


Arnab De
Arnab De on 28 Apr 2013
Edited: Arnab De on 28 Apr 2013
MATLAB Coder only respects the interface of the entry point function. Although you did not specify your codegen command, I guess it was something like
codegen helloworld -args {1} ...;
which specifies helloworld as the only entry point function. Therefore Coder optimized other functions thinking that they are not called from outside. If you want to call loadmyconfig and addnums from outside, you must specify them as entry point functions. Your codegen command should be
a = struct('junk', 1, 'A', 1, 'B', 1);
codegen loadmyconfig addnums -args {a} ...;
The GUI also lets you specify multiple entry points. You do not need to add helloworld unless you need it. Do not add functions that you are not going to call from outside as entry points, because you'll lose out on optimizations.
BTW, you mentioned that you compiled the MATLAB code to executable. However, it appears that for your purpose, it will be better to compile them to a static/dynamic library. Also, it is recommended that you first test your code by converting it to mex.
  4 Comments
Jared
Jared on 29 Apr 2013
Edited: Jared on 29 Apr 2013
Here is the m-code:
% hello world
function x=helloworld(y)
persistent myconfig
myconfig=loadmyconfig();
x=addnums(myconfig)+y;
end
% load my config
function Data=loadmyconfig()
Data=struct('junk',double(1),...
'A',1,...
'B',41);
end
% add nums
function y=addnums(myconfig)
y=myconfig.A+myconfig.B;
Calling the codegen command in the order previously listed generates an error. The code below is what I used instead:
y=double(7);
cfg = coder.config('exe');
cfg.CustomSource = 'main.c';
codegen -config cfg helloworld -args {y} loadmyconfig
This generates (helloworld.c):
real_T helloworld(real_T y) { return 42.0 + y; }
I am using 2012b.
Arnab De
Arnab De on 30 Apr 2013
As I said, Coder only respects the interface of the entry point functions. You specified helloworld and loadmyconfig as entry points and it did not change the signatures of those functions --- see helloworld.h/.c and loadmyconfig.h/.c. If you added addnums to your entry point list, it would have preserved the interface of addnums too (it would be in addnums.h/.c). Additionally, the structure would be declared in helloworld_types.h. However, the compiler is free to optimize the codes of these functions in any way it sees fit. In your case, you'll be calling the functions loadmyconfig and addnums from outside --- so as long as the interface of these functions are preserved, it should work for you. It does not matter if it has optimized calls to these functions in helloworld.c. Run the codegen command
a = struct('junk', 1, 'A', 1, 'B', 1);
codegen -config:lib helloworld -args {1} loadmyconfig addnums -args {a};
You should find the generated files in codegen/lib/helloworld/.
I also suggest that you go through the tutorials and documentation once. You can also contact MathWorks tech support.

Sign in to comment.

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!