Accessing a wave from other function which is created inside another function

I had defined  a function which creates a wave and then i want to access the wave from another function so that i can compute. I read about wave references but I think it does not satisfy the purpose.

here is the dummy code:

Function createdata()
Make /O/T Datawave={{"x1","x2","x3","x4"},{"1","2","3","4"}}
End
Function LetUsFindAndCompute()
String var
Variable i
for(i=1;i<=4;i+=1)
var="x"+num2str(i)
//search the element in wave and store the corresponding element and compute to your desires.
endfor
end

 

It seems to me wave references do seem to fit your purpose, at least in your example:

Function createdata()
   Make /O/T Datawave={{"x1","x2","x3","x4"},{"1","2","3","4"}}
End

Function TestReferencing()
   Wave/T internalreftowave = Datawave // assuming Datawave exists in the current folder
   internalreftowave[0][0] = "test" // write something into the wave as a test
end

Is there something specific which does not work as intended?

well i was passing the wave as parameter to the second function while the first function returned a wave and then tried to duplicate the parameter into a wave which obviously did not work out and threw the error as point return or something i will give the specifics about the error.

the code for the first function was like this

Function /Wave createdata()
Make /O/T Adata={{"1","2","3","4"},{"1","2","3","4"}}
return Adata
End

 

In reply to by coltblaze

coltblaze wrote:
well i was passing the wave as parameter to the second function while the first function returned a wave and then tried to duplicate the parameter into a wave

The code that you showed in your original post did not show passing a wave as a parameter.

To get the best answer, it is best to provide a minimal, self-contained example that others can inspect and, if necessary, execute and debug.

Here is an example of a minimal, self-contained example that shows how to pass a text wave from a routine to a subroutine:

Function Test()
    Make /O/T DataWave={{"x1","x2","x3","x4"},{"1","2","3","4"}}
    Variable result = Subroutine(DataWave)
    Print result
End

Function Subroutine(tw)
    Wave/T tw
   
    FindValue /TEXT="x3" /Z tw
    Variable result = V_value
    return result
End

 

In reply to by chozo

can we not pass the wave as string reference lets say:

I have a list of waves and i want to create a wave reference in the loop and do some operations.

Function ToDoSomething()
string x,filename
variable i,n
x=wavelist("*",";","")
Make/O/N=(ItemsinList(x)) recordofwaves
recordofwaves= stringfromlist(p,x)
n= numpnts(recordofwaves)
Wave Waveref
 for(i=0;i<n;i=i+1)
 filename=x[i]
 Waveref=$filename //something like this as a wave reference
 //doing some operations and then kudos  
Endfor
End

so this way it is giving error in Waveref=$filename

In reply to by coltblaze

Untested, but try this:

Function ToDoSomething()
string x,filename
variable i,n
x=wavelist("*",";","")
Make/O/T/N=(ItemsinList(x)) recordofwaves
recordofwaves= stringfromlist(p,x)
n= numpnts(recordofwaves)
 for(i=0;i<n;i=i+1)
 filename=recordofwaves[i]
 Wave Waveref=$filename //something like this as a wave reference
 //doing some operations and then kudos  
Endfor
End

Note: The wave reference is inside the for-loop, so it is 'refreshed' each cycle. Also, I have added the /T flag to the recordofwaves wave as this is a text wave.

EDIT - changed line in code above from

filename=x[i]

to 

filename=recordofwaves[i]

Hope this helps,

well it is giving the following error :"The wave reference to the Waveref failed "

If you want to store multiple wave references in a wave then you can use a 'wave reference wave', i.e., waves with references to other waves as entries. Then you can skip all this mess with strings. Such waves are generated with

Make/O/WAVE/N=(numofrefs) mywaverefs

Read more by executing...

DisplayHelpTopic "Wave Reference Waves"

Then you could just do

for(i=0;i<n;i+=1)
   Wave currentwave = mywaverefs[i]
   //doing some operations
Endfor

To fill the indexes with references to all waves in a certain folder you could use something like WaveRefIndexedDFR in a loop. But if you just want to reference all waves just once you could use WaveRefIndexedDFR directly in above for-loop and skip even the stuff with the wave reference wave. Depends on what you want to do exactly (you would have to tell us).

As a side note, I would recommend you refrain from using Igor-own commands as wave names, such as 'x' in this example. Makes debugging much more difficult.

In reply to by coltblaze

So, you have not supplied a self-contained example reproducing the error.

Here is a cleaned up version that makes the data and illustrates it a working function:

Function DoStuff()
    Make wave0, wave1, wave2
    ToDoSomething()
End

Function ToDoSomething()
    string myWaveList,MyWavename
    variable i,n
    MyWaveList = wavelist("*",";","")
    n = ItemsinList(MyWaveList)
    for(i = 0 ; i < n ; i += 1 )
        MyWavename = stringfromlist(i, MyWaveList)
        Wave Waveref=$MyWavename
        printf "Index: %d  WaveName: %s\r", i, NameOfWave(Waveref)
    endfor
End

I suspect you are getting the error because you are not managing DataFolders - the waves are in the current datafolder but this is not necessarily the one containing the waves.