开发者

MATLAB Builder NE (.NET Assembly) Data type question

I am using MATLAB Build开发者_运维百科er NE (MATLAB's integrated .NET assembly builder), but I am having an issue with data types.

I have compiled a small, very simple, function in MATLAB and build it for .NET. I am able to call the namespace and even the function just fine. However, my function returns a value, and MATLAB defaults to returning it as an object[] data type. However, I know that the value is an integer, but I can't figure out how to cast it.

My MATLAB function looks like this:

function addValue = Myfunction(value1, value2)

addValue=value1+value2;

end

Pretty simple right?

And then in .NET I can call it as:

xClass.addValue (1, 3, 4);

where xClass is the name of the MATLAB built class but when I try:

int x = xClass.addValue (1, 3, 4);

C# errors out. Typical .NET casting (int) doesn't work. The compiler states it cannot convert object[] to int.

Does anyone have experience with the .NET builder in MATLAB that can help me with this? It is really throwing me for a loop. I have scanned through most of the MATLAB BUILDER doc (484 pages!) with zero help.


I no longer have Builder NE, but if I remember correctly you can do something like:

using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;

int x = ((MWNumericArray)(xClass.addValue(1, 3, 4)).ToScalarInteger();

Using IntelliSense and the class browser on the MWArray, MWCellArray, MWStructArray, and MWNumericArray types was a lot more useful than the MATLAB documentation.

Having said all that, though, I'm confused by the first parameter in your addValue call?


What you are seeing in the immediate window is telling you that result[0] contains a two dimensional array that is of length 1 in both dimensions. The data is in the [0,0] element because the array is 0-based.

The following cast looks ugly, but will assign 7 to x:

int x = ((int[,])result[0])[0,0]


When I do that, I get a conversion error as well. However, I typed:

result[0] into the intermediate window and it came back with:

> {int[1, 1]}
>     [0, 0]: 7

I just can't isolate the "7" and it is driving me crazy. :-)


The result is Object[] because Matlab can return a vector of result parameters.

I haven't used Matlab NE for a while, so I can't remember the exact syntax, however, Matlab uses MWArray's, you'll have to examine MWArray's member to see what you have access to.

I think result[0] is a MWArray containing an int.


In my particular case, i have a double result.

 double suma=((double[,])xClass.addValue(1, 3, 4)[0])[0,0];

Then you can to convert to int

int x = Convert.ToInt32(suma);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜