3.5.12.3 Exist


The exist(name[,n]) function has two primary applications:

  • Checking for the existence of an object.
  • Checking the state of an object.

When option n is not specified: You can test for the existence of (a) an object, (b) a range variable, (c) a file or (d) a LabTalk tree.

When n is specified: You can test for (a) graphic objects, (b) string variables, (c) Origin C functions, (d) Origin project folders, or (e) window activity (active, hidden).

Note 1: A matrix is also a dataset. If you delete a matrix window without deleting the data, the exist(name) function will return 5 before deleting the matrix window, and 1 after deleting the matrix window.
Note 2: Starting with Origin 8.0 SR2, you can pass a string containing the name of an object instance (e.g., a worksheet, a variable, etc.) into the exist function, and Origin tests for the existence of that name in the project.


Testing for General Object Instances

Usage: exist(Object name) returns Value if named object exists or 0 if it does not.

Value Object type
0 Object does not exist.
1 dataset
2 workbook/worksheet
3 graph window
4 numeric variable
5 matrixbook/matrixsheet
7 tool
8 macro
9 notes window
11 layout window
12 Excel worksheet
str$="Book1";
exist(str$)=;  // Will return 2
// you don't have to do this, even though it will lead to the same result
exist(%(str$))=;
str$="Book2"; // Book2 is a Excel workbook
exist(str$,12)=; // will return 12

Graphic Objects

Usage: exist(name,16) Looks for the named graphic object, returns 16 if it exists in the active layer or sheet, or 0 if it does not (9.0 SR0).

String Variables

Usage: exist(name, 18) Looks for the named string variable and returns 18 if it exists, or 0 if it does not (8.0 SR0).

The exist function will look at the content of the string, and not the string name itself, unless it is told to do otherwise. Therefore, in order to test if a string variable exists, you must use the value 18:

if(exist(str$,18))
  type "Yes, str$="+str$;
else
  type "No, there is no string variable named str$";

Range Variables

In Origin 8.0 SR5 and before, a range variable would create a new instance of an object upon assignment, such that exist(range) would never return 0.

In 8.0 SR6 this was changed, so that exist(range) will return 0 until the object is assigned a value. For example:

range aa=testCol; 
exist(aa)=;  // ANS: EXIST(AA)=0

aa[1]=1;     // New column 'testCol' is created in the active worksheet
exist(aa)=;  // ANS: EXIST(AA)=1


Testing for Origin C Functions

You can also use the exist(name) function to determine whether or not an Origin C function exists:

Function Purpose Return value if function exists Return value if function does NOT exist
exist(name)

or

exist(name,20)

test any Origin C function callable from LabTalk 20 0
exist(name,21) test for an Origin C string function 21 0
exist(name,22) test for an Origin C numeric function 22 0
exist(name,23) test for an Origin C vector function 23 0

For example,

exist(char,21)=; // Returns 21
exist(Degrees)=; // Returns 20

File Testing with exist(fileName) or exist(filename, n)

When the string passed into exist function is in the form of a full path file name, then the exist(filename) function will return the file size in kbytes, including to return 0 if file is empty. If the specified file does not exist, -1 is returned.

Use the exist(filename, n) function to check the version number and date of a file. If n = 1, 2, 3, or 4, this function returns the first, second, third, or fourth byte from the version resource. For example:

str$=system.path.program$+"oUtl60.dll";
for(ii=1;ii<=4;ii++) exist(str$,ii)=;

To return the date (in Julian Days) of the file, set n = 5. For example:

str$=system.path.program$+"oUtl60.dll";
if(exist(str$) > 0)
{
  double dd = exist(str$,5);
  type "The file "+str$+" was last modified on $(dd, D9)";
}

exist(winName, n)

The exist(winName, n) function provides information about window activity.

If n = 0, this function returns a non-zero value if the specified window is active and is not hidden. Otherwise, it returns 0.

If n = 10, this function returns a non-zero value if the specified window is active. Otherwise, it returns 0.

For example,

exist(Graph1,10)=; //will return 3 if Graph1 window is active.

Testing for LabTalk tree

You can use the exist(tree name) function to check whether or not the specified tree exists.

tree tt; //define a LabTalk tree
exist(tt)=; //will return 24 if this tree exists; Otherwise, return 0.

Testing for Folders

You can also use the exist(name) function to determine whether or not a Folder exists in a project. (Origin 8.1SR2):

Function Purpose Return value if folder exists Return value if folder does NOT exist
exist(Folder Name [, 25])

or

exist(../Folder Name [, 25])

test for existence of Folder Name in current folder or in specified path. For example

if(exist(Run Data,25) == 0) pe_mkdir "Run Data";

25 0