C# MWArray is empty

Hi, I have written a simple MATLAB program (see attached). I wish to distribute this program as a Dot Net DLL and have so far succeeded in creating the DLL and importing it in a C# console application (see attached). The problem is that when the MATLAB function is called in Dot Net, it does not return any output. MWArray is empty! However, when I run the program in MATLAB I am able to see an output. I am not very good at C#. Can anyone help me out here? I will be much obliged.
Thanks
% MATLAB Code
% line only algorithm
function vse1()
line_data=[1 2 0.2;
1 3 0.4;
2 3 0.25];
meas_data=[1 2 0.6;
1 3 0.05;
3 2 0.4];
sigma=0.01;
eps = 0.01;
iter=0;
maxiter=3;
nb=max(max(line_data(:,1)),max(line_data(:,2)));
for i=1:nb
v_mag(i,1)=i;
v_mag(i,2)=1.0;
delta(i,1)=i;
delta(i,2)=0.0;
r(i,i)=sigma^2;
end
while iter < maxiter
% ref. bus 3
% formation of H matrix
% derivatives of Pij with respect to deltas
for i=1:size(meas_data,1)
for j=1:nb-1
from_bus=meas_data(i,1);
to_bus=meas_data(i,2);
k1=v_mag(from_bus,2);
k2=v_mag(to_bus,2);
k3=abs(1/complex(0,line_data(i,3)));
k4=delta(from_bus,2);
k5=delta(to_bus,2);
k6=angle(1/complex(0,line_data(i,3)));
if from_bus == j
h(i,j)=-k1*k2*k3*sin(k6+k5-k4);
elseif to_bus == j
h(i,j)=k1*k2*k3*sin(k6+k5-k4);
else
h(i,j)=0;
end
end
end
% Formation of measurement function f(x)
for i=1:size(meas_data,1)
from_bus=meas_data(i,1);
to_bus=meas_data(i,2);
k1=v_mag(from_bus,2);
k2=v_mag(to_bus,2);
k3=abs(1/complex(0,line_data(i,3)));
k4=delta(from_bus,2);
k5=delta(to_bus,2);
k6=angle(1/complex(0,line_data(i,3)));
f(i,1)=-k1*k2*k3*cos(k6+k5-k4);
end
% formation of Z - f(x)
zfx = meas_data(:,3)-f;
check=max(zfx);
if check <= eps
iter;
delta(:,2)
break;
end
delx=inv(h'*inv(r)*h)*(h'*inv(r)*zfx);
for i=1:nb-1
delta(i,2)=delta(i,2)+delx(i,1);
end
delta(:,2)
iter=iter+1
%xx=input('enter')
it=iter;
end
J=0;
for i=1:size(meas_data,1)
J=J+zfx(i,1)/r(i,i);
%J
end
end
--------------------------------------------------------------- C# Code --------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using MatlabDemo; using MathWorks.MATLAB.NET.Arrays; using MathWorks.MATLAB.NET.Utility;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MLTestClass obj = null;
MWNumericArray output = null;
MWArray[] result = null;
MWArray[] result1 = null;
try
{
obj = new MLTestClass();
result1 = obj.vse1(0);
int test = result1.Length;
output = (MWNumericArray)result[0];
}
catch (Exception)
{
throw;
}
}
}
}

Answers (1)

Your function vse1 doesn't have any output arguments declared:
function vse1()
If you have an output argument, that line should look like:
function outputArg = vse1()
I don't know how you see an output even in MATLAB. What is the name of your output variable?

2 Comments

Sid Gordon
Sid Gordon on 29 Nov 2011
Hi Kaustubha, thanks for your reply. Basically, I want to output the "iter" and "delta" variables in my MATLAB program. If you look closely at the program, I have written both variables without a semicolon. This allows me to run the program directly in MATLAB without having to declare the output variables. The program iterates twice and writes the iteration number (i.e. iter) and delta value to the MATLAB command line.
After reading your post, I have now declared the function as:
function [delta, iter]= vse1()
My C# code remains as posted earlier. Now, when the function is called in this line:
result1 = obj.vse1(0);
I can see the output in my Windows Console. Please see: http://tinypic.com/r/10zbbqe/5
However, MWArray is still empty. I am not sure what I am doing wrong. Will be grateful if you can guide me in the right direction.
Thanks
Sid.
I can see the
I think you need to pass the output variable also as an input argument. See http://www.mathworks.com/help/toolbox/dotnetbuilder/ug/f5670.html
You probably need to call it like this:
obj.vse1(1, result1);

Sign in to comment.

Asked:

on 29 Nov 2011

Community Treasure Hunt

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

Start Hunting!