Calllib with C++ out char *parameter

5 ビュー (過去 30 日間)
Justin Vuffray
Justin Vuffray 2021 年 4 月 7 日
回答済み: Rishav 2024 年 2 月 21 日
Hello,
I'm doing an example of my sdk. The c++ function on header is
int myfunction(int index, out char *parameter);
So my call in MATLAB is
strOutParameter = "00";
[intReturned, strOutParameter] = calllib('myLib', 'myfunction', index, strOutParamter);
I try it but it isn't working. I've an error :
Error using calllib
Parameter can not be converted to a character vector
I really don't code on MATLAB. I'm just doing a little example file of few lines for my users.
So my question is ; How to pass and get a out char * parameter ?
Thanks in advance

回答 (1 件)

Rishav
Rishav 2024 年 2 月 21 日
Hi Justin,
In MATLAB, when passing and receiving char* parameters to and from C++ functions using 'calllib' function, you need to handle them as MATLAB strings (char arrays).
Here is how you can modify your MATLAB code:
% Define the input parameters
index = 1;
strOutParameter = blanks(256);
% Call the C++ function using calllib
[intReturned, strOutParameter] = calllib('myLib', 'myfunction', index, strOutParameter);
% Convert the output parameter to a string
strOutParameter = strtrim(strOutParameter);
where,
  • blanks(256) return 1-by-n array of space characters.
  • strtrim is used to remove any trailing whitespace characters from the output parameter.
Please refer to the below documentations for more details on:
  1. blanks: https://www.mathworks.com/help/matlab/ref/blanks.html
  2. strtrim: https://www.mathworks.com/help/matlab/ref/strtrim.html

カテゴリ

Help Center および File ExchangeC Shared Library Integration についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by