How to marshal to MATLAB COM Interop

2 views (last 30 days)
Hello,
I'm pretty sure I am seeing a marshaling issue interfacing with MATLAB. I've got a client wiring up just fine in a C# program, and want to call fft, I gather through the Feval call.
Question (or questions) is (are), how to specify and/or marshal the requisite inputs and outputs in such a way that we get the desired response.
There' a follow on call in many of the fft examples to linspace, but I gather that technically this is optional, unless we want to wire up for visualization or otherwise.
I've verified on the "simple" hello world scenario, I call Feval to "version" and obtain the desired response. Now I want to step it up a notch for fft, and so on.
Next from that, various bandpass filters.
I should also mention, we're running against R2011b.
Thank you.

Accepted Answer

Michael
Michael on 4 Jun 2014
Working through the examples via COM Interop, I am able to make a similar call to the "linspace".
matlab.Feval(@"linspace", 1, out lso, 0d, 1d, nfft / 2 + 1);
And with a bit of C# extension method and lambda persuasion, extract out the desired result.
So at this point I am thinking it's something in how arrays are being passed into MATLAB.
Am I in the ball park if I assume (which I am loathe to do) that it prefers arrays to be 2-dimensional, even if we're talking about a "single dimension" of sampled data? So in other words, instead of double[] (C#), I should be transforming that into double[,]?
Thank you...
  1 Comment
Michael
Michael on 5 Jun 2014
That did the trick. Deal with "arrays" (loosely speaking) as terms of two dimensional arrays. Trick is to get the row/column-major gymnastics correct.

Sign in to comment.

More Answers (2)

Michael
Michael on 4 Jun 2014
I am receiving the following exception:
System.Reflection.TargetInvocationException occurred HResult=-2146232828 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) InnerException: System.Runtime.InteropServices.COMException HResult=-2147467259 Message=Undefined function 'fft' for input arguments of type 'int32'. Source=Matlab.MLApp ErrorCode=-2147467259 InnerException:
And the call is something like this:
object temp; matlab.Feval(@"fft", 1, out temp, y, y.Length, nfft);
where y is the signal generated sample double[] (C# language level), and nfft is the next power of two length.

Michael
Michael on 4 Jun 2014
I have also verified "which fft" over the COM client connection, and received the valid response: "built-in (C:\\Program Files\\MATLAB\\R2011b\\toolbox\\matlab\\datafun\\@logical\\fft)". So it's not because fft could not be identified. It's got something to do with language-level expected params, COM Interop marshaling, something like that I think.

Categories

Find more on COM Component Integration in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!