Function to get an Axis Label

Buggy! See below.

This function, given the name of graph ("" uses the top graph window) and the name of an axis, returns the axis label.
Function/S GetAxisLabel(gname, axisname)
    String gname, axisname
   
    String grecreation = WinRecreation(gname, 0)
    String oneLine
    String labelStr = ""
    String part1, part2
   
    Variable i=0
    do
        oneLine = StringFromList(i, grecreation, "\r")
        if (stringmatch(oneLine, "\tLabel*"))
            String regexp = "^\\tLabel "+axisname+" \\\"(.*)\\\""
            SplitString/E=regexp oneLine, labelStr
            break;
        endif
        i += 1
    while(strlen(oneLine) > 0)
   
    return labelStr
end


The regular expression works like this:

^

match the beginning of the string

\\t

match a tab character. The double backslash is because Igor interprets the backslash first, so to get a single backslash all the way through to SplitString, you need two of them.

Label

matches the literal string "Label "

+axisname+

matches the axis name. We hope there are no special characters in the axis name! Note that the space after the next quote mark matches the space between the axis name and the beginning of the quoted string

\\\"

match the opening quote on the axis label itself

(.*)

match all characters, and the () make it a substring which will be pulled out and put into the string variable labelStr

\\\"

match the closing quote


Hi,

thanks for the hint here. I don't know why but I had to change the stringmatch line to
if (stringmatch(oneLine, "\tLabel " + axisname + "*"))

otherwise the returned labelStr was empty if querying the bottom axis.
Maybe this has something to do with the multiline axis labels from Igor 6.1 which I'm using?

Using Igor 6.11, I did this:

•make junk=x;display junk
•Label bottom "one line\ranother line"
•print GetAxisLabel("graph0", "bottom")
one line\ranother line
•display/B=bottomAxis junk
•Label bottomAxis "one line\ranother line"
•print GetAxisLabel("graph1", "bottomAxis")
one line\ranother line

I used the code I posted above without modification; as you can see, it worked correctly. If you would like, send a copy of your experiment file to support@wavemetrics.com and I'll try to figure out why the code failed on your graph.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
OK- After all this time, Thomas Braun has come back to this topic and proven that I am thick-headed. My test in my original response was flawed- it applied my original code to two different graphs, each of which had a label on only one axis. Naturally, it worked because the graph recreation macro in that case has only one Label command. The bug in the original code is that the StringMatch function matches any Label command, not just the one we're interested in. But the regular expression fails on a Label command for the wrong axis. Thomas' correction makes sure the StringMatch function matches only the correct Label command.

Here is an alternative that dispenses with the StringMatch altogether:
Function/S GetAxisLabel(gname, axisname)
    String gname, axisname
 
    String grecreation = WinRecreation(gname, 0)
    String oneLine
    String labelStr = ""
    String part1, part2
 
    Variable i=0
    do
        oneLine = StringFromList(i, grecreation, "\r")
        String regexp = "^\\tLabel "+axisname+" \\\"(.*)\\\""
        SplitString/E=regexp oneLine, labelStr
        if (strlen(labelStr) > 0)
            break;
        endif
        i += 1
    while(strlen(oneLine) > 0)
 
    return labelStr
end


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
I have to wonder whether the line-by-line parsing of the recreation macro is needed and/or is slower versus using a more focused one-shot regExp parsing of the entire recreation string?

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
This function in
#include <Axis Utilities>

works fine for a graph with multiple axis labels (though I see it fails with labelled subgraphs) and it doesn't loop through all the lines:

/ Returns the text of the axis label.
// If optional parameter SuppressEscaping is non-zero, extra backslashes ("\\" is required
// to include a single backslash in a quoted string) are removed
// so that the result is a string suitable to be passed directly to the Label operation.
Function/S AxisLabelText(graphName, axisName, [SuppressEscaping])
    String graphName, axisName
    Variable SuppressEscaping
   
    if (ParamIsDefault(SuppressEscaping))
        SuppressEscaping = 0
    endif
   
    String axisLabel=""
    String info= WinRecreation(graphName,0)
    Variable start= strsearch(info, "Label "+axisName, 0)
    if( start >= 0 )
        start = strsearch(info, "\"", start)+1
        Variable theEnd= strsearch(info, "\"", start)-1
        axisLabel= info[start,theEnd]
    endif
    if (SuppressEscaping)
        start = 0
        do
            start = strsearch(axisLabel, "\\\\", start) // search for double backslash
            if (start >= 0)
                string newLabel = axisLabel[0,start-1]
                newLabel += axisLabel[start+1, strlen(axisLabel)-1]
                axisLabel = newLabel
            else
                break
            endif
        while(1)
    endif
    return axisLabel
End

Example:
make junk=x;display junk
Label bottom "one line\ranother line"
print AxisLabelText("graph0", "bottom")
  one line\ranother line
Label left "Left Axis Label"
print AxisLabelText("graph0", "bottom")
  one line\ranother line
print AxisLabelText("graph0", "left")
  Left Axis Label

--Jim Prouty
Software Engineer, WaveMetrics, Inc.

Needed this now for IP8 and IP9, this should be shorter:

Function/S ReturnLabelForAxis(GraphName, AxisName)
    string GraphName, AxisName
    //string function, returns label for graph + axis specified
    //want to get label for axis here, example of expected labels :
    //Label left "Intensity [arb]"
    //Label bottom "Q [A\\S-1\\M]"
    string OldClipb = GetScrapText()
    PutScrapText WinRecreation(GraphName, 0)
    Grep /E={AxisName,0}/Q/LIST "Clipboard"
    string MyAxisLabel = S_value[strsearch(S_value, "\"", 0)+1, strsearch(S_value, "\"", strlen(S_value)-1,1 )-1]
    MyAxisLabel = ReplaceString("\\\\", MyAxisLabel, "\\")
    PutScrapText OldClipb
    return MyAxisLabel
end

It is not idiot proof, there are clearly many cases when it may fail. 

I am really confused why there is no Igor function which would return this.  

There is an AxisLabelText user function already in 

#include <Axis Utilities>

But Igor 9 will (soon) have an AxisLabel function.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More