How to call C# ENUM with values whose name start with underscore?
조회 수: 5 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2018년 5월 24일
답변: MathWorks Support Team
2018년 5월 24일
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.
채택된 답변
MathWorks Support Team
2018년 5월 24일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler SDK에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!