DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Communicating with MATLAB

I started to learn DIAdem recently. I would like if somehow DIAdem would be able to talk to MATLAB and vice versa. When the sales rep introduced this to me, he was pretty confident about it. Also, in the DIAdem -Example Finder--> Examples-->Creating Scripts-->Using Interfaces--> Communicating with MATLAB, I found this example. But, when I run the example i do not get the desired result. I do not get any errors as well.

 

I am expecting 2 FFT graphs, from MATLAB and DIADEM. But, I get nothing. The MATLAB command window (not whole MATLAB) opens for some time and then closes. I am also attaching the screenshot of DIAdem after the VB Script is executed.  

 

Can someone help me work this out? 

 

Thanks in advance

0 Kudos
Message 1 of 17
(5,297 Views)

Hi Nueropotential,

 

It looks like the Time channel and all 3 Sine_Wave channels are correctly created in the ML_Create_SineWave() Sub, so you must be at least getting to line 32 without any errors.  Try this, delete or comment out line 17 (On Error Resume Next), that will stop suppressing error messages and may give us a clue as to what's going wrong.  Either MatLab can't find the m script file, can't execute it, or DIAdem is failing to request the values of the resulting "Freq" and "FFTRes" variables from MatLab.  To start with we need to know which of these items is causing the current error.

 

By the way, what operating system are you using?

 

Brad Turpin

DIAdem Product Support Engineer

Nationa Instruments

Message 2 of 17
(5,271 Views)

Hey Brad,

 

Thank you for your reply. I followed your instructions and commented out line 17. Now, I get the following error in DIADEM

 

Error in <MatLabTest1.vbs> (Line:39,Column:3):

 

Exception Occured

 

The Line 39 is as follows

 

Call oMATLAB.GetWorkSpaceData("Freq","base",tmpdata)'Get the data back from MatLab 

 

 

Also, before trying the example in DIAdem, I had followed instruction on the NI webpage to communicate with MATLAB. Just wanted to inform you about it as well. 

 

http://digital.ni.com/public.nsf/allkb/1706BA4C65A8533686256C630062DD03

 

 

 

I am using Windows XP SP3 with MATLAB 7.6.0 (R2008a)

0 Kudos
Message 3 of 17
(5,268 Views)

Hi Neuropotential,

 

Thanks for that information.  Now we know that the error is occurring when trying to retrieve the variables from MatLab.  That's either because they weren't created by the m script or due to some other error in communicating the request.  Let's try manually loading the m script into your MatLab and running it by itself (no DIAdem involved):

 

"C:\Program Files\National Instruments\DIAdem 11.1\Examples\Documents\MatLab_FFTTest1.m"

 

Does it run without errors?  Does it create variables called "Freq" and "FFTRes" in MatLab?  If so, then we need to investigate why the ActiveX call to retrieve these variables failed.  If not, then we need to look into the m script and see why it failed to create those variables.

 

Let me know what you find,

Brad Turpin

DIAdem Product Support Engineer
National Instruments

Message 4 of 17
(5,255 Views)

When I ran the m script "C:\Program Files\National Instruments\DIAdem 11.1\Examples\Documents\MatLab_FFTTest1.m", I got an error saying there was no variable Sine_Wave. I looked at the m script and saw the 2 lines were commented out as follows. 

%w=[1:1:100];
%Sine_Wave=sin(w);

 

I took out the comments and run the script again and it ran perfectly. It created the two variables called "Freq" and "FFTRes". I thought this should have solved my problem. But when I ran my VB Script again, I had the same problem. I am attaching the m script as a text file if you need it.

 

I think there is some issue with communication. Is there any way to check if the ActiveX is working properly? Let me know what you think.   

0 Kudos
Message 5 of 17
(5,251 Views)

Hi Neuropotential,

 

Yeah, unfortunately I don't have access to MatLab and can't do anything with either the m script that ships with DIAdem or the one you just sent me.  Line 34 of the DIAdem VBScript attempts to write to a variable in MatLab called "Sine_Wave".  So that's why those two lines are commented out-- they shouldn't be needed.  But if you uncommented them and saved the m script back to disk, then when you run the DIAdem VBScript those target variables should get filled.

 

Try adding a MsgBox line to the DIAdem VBScript below the current line 37:

 

Call oMATLAB.Execute("MatLab_FFTTest1")

MsgBox "Look at MatLab graph"

Call GroupDefaultSet(GroupIndexGet("MATLAB_Results"))

This will cause the VBScript to pause after it's told MatLab to run the m script and before DIAdem tries to request those 2 target variables.  The m script displays a MatLab graph, so if all is well in MatLab with those 2 target variables, the graph will show FFT results with 3 main peaks.  It would be helpful to know if the graph displays properly, and if it does, whether it requires you to uncomment those two lines you mentioned or whether MatLab can successfully receive the "Sine_Wave" variable DIAdem is sending it.

 

Brad Turpin

DIAdem Product Support Engineer
National Instruments

Message 6 of 17
(5,249 Views)

I added the MsgBox at the place you asked me to put. After that I run the VBScript twice. First, with the lines commented in m script and second, with the lines uncommented in the m script. Unfortunately in both cases I did not see a graph. But I saw the message in both cases.

 

Then i ran the m script from matlab without diadem. The script run and I also got the figure. But, the figure has only 1 peak and not 3. I have attached the figure. 

0 Kudos
Message 7 of 17
(5,247 Views)

Hi Neuropotential,

 

Yeah, the "Sine_Wave" that DIAdem tries to pass to MatLab is a superposition of 3 different sine waves, so if you run the FFT on that starting point you'll get 3 separate tone lines.  The MatLab code you uncommented is just a single sine wave tone.  So that explains that without helping us forward any.

 

Is there a way to add a message box in the m script?  We need to determine if DIAdem is successfully telling MatLab to run that m script and whether MatLab is successfully finding it and running it when DIAdem gives MatLab that ActiveX instruction in line 37.  The fact that you get the VBScript message box both times means that nothing causes an error until DIAdem requests those 2 target variables, but perhaps the commands to send the "Sine_Wave" variable and run the m script are failing to execute in MatLab without any error message in DIAdem.  I've seen this example work on computers that have MatLab.  I'm still not sure what the problem is.

 

Brad Turpin

DIAdem Product Support Engineer
National Instruments

Message 8 of 17
(5,245 Views)

I inserted a msgbox at the start of the m script. But, it worked similar to our previous attempts. If I uncomment the line 17, nothing happens. If I comment it, I get the following error which is exactly same as which we got previously

Error in <MatLabTest1.vbs> (Line:40,Column:3):

Exception Occured

 

The Line 40 is as follows

Call oMATLAB.GetWorkSpaceData("Freq","base",tmpdata)'Get the data back from MatLab  

Now, all this happens when I click on MatLab.vbs on the Example Finder Window to run this VB Script. This loads the VB Script in my Script Window of DIAdem and then I run it. 

But, instead if I click on "Start example" which is also on the Example Finder Window then I get an additional error which might be useful to you (I hope so!!)

 

52   3/2/2010 6:22:30 PM Error:
     Error in <MatLabTest1.vbs> (Line: 40, Column: 3):
     Exception occurred.
53   3/2/2010 6:22:30 PM Error:
     Error in <CHMCommand.VBC> (Line: 414, Column: 5):
     Error in <MatLabTest1.vbs> (Line: 40, Column: 3):
     Exception occurred.

 

Hope this helps. Let me know if I missed anything which you asked me to do. Feel free to shout at me f I do so 😛

 

 

 

 

 

 

0 Kudos
Message 9 of 17
(5,241 Views)

Hi Neuropotential,

 

It sounds to me like the problem is that the m script is never getting run.  Perhaps the "Addpath" command is not working properly.  Let's try providing the full path to the m script in line 37:

 

'Run MScript that has MatLab Perform FFT

 Call oMATLAB.Execute("Addpath '"&AutoActPath&"'")

Call oMATLAB.Execute(AutoActPath & "MatLab_FFTTest1.m")

 

How would you normally invoke an m script to run from the MatLab command prompt?  Would you just type the m script file path and hit return?

 

Brad Turpin

DIAdem Product Support Engineer
National Instruments

0 Kudos
Message 10 of 17
(5,211 Views)