How to call C# ENUM with values whose name start with underscore?
5 views (last 30 days)
Show older comments
MathWorks Support Team
on 24 May 2018
Answered: MathWorks Support Team
on 24 May 2018
I like to call a C# function in MATLAB.
The C# function has ENUM with values whose names start with underscores.
And a function which accepts the Enum as argument, something like this--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace firstdotnet
{
public enum RF_Amp_Atten_Step
{
_3dB = 8,
_9dB = 17,
_12dB = 18,
_15dB = 19,
_6dB = 28,
aa = 25,
abc_ = 30,
Full_Core = 56,
}
public class fistclass
{
public int callmylib(RF_Amp_Atten_Step e)
{
return (int)e;
}
}
}
Now, to call this function in MATLAB, I compile this program into assembly, and using Net.addAssembly and call assembly's function.
asm = NET.addAssembly('<FullPathToAssembly>\firstdotnet.dll');
cls = firstdotnet.fistclass;
If I use Enum's value whose name start with letter, then it works fine-
res = callmylib(cls, firstdotnet.RF_Amp_Atten_Step.Full_Core);
But, when calling the function with Enum's value whose name start with underscore gives error- ERROR: res = callmylib(cls, firstdotnet.RF_Amp_Atten_Step._12dB); ↑ Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
I know that a valid variable name in MATLAB can only start with a letter, followed by letters, digits, or underscores.
And, the Enum that we are passing to function is a MATLAB struct which cannot have invalid variable names.
But, is there a way I can use those Enum values?
I cannot modify my C# code.
Accepted Answer
MathWorks Support Team
on 24 May 2018
If you type in firstdotnet.RF_Amp_Atten_Step. and then hit tab, the MATLAB autocomplete brings up a list of the possible values.
In there you'll notice that all the underscore dB values have been renamed such that the underscore is at the end (3dB_, 6dB_, etc.).
I think the .NET framework or MATLAB renamed it.
The underscore throws off the MATLAB interpreter if you try to access it directly.
To workaround this, you can use the dot-parent syntax with the renamed version of ENUM value-
res = callmylib(cls, firstdotnet.RF_Amp_Atten_Step.('12dB_'))
This will let you use all ENUM values without modifying your C# code.
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Compiler SDK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!