Plotting waves called from a root:folder location

Hi, 

I have run into a new situation. Based on the advice I received here, I could now use the DFREF /SDFR to access waves from nonroot folders. That works great. However, when I try to plot the waves, the program tries to plot from root. In the code I have attached, the following things are supposed to happen: 

 

-----------------------------------

Take input waves from a folder called Volatilitybins

Set output data folder to SampleDBEmassdata

Generate waves called CnumDBEmassXn  where n=0-18 in the output folder

Plot the waves. 

----------------------------------------

The meat in the middle is all just data processing. And the meat is all alright because I have another version of the code saved where the only thing different is how I am calling the plot waves from output folder and how I am plotting it.

Here is the problem: 

The code compiles and when I run it, the panel that this code is a part of (as a button), crashes/disappears. I also have a notebook open in the program. Weirdly enough, as the panel crashes, the notebook gets renamed to the name I am assigning to the graph (please scroll to the bottom of the code for graphing details). I don't understand why this is happening. Then if I reopen the panel and rerun this code, the notebook gets killed of course because the code kills and reopens the window of that name to plot the graph in it. 

 

-----------------------------

What works: 

 Here is what works. If I don't output to SampleDBEmassdata folder. Instead output to root. Everything works fine in that case. 

---------------------------------

 

I would be very thankful for the guidance! What am I not seeing here? 

Sincerely, 

Peeyush

Function ButtonProc_54(ba) : ButtonControl
    STRUCT WMButtonAction &ba

    switch( ba.eventCode )
        case 2: // mouse up
            // click code here
           

            //setdatafolder root:Volatilitybins
            DFREF dfWorkDir=root:Volatilitybins
            WAVE/SDFR=dfWorkdir bin1,bin2,bin3,bin4,bin5,bin6,bin7,bin8,bin9,bin10
            WAVE/SDFR=dfWorkdir bin11,bin12,bin13,bin14,bin15,bin16,bin17,bin18,bin19,bin20
            WAVE/SDFR=dfWorkdir bin21,bin22,bin23,bin24,bin25,bin26,bin27,bin28,bin29,bin30
            WAVE/SDFR=dfWorkdir bin31,bin32
            WAVE/SDFR=dfWorkdir bin33,bin34,bin35,bin36,bin37,bin38,bin39,bin40
            WAVE/SDFR=dfWorkdir bin41,bin42,bin43,bin44,bin45,bin46,bin47,bin48,bin49,bin50
            WAVE/SDFR=dfWorkdir bin51,bin52,bin53,bin54,bin55,bin56,bin57,bin58,bin59,bin60
            WAVE/SDFR=dfWorkdir bin61,bin62,bin63,bin64,bin65,bin66,bin67,bin68,bin69,bin70
            WAVE/SDFR=dfWorkdir bin71,bin72,bin73,bin74,bin75,bin76,bin77,bin78,bin79,bin80
           
           
           
if(dimsize(responsefactoravg,0)==dimsize(DBEmatrix,0))   //Safety feature so that matrix of same dimensions are multiplied. If different dimensions, outputs garbage.
           
          setdatafolder root:SampleDBEmassdata
          DFREF dfWorkDir=root:        
            WAVE/SDFR=dfWorkdir modeA,modeB,responsefactoravg,responsefactorstdv
            WAVE/SDFR=dfWorkdir Cnumwave,DBE,responsefactoravg,DBEmatrix
            WAVE/SDFR=dfWorkdir Totalcarbongroups
            WAVE/SDFR=dfWorkdir CHchecked,CHOchecked,CHSchecked
            WAVE/SDFR=dfWorkdir subDBEstart,subDBEend,DBEexclude,graphordinate
           WAVE/SDFR=dfWorkdir subDBEstart2,subDBEend2,DBEexclude2
           
            killwaves /Z CnumDBEmass2D
            make/n=(numpnts(Cnumwave),DBE[0]+1)CnumDBEmass2D
            CnumDBEmass2D=responsefactoravg*DBEmatrix
            variable i,j
            for(i=0;i<=numpnts(Cnumwave);i+=1)
            for(j=0;j<=DBE[0]+1;j+=1)
            if(numtype(CnumDBEmass2D[i][j])==2)
            CnumDBEmass2D[i][j]=0
            endif
            if(CnumDBEmass2D[i][j]<0)
            CnumDBEmass2D[i][j]=0
            endif
            endfor
            endfor 
   
    //-------------------------

    if(DBEexclude[0]==1)
    for(i=0;i<Totalcarbongroups[0];i+=1)
    for(j=subDBEstart[0];j<subDBEend[0]+1;j+=1)
    CnumDBEmass2D[i][j]=0
    endfor
    endfor
    endif
    if(DBEexclude2[0]==1)
    for(i=0;i<Totalcarbongroups[0];i+=1)
    for(j=subDBEstart2[0];j<subDBEend2[0]+1;j+=1)
    CnumDBEmass2D[i][j]=0
    endfor
    endfor
    endif
    //-------------------------    
           
variable k
string basename = "CnumDBEmass"
string xName3
for (i=0;i<=DBE[0];i+=1)
        sprintf xName3, "%sX%d", baseName, i
        killwaves /Z $xName3
        endfor
for (i=0;i<=DBE[0];i+=1)
        sprintf xName3, "%sX%d", baseName, i
        Make/o/N= (numpnts(Cnumwave)) $xName3
        Duplicate/O/RMD=[][i] CnumDBEmass2D,$xName3
        Wave xWave3 = $xName3
endfor
//wave graphordinate

string datainfo="CnumDBEmassX"
string xname4
for(i=0;i<=DBE[0];i+=1)
sprintf xname4,"%s%d",datainfo,i
wave xWave4=$xName4
matrixop/o xWave4=replace(xWave4,Nan,0)
endfor

//---------------
killwaves/Z totalmassperCnum
make/n=(Totalcarbongroups[0],1) totalmassperCnum
Matrixop/o totalmassperCnum=sumrows(CnumDBEmass2D)
variable totalmass=sum(totalmassperCnum)
//variable IVOC=totalmassperCnum[4]+totalmassperCnum[5]+totalmassperCnum[6]+totalmassperCnum[7]+totalmassperCnum[8]

if(modeA[0]==1&&modeB[0]==0)

controlinfo samplebutton
Strswitch(S_value)

case "CH":

variable IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin1[0]),pnt2x(totalmassperCnum,bin2[0]))   //The numerals are row numbers which intend to represent carbon boundaries
variable SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin17[0]),pnt2x(totalmassperCnum,bin18[0]))
variable IVOCprcnt=(IVOC/totalmass)*100
variable SVOCprcnt=(SVOC/totalmass)*100
break

case "CHO":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin3[0]),pnt2x(totalmassperCnum,bin4[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin19[0]),pnt2x(totalmassperCnum,bin20[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHS":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin9[0]),pnt2x(totalmassperCnum,bin10[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin25[0]),pnt2x(totalmassperCnum,bin26[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break


case "CHO2":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin5[0]),pnt2x(totalmassperCnum,bin6[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin21[0]),pnt2x(totalmassperCnum,bin22[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break


case "CHO3":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin7[0]),pnt2x(totalmassperCnum,bin8[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin23[0]),pnt2x(totalmassperCnum,bin24[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break


case "CHOS":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin11[0]),pnt2x(totalmassperCnum,bin12[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin27[0]),pnt2x(totalmassperCnum,bin28[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHN":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin13[0]),pnt2x(totalmassperCnum,bin14[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin29[0]),pnt2x(totalmassperCnum,bin30[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break


case "CHON":

IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin15[0]),pnt2x(totalmassperCnum,bin16[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin31[0]),pnt2x(totalmassperCnum,bin32[0]))
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

endswitch

endif


if(modeA[0]==0&&modeB[0]==1)

controlinfo samplebutton
Strswitch(S_value)

case "CH":

variable VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin33[0]),pnt2x(totalmassperCnum,bin34[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin35[0]),pnt2x(totalmassperCnum,bin36[0]))
 SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin37[0]),pnt2x(totalmassperCnum,bin38[0]))
variable VOCprcnt=(VOC/totalmass)*100
 IVOCprcnt=(IVOC/totalmass)*100
 SVOCprcnt=(SVOC/totalmass)*100
break

case "CHO":


VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin39[0]),pnt2x(totalmassperCnum,bin40[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin41[0]),pnt2x(totalmassperCnum,bin42[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin43[0]),pnt2x(totalmassperCnum,bin44[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHS":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin57[0]),pnt2x(totalmassperCnum,bin58[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin59[0]),pnt2x(totalmassperCnum,bin60[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin61[0]),pnt2x(totalmassperCnum,bin62[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break


case "CHOS":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin63[0]),pnt2x(totalmassperCnum,bin64[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin65[0]),pnt2x(totalmassperCnum,bin66[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin67[0]),pnt2x(totalmassperCnum,bin68[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHO2":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin45[0]),pnt2x(totalmassperCnum,bin46[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin47[0]),pnt2x(totalmassperCnum,bin48[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin49[0]),pnt2x(totalmassperCnum,bin50[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHO3":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin51[0]),pnt2x(totalmassperCnum,bin52[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin53[0]),pnt2x(totalmassperCnum,bin54[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin55[0]),pnt2x(totalmassperCnum,bin56[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHN":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin69[0]),pnt2x(totalmassperCnum,bin70[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin71[0]),pnt2x(totalmassperCnum,bin72[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin73[0]),pnt2x(totalmassperCnum,bin74[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

case "CHON":

VOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin75[0]),pnt2x(totalmassperCnum,bin76[0]))
IVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin77[0]),pnt2x(totalmassperCnum,bin78[0]))
SVOC=sum(totalmassperCnum,pnt2x(totalmassperCnum,bin79[0]),pnt2x(totalmassperCnum,bin80[0]))
VOCprcnt=(VOC/totalmass)*100
IVOCprcnt=(IVOC/totalmass)*100
SVOCprcnt=(SVOC/totalmass)*100
break

endswitch

endif





//wave Cnumwave,DBE,responsefactorstdv,DBEmatrix
killwaves /Z CnumDBEmass2Dstdv,totalmassperCnumstdv
make/n=(numpnts(Cnumwave),DBE[0]+1)CnumDBEmass2Dstdv
make/n=(Totalcarbongroups[0],1) totalmassperCnumstdv
CnumDBEmass2Dstdv=responsefactorstdv*DBEmatrix
for(i=0;i<=numpnts(Cnumwave);i+=1)
            for(j=0;j<=DBE[0]+1;j+=1)
            if(numtype(CnumDBEmass2Dstdv[i][j])==2)
            CnumDBEmass2Dstdv[i][j]=0
            endif
            endfor
            endfor 
           
            //-------------------------

    if(DBEexclude[0]==1)
    for(i=0;i<Totalcarbongroups[0];i+=1)
    for(j=subDBEstart[0];j<subDBEend[0]+1;j+=1)
    CnumDBEmass2Dstdv[i][j]=0
    endfor
    endfor
    endif
    if(DBEexclude2[0]==1)
    for(i=0;i<Totalcarbongroups[0];i+=1)
    for(j=subDBEstart2[0];j<subDBEend2[0]+1;j+=1)
    CnumDBEmass2Dstdv[i][j]=0
    endfor
    endfor
    endif
    //-------------------------        
           
Matrixop/o totalmassperCnumstdv=sumrows(CnumDBEmass2Dstdv)
variable totalmassstdv=sum(totalmassperCnumstdv)
//variable IVOCstdv=totalmassperCnumstdv[4]+totalmassperCnumstdv[5]+totalmassperCnumstdv[6]+totalmassperCnumstdv[7]+totalmassperCnum[8]

if(modeA[0]==1&&modeB[0]==0)

controlinfo samplebutton
Strswitch(S_value)

case "CH":
variable IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin1[0]),pnt2x(totalmassperCnumstdv,bin2[0]))
variable SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin17[0]),pnt2x(totalmassperCnumstdv,bin18[0]))
variable IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
variable SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHO":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin3[0]),pnt2x(totalmassperCnumstdv,bin4[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin19[0]),pnt2x(totalmassperCnumstdv,bin20[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHS":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin9[0]),pnt2x(totalmassperCnumstdv,bin10[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin25[0]),pnt2x(totalmassperCnumstdv,bin26[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break


case "CHO2":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin5[0]),pnt2x(totalmassperCnumstdv,bin6[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin21[0]),pnt2x(totalmassperCnumstdv,bin22[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHO3":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin7[0]),pnt2x(totalmassperCnumstdv,bin8[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin23[0]),pnt2x(totalmassperCnumstdv,bin24[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHOS":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin11[0]),pnt2x(totalmassperCnumstdv,bin12[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin27[0]),pnt2x(totalmassperCnumstdv,bin28[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHN":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin13[0]),pnt2x(totalmassperCnumstdv,bin14[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin29[0]),pnt2x(totalmassperCnumstdv,bin30[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHON":

IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin15[0]),pnt2x(totalmassperCnumstdv,bin16[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin31[0]),pnt2x(totalmassperCnumstdv,bin32[0]))
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

endswitch
endif


if(modeA[0]==0&&modeB[0]==1)

controlinfo samplebutton
Strswitch(S_value)

case "CH":

variable VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin33[0]),pnt2x(totalmassperCnumstdv,bin34[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin35[0]),pnt2x(totalmassperCnumstdv,bin36[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin37[0]),pnt2x(totalmassperCnumstdv,bin38[0]))
variable VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHO":
VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin39[0]),pnt2x(totalmassperCnumstdv,bin40[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin41[0]),pnt2x(totalmassperCnumstdv,bin42[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin43[0]),pnt2x(totalmassperCnumstdv,bin44[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHS":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin57[0]),pnt2x(totalmassperCnumstdv,bin58[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin59[0]),pnt2x(totalmassperCnumstdv,bin60[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin61[0]),pnt2x(totalmassperCnumstdv,bin62[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHOS":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin63[0]),pnt2x(totalmassperCnumstdv,bin64[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin65[0]),pnt2x(totalmassperCnumstdv,bin66[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin67[0]),pnt2x(totalmassperCnumstdv,bin68[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHO2":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin45[0]),pnt2x(totalmassperCnumstdv,bin46[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin47[0]),pnt2x(totalmassperCnumstdv,bin48[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin49[0]),pnt2x(totalmassperCnumstdv,bin50[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHO3":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin51[0]),pnt2x(totalmassperCnumstdv,bin52[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin53[0]),pnt2x(totalmassperCnumstdv,bin54[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin55[0]),pnt2x(totalmassperCnumstdv,bin56[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHN":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin69[0]),pnt2x(totalmassperCnumstdv,bin70[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin71[0]),pnt2x(totalmassperCnumstdv,bin72[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin73[0]),pnt2x(totalmassperCnumstdv,bin74[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

case "CHON":

VOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin75[0]),pnt2x(totalmassperCnumstdv,bin76[0]))
IVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin77[0]),pnt2x(totalmassperCnumstdv,bin78[0]))
SVOCstdv=sum(totalmassperCnumstdv,pnt2x(totalmassperCnumstdv,bin79[0]),pnt2x(totalmassperCnumstdv,bin80[0]))
VOCstdvprcnt=((VOC/totalmass)*sqrt((VOCstdv/VOC)^2+(totalmassstdv/totalmass)^2))*100
IVOCstdvprcnt=((IVOC/totalmass)*sqrt((IVOCstdv/IVOC)^2+(totalmassstdv/totalmass)^2))*100
SVOCstdvprcnt=((SVOC/totalmass)*sqrt((SVOCstdv/SVOC)^2+(totalmassstdv/totalmass)^2))*100
break

endswitch

endif


killwaves/Z totalarom,totalaromstdv
make/n=1 totalarom
make/n=1 totalaromstdv
Matrixop/o totalarom=sum(subRange(CnumDBEmass2D,0,Totalcarbongroups[0]-1,4,18))
Matrixop/o totalaromstdv=sum(subRange(CnumDBEmass2Dstdv,0,Totalcarbongroups[0]-1,4,18))
variable totalaromprcnt=(totalarom[0]/totalmass)*100
variable totalaromstdvprcnt= ((totalarom[0]/totalmass)*sqrt((totalaromstdv[0]/totalarom[0])^2+(totalmassstdv/totalmass)^2))*100

//---------------------

variable scb=(sum(CnumDBEmassX0)) //straight-chain and branched
variable nac=(sum(CnumDBEmassX1)+sum(CnumDBEmassX2)+sum(CnumDBEmassX3))  //nonaromatic cyclic
variable sra=(sum(CnumDBEmassX4)+sum(CnumDBEmassX5)+sum(CnumDBEmassX6))  //single-ring aromatics
variable pah=(sum(CnumDBEmassX7)+sum(CnumDBEmassX8)+sum(CnumDBEmassX9)+sum(CnumDBEmassX10)+sum(CnumDBEmassX11)+sum(CnumDBEmassX12)+sum(CnumDBEmassX13)+sum(CnumDBEmassX14)+sum(CnumDBEmassX15)+sum(CnumDBEmassX16)+sum(CnumDBEmassX17)+sum(CnumDBEmassX18))  //polycyclic aromatic hydrocarbons
variable scbp=((scb/(scb+nac+sra+pah))*100)
variable nacp=((nac/(scb+nac+sra+pah))*100)
variable srap=((sra/(scb+nac+sra+pah))*100)
variable pahp=((pah/(scb+nac+sra+pah))*100)
variable total=scb+nac+sra+pah

killwaves/Z srastdv
make/n=1 srastdv
MatrixOp/o srastdv=sum(subRange(CnumDBEmass2Dstdv,0,Totalcarbongroups[0]-1,4,7))
variable srastdvprcnt=((sra/total)*sqrt((srastdv[0]/sra)^2+(totalmassstdv/totalmass)^2))*100



setdatafolder root:
wave Cnumlabels
DFREF dfWorkDir=root:SampleDBEmassdata
WAVE/SDFR=dfWorkdir CnumDBEmassX0,CnumDBEmassX1,CnumDBEmassX2,CnumDBEmassX3,CnumDBEmassX4,CnumDBEmassX5
WAVE/SDFR=dfWorkdir CnumDBEmassX6,CnumDBEmassX7,CnumDBEmassX8,CnumDBEmassX9,CnumDBEmassX10,CnumDBEmassX11
WAVE/SDFR=dfWorkdir CnumDBEmassX12,CnumDBEmassX13,CnumDBEmassX14,CnumDBEmassX15,CnumDBEmassX16,CnumDBEmassX17,CnumDBEmassX18
Dowindow/F CnumDBEmassplot
killwindow /Z CnumDBEmassplot
 string cnumDBEplot="CnumDBEmassplot"
 DoWindow/C $cnumDBEplot
PauseUpdate; Silent 1       // building window...
    Display/n=$cnumDBEplot /W=(300.6,59,786.6,502.4) CnumDBEmassX18,CnumDBEmassX17,CnumDBEmassX16,CnumDBEmassX15,CnumDBEmassX14,CnumDBEmassX13 vs Cnumlabels
    AppendToGraph CnumDBEmassX12,CnumDBEmassX11,CnumDBEmassX10,CnumDBEmassX9,CnumDBEmassX8,CnumDBEmassX7,CnumDBEmassX6,CnumDBEmassX5 vs Cnumlabels
    AppendToGraph CnumDBEmassX4,CnumDBEmassX3,CnumDBEmassX2,CnumDBEmassX1,CnumDBEmassX0 vs Cnumlabels
    ModifyGraph rgb(CnumDBEmassX18)=(48059,48059,48059),rgb(CnumDBEmassX17)=(65535,0,52428),rgb(CnumDBEmassX16)=(65535,43690,0)
    ModifyGraph rgb(CnumDBEmassX15)=(30583,30583,30583),rgb(CnumDBEmassX14)=(65535,49151,49151),rgb(CnumDBEmassX13)=(21845,21845,21845)
    ModifyGraph rgb(CnumDBEmassX12)=(26214,17479,0),rgb(CnumDBEmassX11)=(4369,4369,4369),rgb(CnumDBEmassX10)=(0,26214,13293)
    ModifyGraph rgb(CnumDBEmassX9)=(65535,65535,0),rgb(CnumDBEmassX8)=(26214,26212,0),rgb(CnumDBEmassX7)=(3,52428,1)
    ModifyGraph rgb(CnumDBEmassX6)=(6558,13107,0),rgb(CnumDBEmassX5)=(1,52428,52428),rgb(CnumDBEmassX4)=(0,17409,26214)
    ModifyGraph rgb(CnumDBEmassX3)=(39321,1,31457),rgb(CnumDBEmassX2)=(26411,1,52428),rgb(CnumDBEmassX1)=(1,4,52428)
    ModifyGraph hbFill=2
    ModifyGraph toMode=3
    ModifyGraph fSize=14
    ModifyGraph lblMargin(bottom)=6
    ModifyGraph axOffset(left)=-2.3,axOffset(bottom)=-1.66667
    ModifyGraph lblLatPos(left)=223,lblLatPos(bottom)=-18
    ModifyGraph prescaleExp(left)=0
    Label left "."
    SetAxis/A //left 0,graphordinate[0]
    TextBox/C/N=text0/F=0/B=1/A=MC/X=3.12/Y=43.74 "Compound type: mass observed, % of graph total"
    AppendText "Straight-chain and branched compounds: "+num2str(scb)+",  "+num2str(scbp)+"%"
    AppendText "Non-aromatic cyclic compounds: "+num2str(nac)+",  "+num2str(nacp)+"%"
    AppendText "Single-ring aromatic compounds: "+num2str(sra)+",  "+num2str(srap)+"%"+" | "+num2str(srastdv[0])+", "+num2str(srastdvprcnt)+"%"
    AppendText "Polycyclic aromatic hydrocarbons: "+num2str(pah)+",  "+num2str(pahp)+"%"
    AppendText "Total aromatics: "+num2str(totalarom[0])+", "+num2str(totalaromprcnt)+"%"+" | "+num2str(totalaromstdv[0])+", "+num2str(totalaromstdvprcnt)+"%"
    if(modeA[0]==0&&modeB[0]==1)
    AppendText "VOCs: "+num2str(VOC)+", "+num2str(VOCprcnt)+"%"+" | "+num2str(VOCstdv)+", "+num2str (VOCstdvprcnt)+"%"
    endif
    AppendText "IVOCs: "+num2str(IVOC)+", "+num2str(IVOCprcnt)+"%"+" | "+num2str(IVOCstdv)+", "+num2str(IVOCstdvprcnt)+"%"
    AppendText "SVOCs: "+num2str(SVOC)+", "+num2str(SVOCprcnt)+"%"+" | "+num2str(SVOCstdv)+", "+num2str(SVOCstdvprcnt)+"%"
    AppendText "Graph total: "+num2str(total)+", stdv: "+num2str(totalmassstdv)
   Legend/C/N=text3/J/F=0/A=MC/X=46.40/Y=12.61 "\\s(CnumDBEmassX18) DBE 18\r\\s(CnumDBEmassX17) DBE 17\r\\s(CnumDBEmassX16) DBE 16\r\\s(CnumDBEmassX15) DBE 15\r\\s(CnumDBEmassX14) DBE 14"
    AppendText "\\s(CnumDBEmassX13) DBE 13\r\\s(CnumDBEmassX12) DBE 12\r\\s(CnumDBEmassX11) DBE 11\r\\s(CnumDBEmassX10) DBE 10\r\\s(CnumDBEmassX9) DBE 9\r\\s(CnumDBEmassX8) DBE 8\r\\s(CnumDBEmassX7) DBE 7"
    AppendText "\\s(CnumDBEmassX6) DBE 6\r\\s(CnumDBEmassX5) DBE 5\r\\s(CnumDBEmassX4) DBE 4\r\\s(CnumDBEmassX3) DBE 3\r\\s(CnumDBEmassX2) DBE 2\r\\s(CnumDBEmassX1) DBE 1\r\\s(CnumDBEmassX0) DBE 0"

String/G Updateval="\\K(2,39321,1)The mass distribution graph is plotted! The dimensions of the response factor and peak area matrices match!"

endif

if(dimsize(responsefactoravg,0)!=dimsize(DBEmatrix,0))
String/G Updateval="\\K(65535,0,0)The dimensions of the response factor and peak area matrices do not match! Cannot proceed!"
endif
            break
        case -1: // control being killed
            break
    endswitch
//setdatafolder root:
    return 0
   
End

 

My first strong recommendation is this ...

* Be religious about using /W=windowname and, win=windowname, ... syntax to define where things are to be found that you want to read (e.g. ControlInfo) or change (e.g. AppendText or Button or SetVariable ...). 

My second suggestion is this ...

* Move all code that is in your button call to a different function (e.g. run_mybuttonprocedure()).

* Call that function as the only thing that is done from the button action (e.g. on click --> run_mybuttonprocedure())

* Put a RETURN 0 into the top portion of your run_mybuttonprocedure(). Move that return statement along until the program crashes.

* Turn on the debugger and set a break point at the previous location of the return statement before the crash. Step through and watch what is happening.

My final thought is this ...

* Find ways to break your (rather long and hard to parse) code into manageable function calls that can each be debugged and certified in their own right.

Some further tips to massively cut down your code and make it more readable (this also helps to find errors easily):

* If possible don't use SetDataFolder at all

* Move redundant code outside of loops and switches

* I get the feeling that leaning about the SDFR flag before learning the 'boring' way of working with folders might lead to some bad habits here. You can work with folders like this:

Wave internalname=root:foldername:realwavename

Instead of referencing all waves directly (which might change) and writing each name into the code, you should do this (prefeably inside the switch and then move ALL calculations outside):

Wave work1 = root:Volatilitybins:bin1

i.e., reassign work1 etc. to the correct wave and then just use work1, work2 ... inside your calculation code.

* don't use for loops for simple wave assignments such as setting some parts of the wave to zero. These things can almost always be written as one-liners with a condition statement like:

mywave = condition ? true : false

example:

mywave=numtype(mywave[p][q])==2 ? 0 : mywave[p][q]

... or directly use MatrixOp which is way more efficient.

 

Hope that helps. 

I would avoid a switch with only one effective case. Consider:

Function ButtonProc_54(ba) : ButtonControl
    STRUCT WMButtonAction &ba

    if(ba.eventCode != 2)
       return 0
    endif


    // click code here

    return 0
end

Or, if you want to retain the switch for future expansion to react to other events:

Function ButtonProc_54(ba) : ButtonControl
    STRUCT WMButtonAction &ba

    switch( ba.eventCode )
        case 2: // mouse up
            AnotherFunction()
            break
        case -1: // control being killed
            break
    endswitch
    return 0
   
End

This will remove a level of indentation from your function and make it easier to read.

[this was already recommended by jjweimer]

for human readability, move variable definitions outside of switch cases.

Igor lets you get away with this, but it makes code harder to parse by eye:

    switch(n)
        case 1:
            variable foo=1
            break
        case 2:
            foo=2
            break
    endswitch

 

Hi All, 

Thank you so much for the great advice, tips, and suggestions! I deeply appreciate it and will be sure to implement them in my codes. I also agree that I could probably organize this better by pulling out the processing components into a separate function and call it in to the mother program.

Quick question: What is the significance of the "STRUCT" syntax in a button? I ask because I notice that if I remove the STRUCT syntax, and replace it with a string statement (e.g. string ba), the code still runs alright. But obviously, I'd be missing out on some privileges that STRUCT offers. 

Many Thanks and Regards, 

Peeyush

There is a legacy form of button control procedure that looks like this:

Function procName (ctrlName) : ButtonControl
    String ctrlName
    ...
    return 0
End

A button control function that has a string as a parameter is passed the control name in the string.

New code should use a structure, even if you don't need the extra information that the structure contains.

Note that the structure control procedure is called for all events related to the button, including mouse movements. Be sure to react only to the events you care about, typically mouseup (a click).