title: MATLAB .NET assembly (Undefined function or variable 'feedforwardnet')
body: <p>I'm trying to integrate <a href="http://en.wikipedia.org/wiki/MATLAB" rel="nofollow">MATLAB</a> 2010b with Visual Studio 2008 Professional.</p>
  
  <p>I have the following MATLAB method.</p>
  
  <pre><code>function varargout = perform( func, varargin )
  
  %% // Set default values
  workspaceDirectory = ['Results/MatlabWorkspace_' datestr(now, 'yyyy-mm-dd_HH-MM-SS')];
  
  clear args
  args = struct('workspacePath', [ workspaceDirectory '/workspace.mat'], ...
                'testArray', [], ...
                'k', 50, ...
                'rate', 0.0001, ...
                'trainingDataPath', 'P2AT_LaserImageVectorList.csv', ...
                'network', 'feedforwardnet', ...
                'initialWeights', [], ...
                'divideFcn', 'dividerand', ...
                'trainRatio', 70/100, ...
                'testRatio', 15/100, ...
                'valRatio', 15/100, ...
                'trainInd', [], ...
                'testInd', [], ...
                'valInd', [], ...
                'trainFcn', 'trainlm', ...
                'performFcn', 'mse', ...
                'biasConnect', [0; 0; 0], ...
                'layerSize', [9; 4; 1], ...
                'layerTransferFcn', ['tansig '; 'tansig '; 'purelin'], ...
                'max_fail', 10, ...
                'mu_dec', 0.04, ...
                'useInitialWeights', false, ...
                'saveResults', true);
  
  % // Returns a modified properties structure
  args = getopt(args,varargin);
  
  args.layerTransferFcn = cellstr(args.layerTransferFcn);
  
  if args.saveResults &amp;&amp; ~strcmpi(func,'test')
      if (exist(workspaceDirectory, 'dir') ~= 7)
          mkdir(workspaceDirectory);
      end
  end
  
  if (strcmpi(func,'test'))
      try
          load(args.workspacePath, '-regexp', '.*');
      catch err
          Warning(err.message);
          varargin{1,1} = null;
          return;
      end
  
      data_inputAngle = args.testArray(2501);
      data_inputPCA = args.testArray(1:2500);
  
      if size(data_inputPCA,1) == 1
          data_inputPCA = data_inputPCA';
      end
  
      switch(featureExtractionMethod)
          case {'gha','apex'}
              % // [W, errvals] = gha(data_inputPCA, k, varargin{1,3});
              data_PCs  = W' * data_inputPCA;
              data_inputNN = [data_PCs; data_inputAngle];
  
          case 'nnmf'
              % // [W,H,D] = nnmf(data_inputPCA',k);
              data_PCs  = H * data_inputPCA;
              data_inputNN = [data_PCs; data_inputAngle];
  
          case 'pcaProcess'
  
          otherwise
              warning = 'ERROR: no feature extraction method has been defined.';
              Warning('ERROR: no feature extraction method has been defined.');
              varargout{1,1} = null;
              return;
      end
  
      % // Just to test to see if it recognizes 'feedforwardnet'.
      testnet = feedforwardnet;    % // &lt;------------------------------- LINE 81
  
      % // Saving all the workspace variables to see if they are all correctly processed.
      save('all');
  
      varargout{1,1} = net(data_inputNN); %// &lt;------------------------- LINE 86
  
      end
  end
  </code></pre>
  
  <p>And this is how I create my DLL file to import in Visual Studio:</p>
  
  <pre><code>%%// Determine path names
  workdir = pwd();
  outdir = fullfile(workdir, 'Output');
  dnetdir = fullfile(workdir, 'dotnet');
  
  %%// Determine file names
  mfile = fullfile(workdir, 'perform.m');
  dnetdll = fullfile(dnetdir, 'dotnet.dll');
  
  %%// Build .NET Assembly
  eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
          'dotnetclass,0.0,private'' -T link:lib ''' mfile '''']);
  </code></pre>
  
  <p>So everything works perfectly fine when I use <a href="http://www.mathworks.se/help/matlab/matlab_external/using-matlab-engine.html" rel="nofollow">MATLAB Engine's</a> <a href="http://en.wikipedia.org/wiki/Component_Object_Model" rel="nofollow">COM</a> interface to run the routine inside MATLAB from C#:</p>
  
  <pre><code>/*
   * This function calls the routine inside
   * MATLAB using the MATLAB Engine's COM interface
   */
  static private float MatlabTestDebug(float[] testData, Double targetAngle)
      {
          Array X = new double[testData.Length + 1];
          testData.CopyTo(X, 0);
          X.SetValue((double)targetAngle, testData.Length);
  
          Array zerosX = new double[X.GetLength(0)];
  
          MLApp.MLAppClass matlab = new MLApp.MLAppClass();
  
          matlab.PutFullMatrix("testArray", "base", X, zerosX);
          matlab.PutWorkspaceData("workspacePath", "base", "workspace.mat");
  
          // Using Engine Interface, execute the ML command
          // contained in quotes.
          matlab.Execute("cd 'c:\\Users\\H\\Documents\\Project\\Source Code\\MatlabFiles';");
          matlab.Execute("open perform.m");
          matlab.Execute("dbstop in perform.m");
          matlab.Execute("result = perform('test', 'workspacePath', 'workspace.mat', 'testArray', testArray);");
          matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");
  
          return (double)matlab.GetVariable("result", "base");
      }
  </code></pre>
  
  <p>But when I use the .NET assembly, it's not recognizing 'feedforwardnet'. I used to get an error on line 86 (<code>net(data_inputNN)</code>). So I added a line to test to see if it at least recognizes 'feedforwardnet', but it didn't.</p>
  
  <p>Note: I'm loading some variables from a file including "net" which is a neural network (<code>load(args.workspacePath, '-regexp', '.*');</code>)</p>
  
  <p>Also in the MATLAB method if I load a saved "network" from file and then save it to see how it processes the network, it will save it as a "struct" instead of a "network".</p>
  
  <p>I'm assuming it's loading it as a struct to begin with.</p>
  
  <p>I also had this problem inside MATLAB 2009b itself. That's the reason I'm using MATLAB 2010b now, because apparently MATLAB 2009b didn't have this particular neural networks toolbox.</p>
  
  <p>Following is the C# code to use the .NET assembly.</p>
  
  <pre><code>/*
   * Calls the method from inside a .NET assembly created with MATLAB
   * using Builder for .NET.
   */
  private float MatlabTest(float[] testData, Double targetAngle)
      {
          if (testData != null)
          {
              dotnetclass AClass = new dotnetclass();
  
              Array X = new double[testData.Length + 1];
              testData.CopyTo(X, 0);
              X.SetValue((double)targetAngle, testData.Length);
  
              MWNumericArray XNumericArray = new MWNumericArray(X);
  
              MWArray[] RetVal = AClass.perform(1, "test",
                  "workspacePath", "workspace.mat",
                  "testArray", XNumericArray);
  
              Array result = ((MWNumericArray)RetVal[0]).ToVector(MWArrayComponent.Real);
  
              return (float)result.GetValue(0);
          }
          else
          {
              return 0f;
          }
      }
  </code></pre>
  
  <p>I'm getting this error in Visual Studio:</p>
  
  <pre><code>... MWMCR::EvaluateFunction error ...
  Undefined function or variable 'feedforwardnet'.
  Error in =&gt; perform.m at line 81.
  </code></pre>
  
  <hr>
  
  <p>NOTE: version of my compiler and softwares:</p>
  
  <p>Compiler: Microsoft Visual C++ 2008 SP1 in <code>C:\Program Files (x86)\Microsoft Visual Studio 9.0</code></p>
  
  <p>MATLAB: R2010b (64-bit)</p>
  
  <p>Visual Studio: MVS 2008 professional (.NET Framework 3.5 SP1)</p>
  
  <p>Microsoft Windows SDK 6.1</p>
  
  <hr>
  
  <p><strong>Recent Updates:</strong></p>
  
  <p>I've added the path of the neural network toolbox in <a href="http://www-rohan.sdsu.edu/doc/matlab/toolbox/compiler/mcc.html" rel="nofollow">mcc</a>.</p>
  
  <pre><code>eval(['mcc -N -p ''C:\Program Files\MATLAB\R2010b\toolbox\nnet'' -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
      'dotnetclass,0.0,private'' -T link:lib -v ''' mfile '''']);
  </code></pre>
  
  <p>Now I get these messages in <code>mccExcludeFiles.log</code>:</p>
  
  <blockquote>
    <p>C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\@network\network.m</p>
    
    <blockquote>
      <p>called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\cascadeforwardnet.m
      (because of toolbox compilability rules)</p>
    </blockquote>
    
    <p>C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\@network\network.m</p>
    
    <blockquote>
      <p>called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\feedforwardnet.m
      (because of toolbox compilability rules)</p>
    </blockquote>
  </blockquote>