MATLAB Answers

.NET Assembly dll in C# with error "Not enough input arguments"

22 views (last 30 days)
Vlad Semiga
Vlad Semiga on 9 Jul 2019
Commented: Vlad Semiga on 10 Jul 2019
I am trying to get a simple .NET assembly working before I tackle the real thing. I am either getting "Not enough input arguments" and other times, "Not enough output arguments".
I have made sure that I have the matching MWArray.dll on my c# development computer.
MWArray and my Matlab created dll (TestAddCompiled.dll) have been referenced
I have left the commented out attempts I have made.
In the Matlab console: nargin is 3, nargout is 1
MATLAB R2019a, C# Visual Studio 2015
Matlab code:
function [c] = TestAdd(a,b,iAdd)
% Helper function used in testing type safe API.
%if ~isnumeric(a) || ~isnumeric(b)
% error('Input must be numeric. Input was %s and %s.', class(a), class(b));
switch iAdd
case 1
c = a + b;
case 0
c = Multiply(a,b);
c = Multiply(a,b);
% if iAdd==1
% c = a + b;
% else
% c = Multiply(a,b);
% end
function d = Multiply(a,b)
d = a * b;
Matlab library compiler:
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using TestAddCompiled;
namespace TestAddC
public partial class Form1 : Form
public Form1()
private void button1_Click(object sender, EventArgs e)
//initialize instance of Matlab class
clsAdd clsMyAdd = new clsAdd();
//Dead Code
/*MWNumericArray dFirst = new MWNumericArray(new Int64());
MWNumericArray dSecond = new MWNumericArray(new Int64());
MWNumericArray dThird = new MWNumericArray(new Int64());
dFirst = Convert.ToInt64(5);
dSecond = Convert.ToInt64(5);
dThird = Convert.ToInt64(5);*/
//End Dead Code
//Create array of inputs
Int64[] dArrInput = { 5,6,1 }; //also tried Int32 and double
MWArray[] dArrOutput = null;
//dArrOutput = 0;
// MWNumericArray mlArrInput = new MWNumericArray(dArrInput);
//Create and convert inputs to MWNumericArray
MWNumericArray inputs = null;
inputs = new MWNumericArray(1, 3, dArrInput);
MessageBox.Show("Inputs: " + inputs.ToString());
//double dOutput = 0;
//dArrOutput = clsMyAdd.TestAdd(1, dFirst, dSecond, dThird);
//dArrOutput = clsMyAdd.TestAdd(1, mlArrInput);
//dOutput = clsMyAdd.TestAdd(5,6,1);
//dArrOutput[0] = clsMyAdd.TestAdd(5, 6, 1);
//dArrOutput = clsMyAdd.TestAdd(1, 3, mlArrInput); //#output, #input, array
//dArrOutput = clsMyAdd.TestAdd(1, mlArrInput); //0 based #output, #input, array
dArrOutput = clsMyAdd.TestAdd(1, inputs); //1 output, inputs
System.Exception was unhandled
... MWMCR::EvaluateFunction error ...
Not enough input arguments.
Error in => TestAdd.m at line 8.
... Matlab M-code Stack Trace ...
file C:\Users\cbonneau\AppData\Local\Temp\cbonneau\mcrCache9.6\TestAd3\TestAddCompi\TestAdd.m, name TestAdd, line 8.
at MathWorks.MATLAB.NET.Utility.MWMCR.EvaluateFunction(String functionName, Int32 numArgsOut, Int32 numArgsIn, MWArray[] argsIn)
at MathWorks.MATLAB.NET.Utility.MWMCR.EvaluateFunction(Int32 numArgsOut, String functionName, MWArray[] argsIn)
at TestAddC.Form1.button1_Click(Object sender, EventArgs e) in C:\TestFolder\TestAddC\TestAddC\Form1.cs:line 61
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at TestAddC.Program.Main() in C:\TestFolder\TestAddC\TestAddC\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 10 Jul 2019
Since your TestAdd function requires three input arguments and your inputs in C# is one array, you need to put each input separately.
Please replace the last code (dArrOutput=...) in C# to the following.
dArrOutput = clsMyAdd.TestAdd(1, inputs[1], inputs[2], inputs[3]); //1 output, inputs
MessageBox.Show("Outputs: " + dArrOutput[0].ToString());

More Answers (0)

Sign in to answer this question.