How can I determine if a script is being run by the codegen -test input parameter?

6 vues (au cours des 30 derniers jours)
I would like to avoid running certain lines of code when runing a script that calls a generated mex file (codegen -test or coder.runTest).
I have a script that builds an entry point function "functionA"
codegen -config cfg functionA -args ARGS -test testScript
And I would like to avoid running certain lines of code within testScript only when running the test with codegen (i.e. in the codegen command or with coder.runTest). Is this possible, and if so how.
My first instinct was ~coder.target('MATLAB'), isdeployed, ismcc, however none of these return a true flag when running in test mode.

Réponse acceptée

Mukund Sankaran
Mukund Sankaran le 31 Mar 2020
As far as I'm aware, coder.runTest and codegen -test do not support this behavior. Can you please let us know more about your use case, so we can consider supporting this in the future ?
As a workaround, you could perhaps set a global variable outside the test bench, and check the value of the global while running the test bench. Say something like this:
% Entry point function foo
function out = foo(a, b)
out = a + b;
end
% Test bench foo_tb
function foo_tb()
global tbFlag;
if tbFlag
foo(3, 5);
else
disp('Do Nothing');
end
end
% set global tbFlag
function setGlobalTbFlag(value)
global tbFlag;
tbFlag = value;
end
% build script
setGlobalTbFlag(true);
codegen -config cfg foo -args {2,3} -test foo_tb
setGlobalTbFlag(false);
  3 commentaires
Mukund Sankaran
Mukund Sankaran le 1 Avr 2020
Modifié(e) : Mukund Sankaran le 1 Avr 2020
Thanks for letting us know your use case. Another option you could try, if you'd like to avoid using globals is to use the dbstack function (https://www.mathworks.com/help/matlab/ref/dbstack.html) inside the testScript which will let you inspect functions in the call stack. That way, you can check if runTest or codegen is in the call stack. Here's an example:
function foo_tb()
usingCoder = false;
st = dbstack();
funcs = {st.name};
if any(strcmpi('runTest', funcs)) || any(strcmpi('codegen', funcs))
usingCoder = true;
end
if ~usingCoder
clear;clc;close all;
end
foo(3, 5);
end
Sasanka Pratapa
Sasanka Pratapa le 14 Sep 2022
Function 'dbstack' not supported for code generation. ?

Connectez-vous pour commenter.

Plus de réponses (1)

Noam Greenboim
Noam Greenboim le 7 Mai 2024
you can use the
coder.target
function to determine whether it's a mex or matlab.

Catégories

En savoir plus sur MATLAB Coder dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by