Showing results for 
Search instead for 
Did you mean: 

Same code gives different results in Matlab Script in Labview and Matlab

I am implemeting a Matlab code into a LabVIEW application using Matlab script. When I import the exactly same code to the Matlab Script in LabVIEW it gives a different result than it is in Matlab. This code is a simulation code including first kind bessel functions. Using LabVIEW 7.1 and Matlab R14 service pack 3.
0 Kudos
Message 1 of 22
Is it only slightly different or complely wrong?
Error handling in a matlab script node is not very good. Have you at least hooked up the error output of the matlab script frame? Does it tell you anything?

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 2 of 22
Hi iort,

Along with Altenbach's statement, can you provide me a simplified version of the .m script code you used that will reproduce this error? If we know what is expected and what is actually outputting we can determine internally what the problem is and how to fix it.

Also, have you tried the MathScript node in place of the Matlab script node?


| Michael K | Project Manager | LabVIEW R&D | National Instruments |

Message 3 of 22
Is the script you're running using any global variables that have been initialized in your workspace? When you use the Matlab Script Node, it will create a new, hidden instance of Matlab (TM) that won't share any of those workspace variables (or any other data). That could account for the differences you are seeing. If this is the case, then you should prepend to your Matlab Script Node contents an initialization script that gives appropriate values to these globals before starting your simulation code.
Jarrod S.
National Instruments
Message 4 of 22
Thanks for your interest to my problem.

There are some functions that I am using as seperate .m files and also .dat matrices. Since, definition of functions are not allowed in the script. I copied all files to the matlab\work folder. The code is working after the files were copied but giving a different result. Result is not meaningless but like some part of a function is dealt differently or missing, which changes all result. Is there another way to define functions in or to do matlab script. These functions are as the following:

function y=difsphbesselj(n,x)

0 Kudos
Message 5 of 22

I have similar problem. Same code, exactly the same code gives different results in matlab script of labview and on matlab alone. Also sometimes the same script works and sometimes gives error. Is there any way to make results more reliable?

0 Kudos
Message 6 of 22

... you just added to a 5 year old thread. When you say "same code", do you mean the same code as in the 5 year old thread above or some completely unrelated code which is the same in matlab and matlab script.


What is your LabVIEW version? What is your matlab version? How often is "sometimes"?


Can you attach an example that shows the problem? DId you try some of the suggestions mentioned above?

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 7 of 22

Labview 8.5

Matlab R2009b

Attached are the graphs produced by matlab script in labview and in matlab.

The minimum of graph produced by matlab code is below 1 and that in labview is above 1.

Thanks a lot for your reply.

Sorry, I haven't quantified the "sometimes" yet. 



Download All
0 Kudos
Message 8 of 22

It looks to me like you have the same picture, except that the results seem to be mirrored about the line Y=X.  (the 45deg line).


You should check your code again and make sure it truly matches and you didn't flip your indices.

0 Kudos
Message 9 of 22

I agree, the 2D array seems transposed, while the axis assignment (offset,increment) are not transposed. You did not show any code, but that graph is formed most lkely ouside the matlab node. We need to see some code.

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 10 of 22