5.6.1.2 Entering Expressions in the Set Values Dialog


Expression Basics

The expression used in the Set Values dialog should follow LabTalk syntax. The expression can contain arithmetic operators, range variables, built-in LabTalk functions and user-defined functions etc. There are two places to define expressions:

  • Formula. Only a single-line expression can be input here. Note how this expression is the right-hand part of a complete statement:

Cell(i,j) = c1*sin(i) + c2*cos(j).

  • Before Formula Scripts. Multi-line LabTalk scripts can be input here, which will be executed before matrix values are set with the formula in the Formula edit box.
SetMatrixValuesPanel.png
  1. Origin offers lots of built-in functions under the Function menu, which can be directly used in the expression. You can also create a User-Defined Function yourself.
  2. The letter i and j are system variables that store the current row index and column index while the letter x and y represent the current X and Y values. You can use them in the Formula edit box.

Add Expressions only in Formula

You can add a single-line expression in the Formula edit box and leave the Before Formula Scripts panel empty. The expression in the Formula edit box can include built-in functions and user-defined functions. The following is an example that shows how functions can be used.

Example 1:

  1. Create a new matrix. Then click the AddMatrixMenu.png icon on the upper-right corner of the matrix and select Add to add a new matrix object.
  2. Select Matrix: Set Values to open the Set Values dialog. Then input i in the Formula edit box and click the Apply button to generate data for it.
  3. Click the next SetValuesNext.png button to set values for the second matrix object. Select Function: Math: Cos(x) from the menu of the dialog to add cos() to the Formula edit box. The cursor is positioned within the parentheses. Select Mat(1): Mat(1) from the menu. Then the expression will be cos(Mat(1)). Click the OK button to execute the formula.

Add Expressions only in Before Formula Scripts

You can input multi-line LabTalk scripts in the Before Formula Scripts edit box and leave the Formula edit box empty. In addition to built-in functions or user-defined functions, any LabTalk scripts are supported here. So you can use range variables, string variables, loops and LabTalk accessible X-Functions.

The following example will get the same results as Example 1.

Example 2:

  1. Create a new matrix. Then click the AddMatrixMenu.png icon on the upper-right corner of the matrix and select Add from the short-cut menu to add a new matrix object.
  2. Select Matrix: Set Values to open the Set Values dialog. Then input i in the Formula edit box and click the Apply button to generate data for it.
  3. Click the next SetValuesNext.png button to set values for the second matrix object. Enter Mat(2)=cos(Mat(1)) in Before Formula Scripts and click the OK button to generate the data.

Add Expressions in both Formula and Before Formula Scripts

Often, we need to enter scripts in both the Formula edit box and the Before Formula Scripts panel. For example, you can define range variables in Before Formula Scripts and then use them in Formula.

Example 3:

  1. Create a new project.
  2. Create a new matrix which should be named as MBook1. Then click the AddMatrixMenu.png icon on the upper-right corner of the matrix and select Add from the short-cut menu to add a new matrix object. We will fill some numbers in them and use the numbers later.
  3. Highlight MBook1. Right-click and select Set Matrix Values from the short-cut menu. Then input i in the Formula edit box. Click the Apply button to generate data for it.
  4. Click the next SetValuesNext.png button to set values for the second matrix object. Input j in the Formula edit box. Click the OK button to generate data for it.
  5. Create another matrix. It should be named MBook2.
  6. Right-click in the empty, gray box in the upper left of MBook2 and select Set Matrix Values from the short-cut menu to open the Set Values dialog. We will define two range variables which refer to the two matrix objects of MBook1 in Before Formula Scripts and use them in the Formula edit box as shown in the following screenshot:
    SetMatrixRangeVariables.png
  7. Click the OK button to generate data for MBook2. The results should look like:
    SetMatrixValuesResults.png