(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 Comments

for example,when i input imagePathstr "../data/preSavedData/database.yml.gz", then pass to myFunction as follows,last character is garbled!
../data/preSavedData/database.yml.gz�
But when I use the relative current ". /" paths or absolute paths, the last characters are not garbled, which is strange!
  • "./../data/preSavedData/database.yml.gz"
  • "C:/temp/data/preSavedData/database.yml.gz"
These two approaches work!
I would be interested to know what the binary value is of the character inserted.
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.
@Walter Roberson Unfortunately, this time I went to the last time parent folder and used ". /" relative path instead of using ".. /" in subfolder and still got an error and this is the result of the printed string(last two character)
-----
I'm also guessing null or "\0" or some such terminator, but what is the exact measure of how to do this? Where does the code need to be changed? How do I change it? thanks for your help again!
-----
During time, I tried to print characters like this:
const char *mystr = NULL;
//std::string myTT(mystr);
const char charNULL = '\0';
char charBinary = '0';
printf("charNULL:%d,%c,%x,%x \n", charNULL, charNULL, charNULL, mystr);
output:
charNULL:0, ,0,0

Sign in to comment.

 Accepted Answer

That's probably a NUL character, binary 0 ,
You should test it to see what the binary value is.

3 Comments

I initially thought the same thing, there is a "\0" character at the end of the string in C, but I don't know how to verify if it is "\0" or NULL, what is the practical approach?
Unfortunately, this time I used ". /" relative paths and still got an error(see above comment)
Thank you very much, I tried multiple paths and it worked, you can refine your answer so I can accept it

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!