Programmatically creating waves

I am brand new at writing macros (in Igor), so I'm trying to figure stuff out as I go.
One piece of code that I am having trouble with is programmatically creating waves.
In my procedure, heres what I have so far:

Make/N=200 'WaveA','WaveB','WaveC'..... etc
Prompt xWave, "Enter wave for x values : "
Prompt yWave, "Enter wave for y values: "
DOPrompt "Enter x and y waves", xWave, yWave

Is this correct for picking out two of the created waves, so later in the macro I can graph the two?
Also, is it possible to provide a list of the created waves within the prompt so the user can just pick 2 out?

Thanks,
Cory
For more information on how to use Prompt and DoPrompt, including an example of how to create a popup menu that you can use to select a wave as you want, execute the following command at the Igor command line:
DisplayHelpTopic "The Simple Input Dialog"


Here are two ways to do it:

Function Test1()    // Enter wave name through edit box
    Make /O /N=200 WaveA, WaveB, WaveC
   
    String xName, yName
    Prompt xName, "Enter wave for x values : "
    Prompt yName, "Enter wave for y values: "
    DoPrompt "Enter x and y waves", xName, yName
   
    if (V_flag == 0)
        Wave xWave = $xName
        Wave yWave = $yName
        Display yWave vs xWave
    endif
End

Function Test2()    // Choose wave name from popup menu
    Make /O /N=200 WaveA, WaveB, WaveC
   
    String xName, yName
    Prompt xName, "Enter wave for x values : ", popup WaveList("*", ";", "")
    Prompt yName, "Enter wave for y values: ", popup WaveList("*", ";", "")
    DoPrompt "Enter x and y waves", xName, yName
   
    if (V_flag == 0)
        Wave xWave = $xName
        Wave yWave = $yName
        Display yWave vs xWave
    endif
End


For an explanation of the use of $, execute this:

DisplayHelpTopic "Converting a String into a Reference Using $"

As part of coming up the learning curve in programming, read manual chapters IV-1, IV-2 and IV-3.

Now would I have to put quotations around names that have spaces (literals)?

Eg.
Make /O /N=200 'Wave A (units)', 'Wave B (units)'
or is that not necessary
I figured out the last question I just asked, but now I have a new one (hopefully the last one)

How can I prompt a user to enter a file name?
I guess they could just type it in, but chances are, most users don't know the full pathname.
Is there a way to make a prompt with a 'Browse' button?

I would like to read each column of the file and programmatically assign the wave names.
Cory K wrote:
I figured out the last question I just asked, but now I have a new one (hopefully the last one)

How can I prompt a user to enter a file name?
I guess they could just type it in, but chances are, most users don't know the full pathname.
Is there a way to make a prompt with a 'Browse' button?

I would like to read each column of the file and programmatically assign the wave names.


Use the Open operation with the /D and /R flags. This presents a standard File open dialog and then your function that calls Open can get the full path of the file the user selected by inspecting the S_fileName string that the Open operation creates. The examples at the bottom of the command help for the Open operation should help you figure out exactly how to use it.
Oh, and by the way, if you use Open that has to be separate from the prompt dialog that DoPrompt creates. DoPrompt is very limited in the kind of prompts it supports. As you said, you could ask the user to type in a string which is the path to a file, but that won't make your users very happy :)

Alternately, you can create a panel that contains all of the prompts on it you want, including a "Browse" button which would call the Open operation. Making your own panel gives you many more options for interacting with the user, but is much more difficult to program. Your best bet might be to call the Open operation either before or after you call DoPrompt.
This is what I have for opening and reading the file.
For some reason, either nothing is reading or I am not correctly using the data once it is read.
Can someone see where there is a problem or what I should add?

variable refNum
String xName ="", yName="", pathName="", fileName="", str=""
Open D/R =2/P =$pathName refNum as fileName
Variable err = V_flag
String fullPath = S_fileName
Prompt xName, "Enter wave for x values : ", popup WaveList("*", ";", "")
Prompt yName, "Enter wave for y values : ", popup WaveList("*", ";", "")
DoPrompt "Enter x and y waves", xName, yName
Wave xWave = $xName
Wave yWave = $yName
Display yWave vs xWave
Cory K wrote:
This is what I have for opening and reading the file.
For some reason, either nothing is reading or I am not correctly using the data once it is read.
Can someone see where there is a problem or what I should add?

variable refNum
String xName ="", yName="", pathName="", fileName="", str=""
Open D/R =2/P =$pathName refNum as fileName
Variable err = V_flag
String fullPath = S_fileName
Prompt xName, "Enter wave for x values : ", popup WaveList("*", ";", "")
Prompt yName, "Enter wave for y values : ", popup WaveList("*", ";", "")
DoPrompt "Enter x and y waves", xName, yName
Wave xWave = $xName
Wave yWave = $yName
Display yWave vs xWave

You are missing a slash before your D flag in the call to the Open operation. If that doesn't work, then I'd add a "print S_fileName" line after you call Open, to see if that string is getting set properly.

aclight wrote:


You are missing a slash before your D flag in the call to the Open operation. If that doesn't work, then I'd add a "print S_fileName" line after you call Open, to see if that string is getting set properly.



I added the slash before the D flag, and get an error.
When I go to compile the procedure, it highlights the 'R' and says:
"Ill-formed name"

This is what I typed :
Open /D /R =2/P =$pathName refNum as fileName

is that the correct syntax?
Looking at what I typed, it looks like I am trying to assign a value to a variable named 'Open', instead of calling a function 'Open'.
Is that why Igor is getting confused. Maybe I should be calling the function in a different way.
Cory K wrote:
aclight wrote:


You are missing a slash before your D flag in the call to the Open operation. If that doesn't work, then I'd add a "print S_fileName" line after you call Open, to see if that string is getting set properly.



I added the slash before the D flag, and get an error.
When I go to compile the procedure, it highlights the 'R' and says:
"Ill-formed name"

This is what I typed :
Open /D /R =2/P =$pathName refNum as fileName

is that the correct syntax?


The /R flag doesn't take a value, so /R=2 is not valid. You probably want
Open /D /R /P=$pathName refNum as fileName
Cory K wrote:
Looking at what I typed, it looks like I am trying to assign a value to a variable named 'Open', instead of calling a function 'Open'.
Is that why Igor is getting confused. Maybe I should be calling the function in a different way.

Open is an operation, not a function, so doesn't directly return a value. To quote from Getting Started.ihf:

A built-in function is an Igor routine, such as sin, exp or ln, that directly returns a result. A built-in operation is a routine, such as Display, FFT or Integrate, that acts on an object and may create new objects but does not directly return a result.


In the procedure window, operations are colored blue-green, and functions are colored brown, assuming you haven overridden the default colors.

aclight wrote:


The /R flag doesn't take a value, so /R=2 is not valid. You probably want
Open /D /R /P=$pathName refNum as fileName


That did the trick. Thanks.
For some reason I keep getting an error saying:
Error in user function, attempt to use null string variable

I cant seem to find which one it is talking about. Is there a debugger in Igor that will show me where the error occured?
Sorry to keep bothering people, I'm still getting the hang of programming in Igor.

Once I have the file name, I would like to do the following:
- Read the file (tab delimited text file)
- create a new wave for each column, each wave having a predetermined name
...Then the rest I can handle from there.

Thanks
Cory K wrote:


That did the trick. Thanks.
For some reason I keep getting an error saying:
Error in user function, attempt to use null string variable

I cant seem to find which one it is talking about. Is there a debugger in Igor that will show me where the error occured?


My guess is that you are trying to use the S_fileName string in your function when it has not been defined, as is the case if the user clicks the cancel button in the Open file dialog box. I believe the command help for the Open operation gives an example that properly checks to make sure the cancel button hasn't been pressed.

Also, Igor does have a step debugger. From the command line, execute:
DisplayHelpTopic "Debugging"


This will give you the run down on how to use the debugger. You might also want to enable NVAR/SVAR/WAVE checking after you enable the debugger, as that will cause the debugger to be activated when your code attempts to use a null string variable.
Cory K wrote:
Sorry to keep bothering people, I'm still getting the hang of programming in Igor.

Once I have the file name, I would like to do the following:
- Read the file (tab delimited text file)
- create a new wave for each column, each wave having a predetermined name
...Then the rest I can handle from there.

Thanks


First of all, it's probably best if you create new threads for different questions, because otherwise the thread gets pretty confusing.

If you haven't already, I would suggest you take a look at the Igor manual for details on how to do what you want to do.

From the Igor command line, execute:
DisplayHelpTopic "Loading Delimited Text Files"


I suspect that your question will be answered in that section. Igor has a lot of built in capabilities for loading delimited text files.
aclight wrote:


My guess is that you are trying to use the S_fileName string in your function when it has not been defined, as is the case if the user clicks the cancel button in the Open file dialog box. I believe the command help for the Open operation gives an example that properly checks to make sure the cancel button hasn't been pressed.




I used the following from Igor's help files:
"Open /R/D/P = $pathName refNum as fileName"
where pathName and fileName are strings, and refNum is a variable

The error says that I am calling an empty variable during this line of code
After using print statements, I found that:
refNum = 0
but S_fileName is correctly displaying the selected file.

How can this be? I guess I am not understanding this part of the Open function:
"refNum as fileName" what exactly does that do
OK, well everything is settled now and the macro is running fine.
Thanks for your help
Cory K wrote:
Sorry to keep bothering people, I'm still getting the hang of programming in Igor.

Once I have the file name, I would like to do the following:
- Read the file (tab delimited text file)
- create a new wave for each column, each wave having a predetermined name
...Then the rest I can handle from there.

Thanks


Just a thought .... have you looked at the file loaders already posted here? Perhaps you are re-inventing the wheel. In particular, the tab delimited file loader found here or as a part of the core package here might be useful.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
From your original posted code:
variable refNum
String xName ="", yName="", pathName="", fileName="", str=""
Open D/R =2/P =$pathName refNum as fileName

Apart from the missing slash before the D, there is no real use for it if you actually want to read the file: just /R is readonly access.
/P=$pathname also does nothing as you have just set pathname to "" in the line above.
similarly the 'as filename' does nothing as you have also just set filename to "" above.

If you know the format of the input file, then LoadWave can do all of the loading of tab-separated files for you in one command, including putting up the dialog to find the file:

displayHelpTopic "Loadwave"

It's a very big help topic, but the operation can load a wide range of formats ,with subtle tweaks if needed.
LoadWave/J will load tab-delimited files, and LoadWave/J/L={...} where {...} are your preferred values for name lines etc. if known. This will save you being repeatedly asked for wave information that can be predicetd in advance. The are heaps of other tweaks and options to suit most delimited and general inputs.
Regards,
Patrick.