Determine wave type for reference
I am stuck on what must be a very simple problem. I have some code where the user selects a wave in a dialog using WaveSelectorWidget. They could pick a numeric or a text wave - either are fine - but each case needs to be handled later in the code.
Currently, the choice from the widget is stored in a global text wave as a wave name.
I then call a function to check its type and then attempt to process:
// replicate wave - this can be numeric or text - convert to numeric 0-based
Variable wtVar = numericOrText(waveNameWave[0])
if(wtVar == 2)
// replicate wave is text
Wave/T/Z repW = $(waveNameWave[0])
FindDuplicates/RT=uRepW repW
elseif(wtVar == 1)
// replicate wave is numeric
Wave/Z repW = $(waveNameWave[0])
FindDuplicates/RN=uRepW repW
else
DoAlert 0, "Wrong wave type"
return -1
endif
// do the conversion...
// similar routine done several times
//function is....
STATIC Function numericOrText(wName)
String wName
Wave w = $wName
return WaveType(w,1)
End
The compiler complains about mixing wave references. Adding the /Z flag changes where it complains in the code but I get the feeling that I'm not going about this the right way. I'd like to keep the wave reference name the same but feel free to tell me that a different reference for each type is the way to go.
Is there a more simple way to do what I am doing?
You can make a numeric wave reference for either wave type, but you can't use that reference for a text wave in places where you use it. Use WaveType(w, 1) to find out if it is numeric or text. If it is text, you will then need to make a new wave reference using Wave/T. Use that new wave reference to get the strings out of the wave.
March 10, 2022 at 03:18 pm - Permalink
Thanks John. That makes sense but if I do:
Wave/Z w = $"possibleTextWaveName"
if(WaveType(w,1) == 2)
Wave/T/Z w = $"possibleTextWaveName"
// do something with textwave
else
// do something with numericwave
endif
End
I get the "inconsistent type for a wave reference" error; even if I do WaveClear before the new wave reference (with text flag). I guess the only solution is to use a distinct name for the new wave reference.
March 10, 2022 at 11:11 pm - Permalink
You need to use a different local name for the text wave reference, such as "tw".
March 11, 2022 at 08:57 am - Permalink
Yes, hrodstein is correct. That's what I meant by "new wave reference". I should have been more explicit.
March 11, 2022 at 09:57 am - Permalink