Appending Variables to File Names in DIAdem

Updated Oct 25, 2022

Environment

Software

  • DIAdem

This article demonstrates how to programmatically append a variable to a data file using a Visual Basic Script (VB Script). This can be useful for scenarios where many data files need to be differentiated from each other dynamically.

This article utilises the DataFileSave function to add a variable to the end of a file name. For demonstration purposes, the example prompts the user to specify a frequency for a Sine Wave. The frequency is stored as a variable and appended to the end of a .TDM file name.

1. Navigate to the SCRIPT Panel and select File >> New VBS.
  • Save the VB Script as "Adding Variables to File Name.VBS".
2. Define the variables that will be used in the VB Script.
  • This example utilises two variables: sPathDocuments and selectedFrequency.
  • sPathDocuments will store the path to the current script file, and selectedFrequency is the variable that will be appended to the TDM file name.
  • To define these variables, enter the following script:
Call GlobalDim("sPathDocuments")
Call GlobalDim("selectedFrequency")
sPathDocuments = CurrentScriptPath
selectedFrequency = 1


2. Optional: Remove any data currently inside the Data Portal with the following command.

Call Data.Root.Clear()

3. Generate a User Dialog Box that allows users to specify the value of a variable. For this example, the user will define the value for selectedFrequency.
  • From the top toolbar, select Edit >> Create Dialog Box.
  • In the DIAdem Dialog Editor window, drag and drop objects from the Control bar to the Dialog Box. This example utilises a Numerical EditBox and OK, Cancel Control, as shown.


Dialog Box Layout.PNG
 
  • Click on the Dialog Box to select it. From the Properties Window, select the three ellipses next to the Variables property.
  • In the DIAdem Variables window, select New Entry...
  • Specify the name of your variable as the Name and Variant as the Type. Click OK to save the configuration. Note: In this example, the variable name is selectedFrequency. This property is case sensitive, so ensure that it matches your VB Script variable exactly.
  • The new variable should appear as follows:


Dialog Variables.PNG
 
  • Click on the Numerical EditBox to select it.
  • From the Properties window, click the drop-down arrow next to the Variables property and choose the newly created variable. This maps the VB Script variable to the Dialog Box, allowing users to manipulate the value of VB Script variables.

Numerica; EditBox Variable.PNG
 
  • Save the Dialog Box as "FrequencyDialog.SUD" in the same directory as your VB Script.

4. Call the Dialog Box in your VB Script using SUDDlgShow, as demonstrated below. Here, "FrequencyDialog.sud" should match the name of your SUD file.

Call SUDDlgShow("Dlg1", "FrequencyDialog.sud")

5. Generate a signal with the selected Dialog Box frequency.
  • Use the ChnGenSignal command to generate a signal. This example generates a Sine Wave and takes selectedFrequency as the fifth parameter.
  • Use the Val() function to convert the variable to a numeric data type.
Set ChnResult = ChnGenSignal("Sine", 1024, 100, "/GeneratedSignalChannel", Val(selectedFrequency), 30, 0, 0, "s", "V", 50)

6. Save the generated data as a TDM file and append the variable to the file name.
  • Use the DataFileSave to save the TDM file.
  • The first parameter is the full path of the file. Specify the path as sPathDocuments & "GenerateSine_" & str(selectedFrequency) & "Hz.TDM". This will save the TDM file in the same location as the VB Script with the frequency appended to the end of the name.
  • Note: It is important to use the str() function to convert the variable to a string data type first.
Call DataFileSave(sPathDocuments &"GenerateSine_" & str(selectedFrequency) & "Hz.TDM", "TDM", True)
 

The completed VB Script should resemble the following:

'-------------------------------------------------------------------------------
'-- VBS script file
'-- Created on 10/25/2022 08:34:19
'-- Author: NI
'-------------------------------------------------------------------------------
Option Explicit  'Forces the explicit declaration of all the variables in a script.

'define variables
Call GlobalDim("sPathDocuments")
Call GlobalDim("selectedFrequency")
sPathDocuments = CurrentScriptPath
selectedFrequency = 1

'Clear the data portal so that we start with no data
Call Data.Root.Clear()

'Display a user dialog that allows you to specify a frequency for a Sine Wave. Generate the Sine Wave
Call SUDDlgShow("Dlg1", "FrequencyDialog.sud")
Set ChnResult = ChnGenSignal("Sine", 1024, 100, "/GeneratedSignalChannel", Val(selectedFrequency), 30, 0, 0, "s", "V", 50)

'Save the data as a TDM file with the selected frequency in the file name
Call DataFileSave(sPathDocuments &"GenerateSine_" & str(selectedFrequency) & "Hz.TDM", "TDM", True)