In some experiment scenarios you may need to open a text file containing, for example, randomized number sequences. You may choose not to load a permanent copy of such file into experiment’s Library, for instance, in cases when you regenerate the file content before each experiment run. Thus, the file is stored externally to your experiment. Then, the easiest way to read the file content is to use standard C# file functions directly in code snippets. If you need to read a file only once, at the start of experiment, then using the EXPERIMENT.OnInitializing snippet might be a good idea. The example below demonstrates this process in details.
Suppose, we have a text or csv file, with the following data in three lines:
2,4.2, condition 4
Notice that resulting arrays, FirstColumn, SecondColumn and ThirdColumn are declared as local variables within this code snippet. If you want to access the array data globally, across all snippets in an experiment, you need to declare the arrays in Header. The change is easy. Add to the Header snippet the following code:
Then, change array initialization in the EXPERIMENT.OnInitializing snippet.
As a final remark, always make sure that the file exists in the its location on a disk. If you try to read non-existing file, or a file with wrong content, the experiment run will be aborted on execution of the snippet.
Best wishes, OkazoLab team.