0

I'm using Igor Pro (I don't use this often). I'm trying to count the number of places to the right of a decimal, if there is one, for one wave. And then add a decimal place however many places to the left, that were in the first wave, to a second wave. For reference I attached a pic of what I currently have. The wave "test_correct" is what it SHOULD look like, and "fix_err" is what I'm currently outputting. I've been at this for awhile but can't seem to figure it out, any help would be appreciated. Thanks example

Function testErrFix()

    wave test_energy, test_err
    variable i, j, s
    variable len = numpnts(test_energy)
    make/O/D/N=(len) fix_err
    string current_Energy
    string current_Err
    string Error
    variable slen
    For(i=0;i<len;i+=1)
        current_Energy = num2str(test_energy[i])
        current_Err = num2str(test_err[i])
        slen = strlen(current_energy)
        Error = ""
        For(j=0;j<slen;j+=1)
          If(Stringmatch(current_Energy[j], ".")==1)
           For(s=j;s<slen;s+=1)
                 Error += "." + current_Err[s-4] + current_Err[s-3] + current_Err[s-2]
                EndFor
           ElseIf(Stringmatch(current_Energy[j], "")==1)
             Error += current_Err[s+1]
                For(s=j;s<slen;s+=1)
                 Error = current_Err[s]
                EndFor
                EndIf
            EndFor
            fix_err[i] = str2num(Error)
            EndFor  
    
End
George
  • 1

1 Answers1

0

Your example uses num2str incorrectly. By default this only returns 5 decimal places, see its documentation. You can also tweak the number of digits shown in the table.

Could you ellaborate what you want to achieve? I never had the need to know the number of digits right of the decimal point so far.

EDIT: The number of decimal places depends on the way you convert the number to a string. So in general this will not work.

A naive implementation

Function/S num2strHighPrec(variable num)

    string str
    sprintf str, "%.15f", num

    return str
End

Function/S RemoveEndingExhaustive(string str, string ending)

    string result

    for(;;)
        result = RemoveEnding(str, ending)

        if(!cmpstr(result, str) || strlen(result) == 0)
            return result
        endif

        str = result
    endfor
End

Function CountDecimalPlaces(variable num)

    string numAsStr
    variable pos

    numAsStr = num2strHighPrec(num)
    numAsStr = RemoveEndingExhaustive(numAsStr, "0")
    pos = strsearch(numAsStr, ".", 0)
    
    if(pos < 0)
        return 0
    endif
    
    return strlen(numAsStr) - 1 - pos
End

might be a good starting point.

t-b
  • 152
  • 1
  • 7
  • So I have two waves "test_energy" and "test_err". The test_err values are the uncertainty values associated with the test_energy values, however they're missing the proper decimal placement. i.e. energy 2494.91 has uncertainty 3. But it should actually be 0.03 because 2494.91 has two values after the decimal. So I'm trying to write a macro that will count the places to the right of a decimal (if there is one) of an energy value, and then place a decimal that many places to the left of the corresponding uncertainty value. – George May 24 '21 at 19:15