Graph Names

Hi,

Is it possible to control the method with which Igor names graphs? For example, when I create a graph called EGraph in the code and call that graph multiple times, what Igor does by default is name the first graph EGraph, the second EGraph_1, the third EGraph_2, and so on. Instead, can I make it so that Igor defaults to naming the first graph EGraph1, the second EGraph2, etc.?

Thanks.
Wouldn't it be the easierst way to take care of the naming in your code? I use:
    String WindowName
    do 
        WindowName = "EGraph" + Num2Str(i)
        i = WinType(WindowName) > 0 ? i + 1 : -1
    while (i > -1)
Quote:
Is it possible to control the method with which Igor names graphs?


No. But you can specify window names yourself. Use the /N= flag.

Quote:
For example, when I create a graph called EGraph in the code and call that graph multiple times, what Igor does by default is name the first graph EGraph, the second EGraph_1, the third EGraph_2, and so on.


That's because you are running a Window macro named EGraph. Usually when writing a sophisticated package, we dispense with Window macros, which are intended to create one specific window, and change them to user-defined functions (Function CreateMyGraph()).

In the user-defined function you would call Display /N=$name after setting the string variable name to whatever you want.

You can use:
UniqueName("EGraph", 6, 0)

to determine the next available name.
I see. Thank you. After taking your advice, I changed the code so that the graph is called from a user-defined function instead of a window macro, and the code works as I want. Is it true that the code does not change at all between the two formats, except for that I could change "variable/G Index" into "NVAR Index" and that I need to add /N to Display if I want to alter the default graph name?

I originally had:
Window SGraph() : Graph
    PauseUpdate; Silent 1       // building window...
    variable/G Index
    string FolderName = "FolderName_" + num2str(Index)
    string GraphName = "GraphName_" + num2str(Index)

    doWindow/F SGraph
    doWindow/W=$WinName(0,1,1)/C $GraphName
    string Title = "Trial " + num2str(Index)
    SetDataFolder root:$FolderName

    Display/W=(503.25,64.25,1049.25,374) Y_Axis vs X_Axis as Title


For some reason, the line "doWindow/W=$WinName(0,1,1)/C $GraphName" does not act on SGraph even though I call it to the front on the previous line. Am I using the WinName command incorrectly?

Also, what does the line "i = WinType(WindowName) > 0 ? i + 1 : -1" in chozo's code do? Where can I read more about this type of syntax, and where is it generally used?
Quote:
For some reason, the line "doWindow/W=$WinName(0,1,1)/C $GraphName" does not act on SGraph even though I call it to the front on the previous line.


I'm not clear on what you are trying to do. It seems like you are trying to change the name of the graph window before it is created. The DoWindow calls occur before the Display call and Display is what creates the graph.

Also, putting SetDataFolder in a window macro is not a good idea. A procedure should not change the current data folder unless that is the point of the procedure. Otherwise it is a non-obvious side-effect that is likely to cause trouble and be hard to track down.

Here is a way to do this from a user-defined function without changing the current data folder:
Function CreateSGraph(index)
    Variable index
   
    String dataFolderName = "F_" + num2str(index)
    DFREF dfr = root:$dataFolderName    // Create data folder reference
    SVAR title = dfr:title
    Wave yWave = dfr:Y_Axis, xWave = dfr:X_Axis
    // . . .
    Display yWave vs xWave as title
    // . . .
End


Note that index is passed in as a parameter, not accessed via a global variable. Use of global variables makes programs difficult to understand and debug because it is not obvious from an invocation of a function what it depends on. Instead, as much as possible, pass the information needed by the function via parameters.

In this case it is likely that title should also be a parameter. Probably also xWave and yWave.

To read about data folder references:
DisplayHelpTopic "Data Folder References"




Quote:
Also, what does the line "i = WinType(WindowName) > 0 ? i + 1 : -1" in chozo's code do? Where can I read more about this type of syntax, and where is it generally used?


The expression "test ? true : false" is a shorthand of a If-Else-Endif statement (for numerical expressions) like:

if (test)
    true
else
    false
endif


But it's only one line of code and when you are finally able to read it it's very convenient and can also be expanded into multiple branches. You can read about it in the command help (look for '? :').

Quote:

You can use:
UniqueName("EGraph", 6, 0)



I'm amazed that there is a function like this. It's really astounding that there is a function already implemented in Igor for every workaround I write. I thought I went trough all the functions already. Now, where is the function to round arbitrary numbers to 'nice' numbers (e.g. 129930 -> 100000, 0.0345 -> 0.03)? :)
chozo wrote:
I'm amazed that there is a function like this. It's really astounding that there is a function already implemented in Igor for every workaround I write. I thought I went trough all the functions already. Now, where is the function to round arbitrary numbers to 'nice' numbers (e.g. 129930 -> 100000, 0.0345 -> 0.03)? :)


When I want to do something like this I normally use sprintf/prinf, etc to create formatted text. e.g.
printf "%0.2f", 0.0345


For the other rounding thing you probably want to use a log.
I see. When using the Window macro to create a graph, no window is actually created until the display line. I thought because of the structure of the macro, the window is created as soon as it is called (e.g. as soon as I type "execute "Win_Name"" into the command line). For example, in
Window Win_Name() : Graph
    Display ...
EndMacro

the graph name appears to be defined as Win_Name even before the display line.

I'll keep in mind to use parameters more often when creating functions rather than relying on global variables.

Thanks for explaining the unique syntax, chozo. I found it in the manual under logic operators, and may test it out in the future.