MATLAB Answers

How to pass a string into a Matlab function block?

13 views (last 30 days)
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

Accepted Answer

Ryan Livingston
Ryan Livingston on 14 May 2013
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 Comment
Maharshi Patel
Maharshi Patel on 29 Sep 2020
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)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!