Problem passing parameter for MATLAB function from .NET

5 views (last 30 days)
I am trying to communicate between .NET and MATLAB.
I wrote the function in MATLAB and passing parameter from C#.net I caught an Error.
The MATLAB function is
function [out]= tabs(path,x,y,input)
load(path);
d=iddata(y,x,1);
y1=predict(amx,d);
a=y1.y(input);
out =a;
and c#.net code is
TABS.TABS_CLASS obj = new TABS.TABS_CLASS();
string inputx = "{11,12,13,14,15}";
string outputy = "{55,60,65,70,75}";
int pos = 1;
MWNumericArray mwchar = null;
mwchar = (MWNumericArray)(obj.tabs("C:\\5TAB\\TEST.mat", inputx, outputy, pos));
The error is:
MWMCR::EvaluateFunction error: Error using ==> iddata.iddata at 214
Error using ==> lower
Cell elements must be character arrays.Error in =>tabs.m at 3.
after that I changed the code to:
mwchar = (MWNumericArray)(obj.tabs("C:\\5TAB\\TEST.mat", (MWCharArray)inputx, (MWCharArray)outputy, (MWCharArray)pos));
now I get the error:
Unable to cast object of type 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' to type 'MathWorks.MATLAB.NET.Arrays.MWCharArray'.
after that I made this change:
MWCharArray temp;
temp = (MWCharArray)(obj.tabs("C:\\5TAB\\TEST.mat", (MWCharArray)inputx, (MWCharArray)outputy, (MWCharArray)pos));
And see the same error:
Unable to cast object of type 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' to type 'MathWorks.MATLAB.NET.Arrays.MWCharArray'.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 21 Jun 2011
Have you looked at the documentation example Using a COM Component in a .NET Application?
You are attempting to pass in 'x' and 'y' as strings or character arrays to the compiled MATLAB function which seems to expect numeric arrays. You should probably create double arrays (see the above example for usage) for 'inputx' and 'outputy'.
In the second part of your question, you cite code that attempts to convert an integer 'pos' to an MWCharArray - I think this is what is causing the error "Unable to cast object of type 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' to type 'MathWorks.MATLAB.NET.Arrays.MWCharArray'.". I would recommend declaring 'pos' as a double and pass it in directly.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!