Animating 2D Plots Using Labtalk and a Series of Coefficients


One of Origin’s more advanced features is its ability to generate animated plot videos using little more than some basic functions, a series of coefficients, a script that iterates through them, and a video codec output. In this post we’ll be learning how to use these features in tandem to illustrate the mathematical behavior of trigonometric functions. If you want to view the graph for this blog in Origin, you can download the accompanying OPJ file here.

Before we can automate our graph, we need to generate some data to plot it. In a new workbook, let’s create some angular values in radians. This can be accomplished in two ways, by entering in the f(x) box either “data(0,pi,0.007)” or alternately “radians(data(0,180,0.4))”. The latter will convert any value given in degrees to units of radians. Because we want the lines on our graph to be smooth, our intervals has been set to 0.007 in the radians data and 0.4 for the converted angles data. These incremental values can be changed however, as they are entirely arbitrary.

Now let’s create 5 additional columns either by right clicking the space next to our existing columns and selecting “Add New Columns”, or, by using the hotkey CTRL+D to prompt a dialog that asks how many columns we’d like to add. In our last column, col(G), enter the following- data(0.02,8,0.02). Make the “Long Name” for that column, “Coefficient (n)”, this name will be displayed later on our graph later using our animation script. Then in col(F) give the first cell a value of 0.02.  No coding is required here as this figure is a placeholder that will be changed as our animation is rendered. Next we’ll define some trigonometric functions using this data. First in col(B)’s f(x) cell enter, “sin(col(A)*col(F)[1])”, and then for columns C, D, and E respectively, enter “sin(-col(A)*col(F)[1])”, “cos(-col(A)*col(F)[1])”, and “cos(col(A)*col(F)[1])*-1”.
002Then let’s select our Y-columns B, C, D, and E, and go to Plot>Specialized>Polar Theta(x) r(y). By default the Polar Graph will display in units of Degrees. To change this, double click on one of your radial or angular axes to open the Axis Dialog. Choose Angular Axes-> Scale node on the left panel. Expand Units Definition and set our Units to Radians, keeping the default range 0, to 2 pi. Our graph should now look something like this-
You may want to alter the color/width of your lines in Plot Details and delete the legend as well, but otherwise the scale does not need to be adjusted; our graph looks relatively empty because we gave our coefficient such a low starting value.
In the graph window, create a text-box in one of the upper right hand corner of our plot and enter “sin(n•x)” or “cos(n•x)“, the basic expressions by which our data will be modified. Then create a second text-box and type in “Coefficient placeholder”. This text-box will be used as a display for our modified coefficient as Origin runs through the data. Each object has a unique name so that it can be called in our Script. Right click the text object and choose Programming Control. Then in the object name field, rename Text1 as e.g. “updco“. We’ll be calling on this object in our script later to update our coefficient n.

After setting up our graph, we’re now ready to run our script-

The //comments in the above text-box explain the functions of each section of this code.
There are two ways we can run this code, we can copy/paste it into the script window, select all, and press enter. You’ll be prompted with a “Save as…” window to name and save your AVI file. After doing this, the script will begin running through the iterations and after some period of time (depending on the number of windows you have open, and your computing power), you’ll have an animated AVI file of your graph that should look something like the .gif at the beginning of the blog.

If you wish to stop your video from rendering, simply click on an active section of the graph as it changes (nothing needs to be highlighted), and press ESC several times until the graph stops updating. If you stop Origin any time before the graph finishes rendering, to open the video file without closing the program, you’ll need to first highlight or enter this portion of the script “vw.Release();”. This will release the AVI file generated by the software.


Adding a Basic User Interface

That’s how we can generate a video with our graph updating, but let’s say we want to view this without creating an AVI file. Moreover, if we want to share this file with others, it may make more sense to design an interface rather than have them copy and paste our script into the script window.

To do this, let’s begin by making some buttons. First, let’s go to the draw tool on the lefthand side of our workspace and make three objects in one of the corners of our graph window.

For our “Play” button, we’ll select the polygon tool and draw a right-pointing triangle. To finish the triangle, double click on the last drawn point.
We’ll draw a square for our “Stop” button and a circle for our “Record” button, and then change their colors and line formatting to make them more unique.

With our controls drawn and placed on our graph, let’s right-click on our play button and select Programming Control. In the Programming Control dialog, change the Script drop-down menu to “Button Up”, and then paste the following script into the dialog below.

Then click OK to close the dialog. For the moment, DO NOT CLICK this button. With this script in the place, every time we click it, Origin will cycle through our script without saving it to a video file. To stop this script, we’ll need to program the next object. Right click on the square and go to its Programming Controls. Again set the script to run after “Button Up”, and in the script window type-

This single line of code will stop the previous script and any others running in Origin when the “stop” button is clicked. Unless you press this or let the previous script loop up to 8, Origin will keep running through different values of our coefficient until you close the program.

Lastly, we’ll open the Programming Control for the “record” button. After setting it to activate after “button up” we’ll copy and paste our initial script into the window, effectively allowing us to generate our AVI file with the click of a button.

One Comment on “Animating 2D Plots Using Labtalk and a Series of Coefficients”

Leave a Reply

Your email address will not be published. Required fields are marked *