How to check for and build/replace tables or graphs

I would like my functions to store/graph data, but only in specific tables/graphs and to rewrite/update any existing data of the same names. How do I code for this kind of check?

So if I graph wave_y vs wave_x, it checks to see if its already graphed, overwrites if it is, or creates a new graph if not. Currently im getting a ton of graphs that I have to close manually..

Im aware that tables automatically update, but the question arises again if I would like a new table in case i switch to wave_a and wave_b from wave_x and wave_y.


Hope that made sense..
One solution is to use named windows.

// check if a graph with the name "dummy" exists
DoWindow /F dummy   // /F means 'bring to front if it exists'
if (V_flag == 0)
    // window does not exist
    Display /N=dummy myData // note '/N=' flag
else
    // window exists.
endif


Note the following documentation for Display:
Igor Manual wrote:
/N=name Requests that the created graph have this name, if it is not in use. If it is in use, then name0, name1, etc. are tried until an unused window name is found. In a function or macro, S_name is set to the chosen graph name. Use DoWindow/K name to ensure that name is available.


This is why it's important to test for window existence first, since otherwise you'll automatically end up with windows called "dummy0", "dummy1", and so forth.

Alternatively you could loop over all graph windows (WinList) to see if any of these displays wave_y. But given the information you provide I would probably used named windows.

This concepts translates to tables as well.
This is exactly what I was looking for. Thanks, you guys are great.

EDIT: btw, it should be IF (V_flag ==0) if you are checking that the window does NOT exist. Took me a minute to figure out why that code wasnt working ;-)

EDIT EDIT: Ok, what do I do then if my window does exist and I want to overwrite the graphs? I cant seem to find any overwrite function in Display, is it necessary to kill the window and redraw it?
daggaz wrote:
Ok, what do I do then if my window does exist and I want to overwrite the graphs? I cant seem to find any overwrite function in Display, is it necessary to kill the window and redraw it?


I'm not sure what you mean by "overwrite the graphs". Some possibilities are:


  1. Replace the data in a wave already displayed as a trace (just change the data in a displayed wave).

  2. Replace a trace with another trace from a different wave (use ReplaceWave - Graph->Replace Wave).

  3. Create an entirely new graph with the same name (kill the old one and create a new one).


Sorry. What I mean is that I am replacing/changing the wave data and I want to regraph the waves into existing windows under the same names.

At the moment I am using dowindows/K to just kill it and then rebuilding it according to the code above, but I do wonder if this is the proper method.
daggaz wrote:
Sorry. What I mean is that I am replacing/changing the wave data and I want to regraph the waves into existing windows under the same names.

At the moment I am using dowindows/K to just kill it and then rebuilding it according to the code above, but I do wonder if this is the proper method.


If you change the wave data the graph will automatically update. There is no need to regraph it.

Here is an example. Execute Demo() from the command line several times:
StrConstant kMyGraphName = "MyGraph"

Function Demo()
    // Create wave
    Wave/Z myWave = root:myWave
    if (!WaveExists(myWave))
        Make myWave
    endif
    Wave myWave = root:myWave       // Create wave reference after wave exists
   
    // Store data in wave
    myWave = gnoise(1)
   
    // Graph wave if it is not already graphed 
    if (WinType(kMyGraphName) == 0)
        // Graph does not exist
        Display /N=$kMyGraphName myWave
    endif
End


In the event you did want to change which waves appeared on the graph (lets say you have one that's raw data and one that's scaled).

You can use TraceNameList to get the waves currently on the graph then use RemoveFromGraph to remove these traces. Finally you can add the new waves by using AppendtoGraph. Of course this isn't much different than killing the window and starting fresh.

Of course, my crude example could also be accomplished by having both waves on the graph but hiding one or the other depending on the situation.
daggaz wrote:
btw, it should be IF (V_flag ==0) if you are checking that the window does NOT exist. Took me a minute to figure out why that code wasnt working ;-)


Good catch! I've updated my original post.
daggaz wrote:
I feel rather dumb now. =/

But thanks!


No need to...Matlab works the way you were assuming Igor worked, and when I first started using Matlab, it took me a few tries to figure out why my graph wasn't updating when I changed the data :-). I prefer Igor's behavior as a general rule, but it's not the only way to do things.
With regards to automatic graph updates..

I am overwriting one of my waves using
integrate /d= outputwave, where outputwave is called using a string reference $outputwave_name,

and where outputwave (and its string ref) is a pre-existing wave (from the last time i used the function)

The table gets edited correctly, but the graph gets made with outputwave#1 instead of just outputwave. That screws up my trace modifications, etc, etc.

This doesn't happen with the other pre-existing waves in the same graph. Why the #(number) suffix? I tried to fix the behavior by removing and killing the wave in question beforehand, but to no avail.

EDIT: figured it out finally. I was appending the trace to the graph, and in this case the trace already existed. I had forgotten about that line when I included the dowindow logic. Moved it inside the loop and all is well.