How to pass a string into a Matlab function block?

9 ビュー (過去 30 日間)
Ursula Garcia
Ursula Garcia 2013 年 4 月 29 日
コメント済み: Maharshi Patel 2020 年 9 月 29 日
Hello all, I want to pass a string into a Matlab function block.
This string is a mask parameter that must not be evaluated (it's a name). I'll try to pass it via a structure like this:
function Out = GetDiagAvail_mf(structArg)
%#codegen
coder.extrinsic('disp');
Out = coder.nullcopy(uint8(0));
disp(['in EML code, varName = ',structArg.varName]);
Out = coder.ceval('MyFct',coder.opaque('const char *',structArg.varName));
structArg is built in mask Initialization Pane like this: structArg.varName = Param;
'Param' value could be: 'MyString'.
I want to have in generated code: MyFct(MyString);
But I have this error:
_Error c2_lib_DSM.c: 161 undeclared identifier `MyString'
Warning c2_lib_DSM.c: 161 possible usage of MyString before definition
Warning c2_lib_DSM.c: 318 static `void function(pointer to void,pointer to const incomplete struct mxArray_tag defined at C:\Program Files\MATLAB\R2011b\extern\include\matrix.h 299,pointer to const char,pointer to void) c2_c_sf_marshallIn' is not referenced
Warning c2_lib_DSM.c: 291 static `pointer to const incomplete struct mxArray_tag defined at C:\Program Files\MATLAB\R2011b\extern\include\matrix.h 299 function(pointer to void,pointer to void) c2_c_sf_marshallOut' is not referenced _
1 errors, 3 warnings
Please, can you help me?
I'm using Matlab 2011b.
Thanks in advance,
Ursula

採用された回答

Ryan Livingston
Ryan Livingston 2013 年 5 月 14 日
Hi Ursula,
You should simply be able to pass a MATLAB string to coder.ceval:
Out = coder.ceval('MyFct', structArg.varName);
Note that it will not be null-terminated automatically so that is required if "MyFct" expects a null-terminated C string:
Out = coder.ceval('MyFct', [structArg.varName char(0)]);
  1 件のコメント
Maharshi Patel
Maharshi Patel 2020 年 9 月 29 日
Also, to pass a string as an argument to MATLAB function, you will need to uncheck the Tunable option for 'structArg' in the Ports and Data Manager (via 'Edit Data' button in Editor tab)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by