How to pass a string into a Matlab function block?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!