OK, I figured it out. I need to set the graph name using
page.longname$ = graphname$;
and then access the graph using its name with the ogl variable.
ogl:=[graphname$]
I attach a snippet of code for whoever has the same problem in the future:
// Plot the Heat Map
TemplateGeneric$ = "%(templatepath$)Generic_Perception-test.otpu";
graphname$ = "Perception test_%(date$)_%(subject$)_%(state$)_%(frequency$)_%(totaliterations$)_%(slope$)";
Template$ = "%(templatepath$)Perception test_%(state$)_%(frequency$)_%(totaliterations$)_%(slope$).otpu"; // This is the template path
if(exist(%(uid2name(range2uid(["%(graphname$)"]))$))==0 || exist(%(uid2name(range2uid(["%(graphname$)"]))$))==2) // if there is no graph for this type of measurement (or if just a workbook with the same name exists) then create a new graph
{
// Check if the template exists, otherwise, use a generic one
if (exist(Template$)!=-1)// if the template exists
win -t p "%(Template$)"; // create a graph window using template
else
win -t p "%(TemplateGeneric$)"; // create a graph window using a generic template
page.longname$ = graphname$; //this renames the active window (long name)
page.title = 1; // Let the workbook title shows Long Name only (1 = Long Name, 2 = short name, 3 = both)
};
else
win -a %(uid2name(range2uid(["%(graphname$)"]))$); // activate the graph window
win -a %(activeworkbook$); // activate the previous window (workbook)
page.active$ = "Heat Map"; // Switch to the second worksheet (Heat Map)
// irng 2! means we choose the second sheet
// type 105 means heat map
plotvm irng:=2! format:=xacross rowpos:=selrow1 colpos:=selcol1 type:=105 ogl:=[graphname$];
layer -a; // rescale the layer
win -a %(activeworkbook$); // activate the previous window (workbook)
window -ch 1; // hide the workbook that was processed