question: Use of integrated external C/C++ code in matlab coder does not work for coder.varsize?

조회 수: 3 (최근 30일)
When I follow this topic "Call Custom C/C++ Code from the Generated Code" to generate my own external/legacy C++ code, coder.varsize('myvar') does not work. How do I declare 'myvar' to be a variable-length character vector or a variable-length scalar string?
function out = useImageAndString(imagePathList,image1)%#codegen
% generated C++ code, mainly to see the values passed for the input and output parameters
%
% reference:
% [1] coder.ceval
% [2] coder.opaque
arguments
imagePathList (1,1) string = "input.txt"
image1 (100,200) uint8 = zeros(100,200,'uint8')
end
if ~coder.target("MATLAB")
out = zeros(size(image1),"uint8");
% include external C++ functions
coder.cinclude('test1.h');
coder.updateBuildInfo('addSourceFiles', 'test1.cpp');
imgPath = char(imagePathList);% must convert to char???, can't pass string scalar to coder.rref, this is bug!
imgPath = 'input.txt'; % or i directly specify imgPath, also not work.
coder.varsize('imgPath'); % bug
coder.ceval('getImageNames',coder.rref(imgPath));
% call C++ function
coder.ceval('getImage', coder.rref(image1),coder.wref(out));
end
end
my test1.h like this:
void getImageNames(const char* imgList);
void getImage(const unsigned char inImage[20000],unsigned char outImage[20000]);
The input parameter `imgList` to the getImageNames function is a variable-length character vector or a variable-length scalar string. I tried coder.varsize to declare it, but the generated C++ code still fixes the length size with my example, how can I fix it?
codegen -config:mex useImageAndString -args {"111.txt",ones(100,200,"uint8")} -launchreport -lang:c++
For example, the example command above generates C++ code that fixes imagePathList argument the length to 7
run in R2022b
  댓글 수: 1
cui,xingxing
cui,xingxing 2023년 2월 17일
편집: cui,xingxing 2023년 2월 17일
I understand that the R2022b is starting to support scalar string variable length input, for example the following solution could be a temporary solution to my problem.
s = "input.txt";
t = coder.typeof(s);
t.StringLength = 255;
t.VariableStringLength = true;
then,
codegen myFunction -args {t}
reference:
--------
when i use a string scalar pass coder.ref or coder.rref directly(ie. not use coder.varsize),it report error:
run in R2022b

댓글을 달려면 로그인하십시오.

채택된 답변

cui,xingxing
cui,xingxing 2023년 2월 18일
After some careful research, I found that coder.varsize is not supported for entry function input parameters and there are many restrictions on code generation, see here for details.(so you can use coder.typeof to solve the entry function problem)
In addition, the latest version of coder.ref/coder.rref does not support scalar string input. The matlab string type is a type that has become popular in the last few years, and is officially used as a custom string type in the corresponding generated C language type, so it does not correspond directly to the std::string type in C++. I'm sure future versions will take this ease of use into account!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Generating Code에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by