OriginLab Corporation - Scientific Graphing and Data Analysis Software - 2D graphs, 3D graphs, Contour Plots, Statistical Charts, Data Exploration, Statistics, Curve Fitting, Signal Processing, and Peak Analysis

Worksheet Query

The Worksheet Query menu command directs Origin to locate data in the active worksheet based on a user-specified conditional expression. The located data values are extracted. In other words, these values are copied and pasted to another worksheet, or, the cells that have these values are filled with a specified color or selected.

There are two main panels in this dialog: the left panel and the right panel.

Image:Extract_Worksheet_Data_1.png

Contents

Simple Tutorial

  1. Use the Import Wizard to import the file \Samples\data manipulation\us metropolitan area population.dat. The destination worksheet contains four columns: Population, Sq.Mi., Density, and Metropolitan Area.
  2. Select Worksheet: Worksheet Query from the Origin menu to open Worksheet Query dialog box.
  3. With the ctrl key down, select Density and Metropolitan Area in the left panel. Click the => button to make these columns available for constructing the condition. Note that aliases D and M are assigned to the columns respectively.
  4. Clear the check box before Sq.Mi. in the left panel so that this column will not be included in the extracted result.
  5. Enter the following condition formula in the Condition box. Note: You can click the AND button instead of typing out A-N-D):
    D>100 AND M[i]$ == "* CA*"
    This condition looks for densities above 100 for all areas in California ("CA"). Note that this tool supports wildcard symbols such as * inside strings.
  6. Click the Test -- select if true button. Notice that the number of found rows under the Condition box has been updated, and some rows in the worksheet are highlighted.
  7. Select the Extract to New Worksheet radio button for Output, and then click the OK button. You see that the extracted results are stored in a new worksheet in the source workbook.

Opening and Running the Worksheet Query dialog box

To query data from a worksheet, you can use the Worksheet Query tool. To bring up this tool, you can:

  1. Activate the worksheet.
  2. From the menu, choose Worksheet: Worksheet Query.

After entering the condition in the Condition box, click the OK or Apply button to perform the query.

Left Panel

The left panel lists all the columns in the active worksheet, with some basic information about them. You can check before the columns to select those that will be extracted. To see more information about the column, right-click on the panel area and select the quantities you want to see in the left panel. Menu commands available in the short-cut menu include the following:

Extract

Use this menu command to specify whether or not to show the Extract checkbox column. With these checkboxes, you can specify the columns that will be extracted.

Type

Use this menu command to specify whether or not to show the column type.

Format

Use this menu command to specify whether or not to show the column format.

1st Value

Use this menu command to specify whether or not to show the first column value.

Check all Extract

Use this menu command to check all the checkboxes in the Extract column.

Uncheck all Extract

Use this menu command to clear all the checkboxes in the Extract column.

Right Panel

The right panel includes the condition edit box, output controls, and other controls.

Select Column Variables for If Test

Before querying data, you should select columns into this list, and these columns will be used to build the condition. To move columns to the list, select them on the left panel and then click theImage:Extract_Worksheet_Data_2.png button. Only when there are columns in this list is the Condition edit box editable.

There are two items on this table:

Alias

This allows you to define an alias for each column. Then you can use this alias to build the condition. To edit the alias, double-click on the cell, enter the new alias and click outside.

Column

This is for displaying the column names.

Condition

Type a conditional expression into the text box used to extract data. You can use any of the logical and relational operators listed below. Additionally, you can use any arithmetic operators (+, -, *, /, ^).

Operators Meanings

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

== (or =)

Equal to

!= (NOT)

Not equal to

&& (AND)

And

| | (OR)

Or

Besides typing the conditional expression, you can also use the buttons beside the Condition edit box to build the condition.

When building conditions, you can use both the worksheet alias and Col() notation to specify a column in the expression. To indicate a row number, use the variable i. To continue an expression on the next line, press enter.

For example, to extract rows with values in column A that are greater than 0, you can type:

Col(A)>0

If you have specified an alias, for example, "Temperature" for column A, you can also use:

 Temperature > 0

To query worksheet data using text criteria, use the following syntax:

Col(ColumnName)[i]$ == "text";

For example:

Col(Station)[i]$ == "Northwest";

Note: Origin uses a filter that is not case-sensitive, when it extracts data using text criteria.

Test -- Select if True

Click this button to test the condition. If any record is found, the total number of result rows will be shown in the Worksheet Query dialog and the corresponding rows will be highlighted in the source worksheet.

Row(i): From - To -

Specify the range of rows to be extracted.

Output

Add a column indicating True (1) or False (0)

Add a new column to the source worksheet to indicate whether the condition is satisfied: 1 for "Yes" and 0 for "No".

Extract to a New Worksheet

Extract the data to a new worksheet in the source workbook.

Extract to a New Workbook

Extract the data to a new workbook.

Extract to Specified Sheet

Extract the data to a specified worksheet. Once you have selected this option, you should specify the worksheet name in the Worksheet Name edit box and Column from edit box (its default is set to 1).

Fill with Specified Color

Fill all cells found by the query with a specified color.

Select

Select all cells found by the query.

The Dialog Menus

File

Load Sample

Load the built-in examples.

Load

Load a saved condition.

Save

Save the current condition.

Save As...

Save the condition with a new name.

Scripts

Set LabTalk scripts that will be run before the data query. This will help to prepare proper data for the condition. You can set a Script Before Select Data Loop and a Script Before If Condition.

Origin actually runs the data query routine as follows:

  1. Run the Script Before Select Data Loop.
  2. Loop over all rows defined by the start and end values provided by user. For each column whose row index is i:
    1. Run the Script Before If Condition ;
    2. Test the condition, for data in row i;
    3. Extract data if the condition is true;
    4. End query;
  3. End loop;

Suppose there are two columns. Col(a) has some data. Col(b) contains three numbers which will be used as row indices of cells which will be extracted from Col(a).

Image:Book.png

We can set the Script Before Select Data Loop as follows:

j=1;

Then set the Script Before If Condition as:

if( i == col(b)[j] )
{
	temp = col(a)[i];
	j++;
}

Extract data in the worksheet with the following condition:

A == temp

After clicking the Test button, Origin will highlight row 1, 4, and 7.

Note: To use the scripts, the minimum version of Origin required is Origin 8.0 SR3.

F(x)

You can insert functions from a number of categorized lists, to build your query condition. Recently used functions are accessible from the Favorite submenu. This F(x) menu works the same to the one in the Set Values dialog.

These functions include: LabTalk functions and built-in Origin C functions.

To add a user-defined function in the F(x) selection, please refer to the How to Create a User-Defined Function for Set Column Values.

For more information of these functions, please refer to Script Accessible Functions in Appendix 3.