Please Help, How to Export Graph as Text File

Hi Everybody,

I was wondering if someone can help me with this. I have a plot with multiple waves and it some cases 2 different y-axes and I was wondering how to make a procedure that would export these graphs as text files so that I can import them easily into another program (Excel or KaleidaGraph).

Thanks!
Assuming all of your traces are in the root datafolder and there are no x-axis waves to worry about,

string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph

save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path


If you have x-waves you also want to include in the file, you will need to modify the stringlist to contain the x-waves in the proper location (do you want them first or last???). If your datawaves are in various folders, the stringlist will have to be modified to contain the complete folder paths (root:folder1:wave1; root:folder2:wave2;)
Thank you! I don't have any x-waves to worry about. But I am a little confused with the procedure you gave me. I created a procedure in Igor, saved it at a macro, put the graph I wanted on top but i get an error highlighting "list" saying expected wave name.
proland wrote:
Assuming all of your traces are in the root datafolder and there are no x-axis waves to worry about,

string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph

save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path


If you have x-waves you also want to include in the file, you will need to modify the stringlist to contain the x-waves in the proper location (do you want them first or last???). If your datawaves are in various folders, the stringlist will have to be modified to contain the complete folder paths (root:folder1:wave1; root:folder2:wave2;)


Thank you! I don't have any x-waves to worry about. But I am a little confused with the procedure you gave me. I created a procedure in Igor, saved it at a macro, put the graph I wanted on top but i get an error highlighting "list" saying expected wave name.
redrmangus2 wrote:
I created a procedure in Igor, saved it at a macro...


Not sure what that means but if you:

- open the procedure window and copy the following into it:

Macro ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph
    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end


- then click on the graph from which you want to export the data (this is important to make it the "top graph")
- then click on the command window and execute "ExportData()" from it. Or chose it from the Macro menu. That should work.

Alternatively, just execute the two commands within the function from the command line, after having clicked on the graph

Sorry, the basic code there only works if the waves are in the current data folder (I'm assuming that's the problem you encountered). This addition will determine the datafolder from which the waves are taken. The more robust method would be to include the datafolder paths with the tracenames.

        string list = tracenamelist("", ";", 1)                  //all traces
       
        string str1=StringFromList(0,list, ";")         //first trace name
    Wave w = TraceNameToWaveRef("", str1)       //first trace wave reference
    String DF= GetWavesDataFolder(w,1)             //data folder of first trace  (we are assuming all are the same)
        SetDataFolder $DF                                        //setdatafolder

        Save/G/B list as "OutputFile.txt"
ChrLie wrote:
redrmangus2 wrote:
I created a procedure in Igor, saved it at a macro...


Not sure what that means but if you:

- open the procedure window and copy the following into it:

Macro ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph
    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end


- then click on the graph from which you want to export the data (this is important to make it the "top graph")
- then click on the command window and execute "ExportData()" from it. Or chose it from the Macro menu. That should work.

Alternatively, just execute the two commands within the function from the command line, after having clicked on the graph


when I do that, I get the error I talked about in the above post. I have verbatim that.
Quote:
when I do that, I get the error I talked about in the above post. I have verbatim that.


What about my previous post? Make sure that the data plotted on the graph is in the current datafolder.
proland wrote:
Quote:
when I do that, I get the error I talked about in the above post. I have verbatim that.


What about my previous post? Make sure that the data plotted on the graph is in the current datafolder.


If I use your update procedure I get a different Macro Execute Error Stating "expected wave name, variable name, or operation" in reference to the wave in "Wave w"
I must admit I'm confused by that error. TraceNametoWaveRef is used in the exact same way in the manual.
Ok, got it now.... The error you were seeing is because wave references can't be made inside a Macro, it has to be a function. Use the following code (fixes error and more robust).

Function ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph    

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
        string str1=StringFromList(i,list, ";")         //first trace name
        Wave w = TraceNameToWaveRef("", str1)       //first trace wave reference
        String DF= GetWavesDataFolder(w,2)             //data folder of first trace  (we are assuming all are the same)
        list = removefromlist(StringFromList(i,list, ";"), list)
        list = addlistitem(DF, list, ";", i)
    endfor

    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end

proland wrote:
Ok, got it now.... The error you were seeing is because wave references can't be made inside a Macro, it has to be a function. Use the following code (fixes error and more robust).

Function ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph    

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
        string str1=StringFromList(i,list, ";")         //first trace name
        Wave w = TraceNameToWaveRef("", str1)       //first trace wave reference
        String DF= GetWavesDataFolder(w,2)             //data folder of first trace  (we are assuming all are the same)
        list = removefromlist(StringFromList(i,list, ";"), list)
        list = addlistitem(DF, list, ";", i)
    endfor

    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end


OK, so the error is gone, but as I run the macro, nothing happens. I don't know where it is being saved if it is.
redrmangus2 wrote:
proland wrote:
Ok, got it now.... The error you were seeing is because wave references can't be made inside a Macro, it has to be a function. Use the following code (fixes error and more robust).

Function ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph    

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
        string str1=StringFromList(i,list, ";")         //first trace name
        Wave w = TraceNameToWaveRef("", str1)       //first trace wave reference
        String DF= GetWavesDataFolder(w,2)             //data folder of first trace  (we are assuming all are the same)
        list = removefromlist(StringFromList(i,list, ";"), list)
        list = addlistitem(DF, list, ";", i)
    endfor

    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end


OK, so the error is gone, but as I run the macro, nothing happens. I don't know where it is being saved if it is.


One last thing (sorry to keep perstering you), do you know how I can include the x-axis in the export? Was taht what you were talking about regarding the x-wave?
redrmangus2 wrote:
redrmangus2 wrote:
proland wrote:
Ok, got it now.... The error you were seeing is because wave references can't be made inside a Macro, it has to be a function. Use the following code (fixes error and more robust).

Function ExportData()
    string list = tracenamelist("", ";", 1)          //get list of traces plotted on y-axis of top-most graph    

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
        string str1=StringFromList(i,list, ";")         //first trace name
        Wave w = TraceNameToWaveRef("", str1)       //first trace wave reference
        String DF= GetWavesDataFolder(w,2)             //data folder of first trace  (we are assuming all are the same)
        list = removefromlist(StringFromList(i,list, ";"), list)
        list = addlistitem(DF, list, ";", i)
    endfor

    save/B/G list as "OutputFile.txt"               //you can replace "outputfile.txt",  //add a /P flag if you want to programatically specify the path
end


OK, so the error is gone, but as I run the macro, nothing happens. I don't know where it is being saved if it is.


OHHH you already saved it as a function. It works perfectly! Thank you!


One last thing (sorry to keep perstering you), do you know how I can include the x-axis in the export? Was taht what you were talking about regarding the x-wave?
I deleted your other thread about this same issue. Please don't paste code in as a graphic attachment.

As to your X values question, other programs may want to have the X values listed out for them.

This function should do the trick:

Function SaveWavesFromGraph(graphName)  // Saves all waves in graph.
    String graphName                            // "" for top graph.
   
    String list, traceList, traceName
    Variable index = 0
    list = ""
    traceList = TraceNameList(graphName, ";", 1)
    string tempXwavesList=""
    do
        traceName = StringFromList(index, traceList, ";")
        if (strlen(traceName) == 0)
            break
        endif
        Wave w = TraceNameToWaveRef(graphName, traceName)
        list += GetWavesDataFolder(w,2) + ";"
        Wave/Z wx= XWaveRefFromTrace(graphName, traceName )
        if( !WaveExists(wx) )
            // Make a temporary X wave we'll delete later
            String wxn= NameOfwave(w)[0,27]+"_x"
            Wave/Z wx=$wxn
            if( WaveExists(wx) )
                wxn= UniqueName(wxn,1,0)
            endif
            Duplicate/O w, $wxn
            Wave wx=$wxn
            Redimension/D wx    // the data may be integer, but the x values may be float or double.
            wx=x    // enumerate X values
        endif
        list += GetWavesDataFolder(wx,2) + ";"
        tempXwavesList += GetWavesDataFolder(wx,2) + ";"
        index += 1 
    while(1)

    if (strlen(list) > 0)
        Save/O/J/W/I/B list
        // dispose of temporary waves
        for( index=0; index < ItemsInList(tempXwavesList); index+=1)
            Wave wx= $StringFromList(index,tempXwavesList)
            KillWaves/Z wx
        endfor
    endif
End


--Jim Prouty
Software Engineer, WaveMetrics, Inc.