Solved

ZPL: How to loop over a user defined array variables and store results in csv


Userlevel 1

I would like to loop over array variables and compute analysis results and store in a CSV file.

Example: I would like to loop over 4 field points and compute MTF and store the results in a CSV file. Where, Column 1 = Frequency, Column 2 = Field 1 results, Column 3 = Field 2 results and so on. How can I append results for the different fields points as defined in the ARRAY?

ARRAY= [1, 2, 3, 4]

FOREACH VARIABLE IN ARRAY

    FOR i = 0, 180, 10

            ! Define variables

            freq = i

            wave = 1

            field = VARIABLE

            sampling = 1

            vector = 1

            type = 1    

            GETMTF freq, wave, field, sampling, vector, type

            FORMAT 10.7

            OUTPUT log_filename$, append

           ! PRINT i, “,”, “vec1(0), “,”, vec1(1) # append results for the different fields as defined in the array

           OUTPUT screen       

    NEXT

Thanks

icon

Best answer by Jeff.Wilde 18 May 2022, 05:35

View original

4 replies

Userlevel 6
Badge +2

Hi Asuku,

I think your code is already saving the output correctly (you have it commented out right now), so you're asking about how to use Array Variables.  The ZPL doesn't have a good way populate an array in a single line like you’re showing.  Rather, you need to declare the array variable/length/dimensions and then populate each index in the array one at a time:

DECLARE fy, DOUBLE, 1, 4

fy(1) = 1

fy(2) = 2

fy(3) = 3

fy(4) = 4

For an array with only a handful of elements (limited by the length of a string, which is ~360 characters, so I would say less than 25 elements), you can use a space separated string and use $GETSTRING() to access each element.  From a simplified standpoint, you will need to know the number of elements in the string  The following code shows a simplified example looping over the values 1, 4, 7, 10:

array$ = “1 4 7 10”

FOR j = 1, 4, 1

val$ = $GETSTRING(array$, i)

val = SVAL(val$)   # the 'val’ variable will now hold the index ‘i’ of the ‘array$’ variable

NEXT

 

Userlevel 7
Badge +3

@Asuku:  It’s not completely clear if your question pertains to use of array variables or simply writing text data to file in your desired format.  If appending is the problem, try using the “OUTPUT filename$” statement one time up front, then loop through your field and frequency values, writing the results to file inside the inner loop.

log_filename$ = "D:\data_file.txt"
OUTPUT log_filename$
FORMAT 10.7

! loop thru field values
FOR f = 1, 4, 1

! loop thru freq values
FOR i = 0, 180, 10

! Define GETMTF input parameters
freq = i
wave = 1
field = f
sampling = 3
vector = 1
type = 1

! call GETMTF
GETMTF freq, wave, field, sampling, vector, type

! save results to file
PRINT f, ", ", i, ", ", vec1(0), ", ", vec1(1)

NEXT

NEXT

Regards,

Jeff

Userlevel 1

Thanks Michael and Jeff. I would like to iterate over a string array variables.

I tried the following but it gives error:

A$ = “one,two,three”

$GETSTRINGC(A$, 2)

B$ = "one two three"

$GETSTRING(B$, 2)

Syntax error: Variable must be followed by = sign.
$GETSTRINGC ( A$ , 2 )

This is an example direct from the Zemax help docs.

 

Also, with Michael’s example: I get the following error

Syntax
0.0000
error:
0.0000
1
0.0000
unrecognized
0.0000

 

Userlevel 7
Badge +3

The string or string array should contain numeric characters.  If you spell out the numbers, SVAL will simply provide a floating point representation of the word, not the actual number you want.  For example, you can run a few simple tests like this to see the results:

 

Reply