LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a slider with MathScript in an event structure

Hello,

I'm exploring LabVIEW, and am now examining events and MathScript. I've attached a simple VI program, and it basically works OK, except for a significant quirk.  Basically, all it does is to plot a sine wave, with uslider controlled frequency, both in a LabVIEW graph, and also in a Matlab-like plot, using a MathScrip node.  To explore events, the MathScript node is executed in an event structure, triggered by a change in value of a frequency slider control.  The problem is essentially that the slider can't be operated continuously, and it can only change it's value by clicking on it, which means that it's value can't be precisely controlled.  For comparison, a Dummy slider is added, which does nothing, but which can be continuously operated for verification.  Maybe this plot should not be displayed like this, and neither should the frequency be adjusted this way, but that was an approach to explore.  Thanks for any thoughts or suggestions, or clarifications on what can or should or not be done.

0 Kudos
Message 1 of 4
(2,781 Views)

I'm assuming this is meant as a learning exercise, since it's ineffient to use MathScript just to generate a sine wave. The reason you're seeing what you are seeing is that the plot() command moves the focus to the plot window. Hence, if you have the mouse down, your VI's front panel no longer has focus, and thus moving the mouse (even if the mouse button is down) does nothing. You have to click back on the window in order to get focus back onto the LabVIEW window. Even if you programmatically move the VI's front panel to the top it's too late since the focus has been lost. If you did not plot the data in MathScript inside the loop then moving the slider would work as you expect it to.

 

There isn't any way to change this, as far as I know, since that's how the plot() command works.

0 Kudos
Message 2 of 4
(2,775 Views)

Thanks very much for this; makes sense.  Yes, this is a learning exercise.  I later tried to add a Key Focus property node for the slider, which I set true after the plot() command in a sequence structure, but that didn't improve anything.  I am very familiar with Matlab, and was happy to see that I could embed Matlab code in a Mathscript node, but I had no clue as to the ramifications concerning efficiency or whatever else.  Can you elaborate on that?  Is there a more efficient, or "proper" or preferable way to use a Mathscript node?  Apparently not in an event structure?  Or maybe it's just a matter of learning all about LabVIEW's graph device and all it's benefits, and use that for ploting signals.  Another related question: is code in a Mathscript node (or a formula node) compiled as part of the overall VI compilation before execution?  In that case, in what respect is Mathscript inefficient or better utilized?  Exploring and learning.  Thanks again very much.

0 Kudos
Message 3 of 4
(2,767 Views)

By "inefficient" I meant that it's far better to use the built-in functions for generating waveforms. There's a plethora of functions in the Signal Processing palette.

 

Placing the MathScript node in the event case isn't the issue. The issue with event structures is that normally you should not be placing code that may take a long time to execute in an event handler case. If the action to perform is going to take a while you should pass that off to another process to perform the action so that you can quickly return to the user interface. This makes the user interface more responsive. The producer-consumer architecture is a very good design pattern to handle this. Application Design Patterns: Producer/Consumer

 

One thing you need to be aware of is that MathScript is not Matlab. MathScript is NI's invention. It uses a language that is similar to Matlab, but it not 100% compatible. Some syntax is a little different and some functions don't have the same parameters. The LabVIEW documentation has details on the MathScript syntax and the functions.

 

An alternative to MathScript is the Scilab script node.

0 Kudos
Message 4 of 4
(2,762 Views)