How to pass a string into a Matlab function block?

9 Ansichten (letzte 30 Tage)
Ursula Garcia
Ursula Garcia am 29 Apr. 2013
Kommentiert: Maharshi Patel am 29 Sep. 2020
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

Akzeptierte Antwort

Ryan Livingston
Ryan Livingston am 14 Mai 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 Kommentar
Maharshi Patel
Maharshi Patel am 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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by