(matlab coder)Generating C++ code for scalar string results in a garbled last character?
古いコメントを表示
my entry-point codegen function:
function out = useImageAndString(imagePathstr)%#codegen
arguments
imagePathstr (1,1) string = "./input.txt"
end
% https://www.mathworks.com/help/coder/ug/define-input-properties-programmatically-in-the-matlab-file.html
assert(isstring(imagePathstr));
assert(numel(imagePathstr)==1);
% include external C++ functions
coder.cinclude('test1.h');
coder.updateBuildInfo('addSourceFiles', 'test1.cpp');
% call C++ function
imgPath = char(imagePathstr); % Must convert to char type to pass to coder.ref/coder.rref ???
coder.ceval('myFunction',coder.rref(imgPath));
end
Generate C++ code for the above function in command line:
s = "./input.txt";
t = coder.typeof(s);
t.StringLength = 255;
t.VariableStringLength = true;
codegen -config:mex useImageAndString -args {t} -launchreport -lang:c++
I can generate C++ code successfully, but the first parameter "imagePathstr" passed to my C++ occasionally results(const char* imagePathstr) in a garbled last character. So how can I fix it?
test1.cpp:
void myFunction(const char* imagePathstr)
{
std::string dbFile(imagePathstr);
std::cout << "database name:" << dbFile << std::endl;// The last character is garbled ???
... do something
}
RUN in R2022b
5 件のコメント
xingxingcui
2023 年 2 月 23 日
編集済み: xingxingcui
2023 年 2 月 23 日
xingxingcui
2023 年 2 月 23 日
Walter Roberson
2023 年 2 月 23 日
I would be interested to know what the binary value is of the character inserted.
Walter Roberson
2023 年 2 月 23 日
I wonder if it is simply passing the array of bytes without null-padding it? You might need to specifically put on the null, possibly.
xingxingcui
2023 年 2 月 23 日
編集済み: xingxingcui
2023 年 2 月 23 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Use Prebuilt MATLAB Interface to C/C++ Library についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!