Problem passing parameter for MATLAB function from .NET
5 views (last 30 days)
Show older comments
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'.
0 Comments
Accepted Answer
Kaustubha Govind
on 21 Jun 2011
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)
See Also
Categories
Find more on Deploy to .NET Applications Using MWArray API 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!