Expected wavename error while using KillWaves in function

Hi all,

I have the following code which runs fine in general:

function processForceImage(scaledwave,d)
String scaledwave
variable d
variable n=1,m=1,c, k=1
Wave yourwave=::$scaledwave

//clean the folder first, NEXT COMMAND IS DANGEROUS, MAKE SURE YOU ARE IN RIGHT FOLDER FIRST
Variable x=0
Prompt x, "Kill Waves? (1 if yes, 0 if no)"
DoPrompt "Kill Waves?", x
if (x==1)
KillWaves/A
endif

FFT/OUT=3/WINF=Hanning/DEST=W_FFT yourwave
FFT/ROWS/DEST=$(scaledwave+"_FFTrows") yourwave //FFT is complex
WaveTransform magnitude $(scaledwave+"_FFTrows")
Rename M_Magnitude,$(scaledwave+"_FFTrows_mag")
SumDimension/D=0/Y=-1/DEST=$(scaledwave+"_FFTrows_sum") $(scaledwave+"_FFTrows_mag")
SetScale/I x 0,10.235,"nm", $(scaledwave+"_FFTrows_sum") //specific to this image
Duplicate yourwave $(scaledwave+"_invert")
wave wave2 = $(scaledwave+"_invert")
wave2 = yourwave[q][p]
Duplicate wave2 $(scaledwave+"_1D")
Redimension/N=4194304 $(scaledwave+"_1D") //specific to this image i.e. 2048x2048
FFT/OUT=1/WINF=Hanning/DEST=$(scaledwave+"_1D_FFT") $(scaledwave+"_1D")
//Repeat 2&3 for one quadrant

//loop for each sub-region
for (n=1; n for(m=1; m c=2048/d
Make/O/N=(c,c) $(scaledwave+"_"+num2str(k))
wave wave3 = $(scaledwave+"_"+num2str(k))
wave3 = wave2[p+(m-1)*2048/d][q+(n-1)*2048/d]
SetScale/P x 0,0.0488519785051295,"nm", $(scaledwave+"_"+num2str(k));DelayUpdate //this image
SetScale/P y 0,0.0488519785051295,"nm", $(scaledwave+"_"+num2str(k)) //this image
FFT/COLS/DEST=$(scaledwave+"_"+num2str(k)+"_FFT") $(scaledwave+"_"+num2str(k)) //COLS since using the inverted image
//Now sum the columns
WaveTransform magnitude $(scaledwave+"_"+num2str(k)+"_FFT")
Rename M_Magnitude,$(scaledwave+"_"+num2str(k)+"_FFT_mag")
SumDimension/D=1/Y=-1/DEST=$(scaledwave+"_"+num2str(k)+"_FFTcols_sum") $(scaledwave+"_"+num2str(k)+"_FFT_mag")
SetScale/I x 0,10.235,"nm", $(scaledwave+"_"+num2str(k)+"_FFTcols_sum") //this image
//Convert to 1D and FFT
Duplicate $(scaledwave+"_"+num2str(k)) $(scaledwave+"_"+num2str(k)+"_1D")
variable b = 2048*2048/d/d
Redimension/N=(b) $(scaledwave+"_"+num2str(k)+"_1D") //this image
FFT/OUT=1/WINF=Hanning/DEST=$(scaledwave+"_"+num2str(k)+"_1D_FFT") $(scaledwave+"_"+num2str(k)+"_1D")
KillWaves $(scaledwave+"_"+num2str(k)), $(scaledwave+"_"+num2str(k)+"_FFT") , $(scaledwave+"_"+num2str(k)+"_1D")
KillWaves $(scaledwave+"_"+num2str(k)+"_FFT_mag"), $(scaledwave+"_"+num2str(k)+"_1D")
k=k+1
endfor
m=1
endfor

KillWaves $(scaledwave+"_FFTrows"), $(scaledwave+"_FFTrows_mag"), $(scaledwave+"_invert"),$(scaledwave+"_1D"),W_FFT
end


The only problem is that I get the following error: While executing KillWaves, the following error occurred: expected wave name

Using the debugger, I can see that the problem is 7 lines from the end in the For loop with the command:
KillWaves $(scaledwave+"_"+num2str(k)+"_FFT_mag"), $(scaledwave+"_"+num2str(k)+"_1D")

It can't find $(scaledwave+"_"+num2str(k)+"_FFT_mag") even though every other wave in the 3 KillWaves command works fine. I know the name is correct since I cut and pasted it from earlier in the code and I can see in the debugger that at that step, the wave with that name exists in the current data folder. I don't understand why it can't find this wave but can find every other wave with same $name.

Does anyone see my error?

Thanks,
Brandon


jmcclimo2003 wrote:
Hi all,

I have the following code which runs fine in general:

function processForceImage(scaledwave,d)
String scaledwave
variable d
variable n=1,m=1,c, k=1
Wave yourwave=::$scaledwave

    //clean the folder first, NEXT COMMAND IS DANGEROUS, MAKE SURE YOU ARE IN RIGHT FOLDER FIRST
Variable x=0
Prompt x, "Kill Waves? (1 if yes, 0 if no)"
DoPrompt "Kill Waves?", x
if (x==1)
    KillWaves/A
endif

FFT/OUT=3/WINF=Hanning/DEST=W_FFT  yourwave
        FFT/ROWS/DEST=$(scaledwave+"_FFTrows") yourwave         //FFT is complex
WaveTransform magnitude  $(scaledwave+"_FFTrows")
Rename M_Magnitude,$(scaledwave+"_FFTrows_mag")
SumDimension/D=0/Y=-1/DEST=$(scaledwave+"_FFTrows_sum") $(scaledwave+"_FFTrows_mag")
SetScale/I x 0,10.235,"nm", $(scaledwave+"_FFTrows_sum")  //specific to this image
Duplicate   yourwave $(scaledwave+"_invert")
wave wave2 = $(scaledwave+"_invert")
wave2 = yourwave[q][p]
Duplicate   wave2 $(scaledwave+"_1D")
Redimension/N=4194304 $(scaledwave+"_1D")   //specific to this image i.e. 2048x2048
FFT/OUT=1/WINF=Hanning/DEST=$(scaledwave+"_1D_FFT")  $(scaledwave+"_1D")
    //Repeat 2&3 for one quadrant

    //loop for each sub-region
for (n=1; n<d+1; n+=1)
    for(m=1; m<d+1; m+=1)
        c=2048/d
        Make/O/N=(c,c) $(scaledwave+"_"+num2str(k))
        wave wave3 = $(scaledwave+"_"+num2str(k))
        wave3 = wave2[p+(m-1)*2048/d][q+(n-1)*2048/d]
        SetScale/P x 0,0.0488519785051295,"nm", $(scaledwave+"_"+num2str(k));DelayUpdate  //this image
        SetScale/P y 0,0.0488519785051295,"nm", $(scaledwave+"_"+num2str(k))                    //this image
        FFT/COLS/DEST=$(scaledwave+"_"+num2str(k)+"_FFT")  $(scaledwave+"_"+num2str(k)) //COLS since using the inverted image
                //Now sum the columns
        WaveTransform magnitude  $(scaledwave+"_"+num2str(k)+"_FFT")
        Rename M_Magnitude,$(scaledwave+"_"+num2str(k)+"_FFT_mag")
        SumDimension/D=1/Y=-1/DEST=$(scaledwave+"_"+num2str(k)+"_FFTcols_sum") $(scaledwave+"_"+num2str(k)+"_FFT_mag")
        SetScale/I x 0,10.235,"nm", $(scaledwave+"_"+num2str(k)+"_FFTcols_sum")     //this image
            //Convert to 1D and FFT
        Duplicate   $(scaledwave+"_"+num2str(k))  $(scaledwave+"_"+num2str(k)+"_1D")
        variable b = 2048*2048/d/d
        Redimension/N=(b) $(scaledwave+"_"+num2str(k)+"_1D")                //this image
        FFT/OUT=1/WINF=Hanning/DEST=$(scaledwave+"_"+num2str(k)+"_1D_FFT")  $(scaledwave+"_"+num2str(k)+"_1D")
        KillWaves $(scaledwave+"_"+num2str(k)), $(scaledwave+"_"+num2str(k)+"_FFT") , $(scaledwave+"_"+num2str(k)+"_1D")
        KillWaves $(scaledwave+"_"+num2str(k)+"_FFT_mag"), $(scaledwave+"_"+num2str(k)+"_1D")
        k=k+1
    endfor
    m=1
endfor

KillWaves $(scaledwave+"_FFTrows"), $(scaledwave+"_FFTrows_mag"), $(scaledwave+"_invert"),$(scaledwave+"_1D"),W_FFT
end


The only problem is that I get the following error: While executing KillWaves, the following error occurred: expected wave name

Using the debugger, I can see that the problem is 7 lines from the end in the For loop with the command:
KillWaves $(scaledwave+"_"+num2str(k)+"_FFT_mag"), $(scaledwave+"_"+num2str(k)+"_1D")

It can't find $(scaledwave+"_"+num2str(k)+"_FFT_mag") even though every other wave in the 3 KillWaves command works fine. I know the name is correct since I cut and pasted it from earlier in the code and I can see in the debugger that at that step, the wave with that name exists in the current data folder. I don't understand why it can't find this wave but can find every other wave with same $name.

Does anyone see my error?

Thanks,
Brandon


I don't have an answer, but would suggest that you use something like Wave wFFT_mag = $(scaledwave+"_"+num2str(k)+"_FFT_mag") to refer to these waves instead of using the string conversion. It will make your code easier to read and will cut down on the possibility of difficult to trace typos. You do this in one place for "wave3", but then fall back to referring to the same wave using the "$..." reference.

Sorry I don't have anything more specific, but, maybe this will help.
Thanks jtigor. Point taken about cleaning up my code, especially if I am going to share with others. I tried using a wave reference for that wave as you suggest and the wave reference in the killwaves command but it still gives the error and doesn't delete the wave. It's just very strange that KillWaves works fine on every other wave.
Hi,

I was looking at your code around the killwaves command
        KillWaves $(scaledwave+"_"+num2str(k)), $(scaledwave+"_"+num2str(k)+"_FFT") , $(scaledwave+"_"+num2str(k)+"_1D")
        KillWaves $(scaledwave+"_"+num2str(k)+"_FFT_mag"), $(scaledwave+"_"+num2str(k)+"_1D")

It looks like you are trying to kill one wave twice, $(scaledwave+"_"+num2str(k)+"_1D").

It was killed in the first line and the second line can't find it and gives an error.

Andy
Also, if you use the "/Z" flag KillWaves: "Doesn't generate an error if a wave to be killed is in use or does not exist."