Sunday, June 20, 2010

Matlab VB communication

i use Matlab for image processing abd VB for speech recognition
this code makes communication between VB and matlab
VB passes parameter's to Matlab and return's code back to calling application


Module Module1
Sub Main()
Dim MatLab As Object ' Create Object variable
Dim Result As String ' Create String variable
Dim lngLen As String

' Either way below will work.
MatLab = CreateObject("Matlab.Application")
MatLab.Execute("enableservice('automationserver',true)")

MatLab.Visible = False ' Hide MATLAB application

'Calling m-file from VB
Result = MatLab.Execute("cd F:\My Documents\MATLAB")
Result = MatLab.Execute("VBtesting(1)") 'passing value to VBtesting.m
lngLen = Len(Result)
MsgBox(Result)
MsgBox(lngLen)
Result = Mid$(Result, 14, 1) ' storing the result from Matlab
MsgBox(Result) ' display the returned value
MsgBox("VBtesting(" & Result & ")") 'passing the received value to VBtesting.m
Result = MatLab.Execute("VBtesting(" & Result & ")")
MsgBox(Result) ' display

End Sub

End Module


code for VBtesting1.m

function p=VBtesting1(a)
tic
load('abc.mat','ao');
toc
tic
if a==1
t = linspace(0,2*pi,16000);
y = sin(500*t);
y1 = sin(800*t);
else
t = linspace(0,2*pi,4000);
y = sin(200*t);
y1 = sin(400*t);
end
putdata(ao, [y' y']);
putdata(ao, [y1' y1']);
set(ao, 'TriggerType', 'Immediate');
toc
start(ao);
wait(ao,5);
p=ceil(rand(1,1)*10);


this code creates different audio signals depending on value's passed to function

thus we can have a bidirectional communication between both