Animating 2D Plots Using Labtalk and a Series of Coefficients

FinishedOPJ

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-
001
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-

int codec = vw.FourCC(1, 0, 0, 0); // First we trigger the Microsoft Run Length Encoded



// Triggers a dialog to set the name of your video. Best to save within User Files directory for permissions reasons
dlgSave fname:=myfile ext:=*.avi title:="Save your video file";
int err = vw.Create(%(myfile$), codec, 30, 800, 600);
// Prompts "Save as" window, creates video writer (fileName, codec, framesPerSec, width, height)
if( 0 == err ) //If no errors, then...
{
 range rZ = [Book1]Sheet1!col(G); //Active dataset var
 range ee = [Book1]Sheet1!col(F)[1]; //Modifier, updated as Origin goes through the above data

 range -wx rngXCol = %C; //Active x-range var
 loop(ii,0.02,rZ.GetSize()) //Loop var ii, from 1st value to the size of the active dataset 
 {
 //set display by angle
 ee = rZ[ii];


 // Update the Angle display
 double dd = rZ[ii]; //integer cycles through x with loop var ii
 GObject goText = 1!updco; //defines object "gotext" in layer 1 as object xtext
 goText.text$ = %(rZ[L]$) = $(dd,.3*); //disp Lname equal to xval to 3 decimals if necessary


 //set %C ee;
 // Wait for graph window to redraw.
 // This allows watching the animation while creating the video file.
 sec -p au;
 sec -pw %h;
 run.section(STANDARD,Refresh);
 // Write graph to video as a single frame.
 err = vw.WriteGraph(%H);
 if( err )
 break;
 }
 
 // Releases the AVI file from the video writer.
 vw.Release();
 
 if( err )
 type "WriteGraph error $(err)";
}
else
 type "VideoWriter Create error $(err).";

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.


Note:
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.
003

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.
004
006

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.
 

range rZ = [Book1]Sheet1!col(G); //Active dataset var
 range ee = [Book1]Sheet1!col(F)[1];

 range -wx rngXCol = %C; 
 loop(ii,0.02,rZ.GetSize()) 
 {
 // Update the Angle display
 double dd = rZ[ii]; //integer cycles through x with loop var ii
 GObject goText = 1!updco; //defines object "gotext" in layer 1 as object xtext
 goText.text$ = %(rZ[L]$) = $(dd,.3*); //disp Lname equal to xval to 3 decimals if necessary



 ee = rZ[ii];
 sec -p au;
 sec -pw %h;
 run.section(STANDARD,Refresh);

}
range ee = 0.02

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-

break 1;

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”

  1. I appreciated what we did here. I enjoyed every little bit some of it. I am always trying to find informative information this way. Thanks for sharing around.

onesetvision にコメントする コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です