run periodically the code

Hello all,
I have this code for moving in three cases:
Window Panel0() : Panel
PauseUpdate; Silent 1
NewPanel /W=(150,50,450,250)
PopupMenu popup0,pos={107,72},size={67,20},proc=PopMenuProc
PopupMenu popup0,mode=2,popvalue="Image",value= #"\"Image;Binning;ROI;\""
EndMacro

Function PopMenuProc(pa) : PopupMenuControl
STRUCT WMPopupAction &pa

switch( pa.eventCode )
case 2: // mouse up
Variable popNum = pa.popNum
String popStr = pa.popStr
print "You selected item #"+num2str(popNum)
print "That item has the text \""+popStr+"\""
StrSwitch(popStr)
case "Binning":
imageinterpolate/pxsz={2,2} pixelate wave0
Display /N=new
AppendImage M_PixelatedImage
SetAxis/A/R left
ModifyImage M_PixelatedImage ctab= {*,*,Rainbow,0}
break;
case "Image":
print "Image"
break;
case "ROI":
Duplicate/O/R=[10,50][200,300] wave0,roi
Display /N=ROI
AppendImage roi
ModifyImage roi ctab= {*,*,Rainbow,0}
SetAxis/A/R left
break;
endswitch
break
endswitch

return 0
End


Now I want to run periodically this code, so I try to use Background Tasks but I'm not successful.For example I wrote the following code based on the help file:
Function ROIProc(ROIName)       // This is the function that will be called periodically
    STRUCT WMBackgroundStruct &ROIName

    Duplicate/O/R=[50,550][40,440] wave0,roi
Display /N=ROI
AppendImage roi
ModifyImage roi ctab= {*,*,Rainbow,0}
SetAxis/A/R left
   
    return 0    // Continue background task
End

Can anyone faced some such problem and/or has solution to it.
I thank you in advance for your help.
I am sorry if that sounds a bit harsh, but you really should work on the way you post here. You often have overly complicated functions but only post fragments of the code or minimum information on what is the problem or what you actually want to do. This just increases the investment for everyone and you have to wait longer for a solution. Please try to post the full code of a simple example of your problem and state detailed what is not working and what are the errors you get.

Now to the current question: First, did you manage to run a simple background task (e.g., printing something in the command history)? You should write a simple but working function and expand from there.
For your current example, you didn't post how you start the background task. Maybe the problem is in there. If you post the function which actually starts the task we can work on this.
By the way, you current background task contains a lot of code which you probably don't want to have executed periodically. Do you really want to duplicate something and create a graph like 10 times a second?
I suspect that you are trying to run commands such as Display in the background that ultimately will interrupt the background task because that command has to do something in the foreground.

I suggest that you think more carefully what you really want to do and perhaps find some other way to do it. As Chozo asks, do you _really_ want to have a graph automatically displayed every 1/60th of a second or so from a background task?

Otherwise, even if you really _need_ a background task, I still think you may be going over your head here trying to do it right away. Basically I wonder whether you are ready to do such effort for what is listed as Chapter IV-10 ADVANCED PROGRAMMING after only 37 posts. Even I have yet to program at that level on a regular basis. :-)

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
chozo wrote:
I am sorry if that sounds a bit harsh, but you really should work on the way you post here. You often have overly complicated functions but only post fragments of the code or minimum information on what is the problem or what you actually want to do. This just increases the investment for everyone and you have to wait longer for a solution. Please try to post the full code of a simple example of your problem and state detailed what is not working and what are the errors you get.

Thanks your answer,I really in bad situation because this program is complicated, so I want to explain from first. Maybe I can find better solution with your help.
The first thing in my code is the datas uploaded from LabVIEW with this code:
 DoWindow MyGraph
if(V_flag == 1)
  KillWindow MyGraph
endif
GBLoadWave/O/N=wave/T={16,80}/W=1 "D:DATA:temporalIgor.txt"
Redimension/N=(640,480) wave0      //resize Image
Duplicate wave0      //Duplicate the original image
Display /N=MyGraph      
AppendImage wave0
ModifyImage wave0 ctab= {*,*,Rainbow,0}
SetAxis/A/R left

Then I want add two another options like Binning and Region of interest(ROI) with this code:
//Binning
imageinterpolate/pxsz={2,2}  pixelate wave0
Display /N=new      
AppendImage M_PixelatedImage
SetAxis/A/R left
ModifyImage M_PixelatedImage ctab= {*,*,Rainbow,0}
//Region of interest
Duplicate/O/R=[10,50][200,300] wave0,roi
Display /N=ROI
AppendImage roi
ModifyImage roi ctab= {*,*,Rainbow,0}
SetAxis/A/R left

Now I want while the datas loding from Labview, it is possible to change in two Options(binnig and ROI). I decide using button or poupmenu to runnig between those options.
Quote:
I suspect that you are trying to run commands such as Display in the background that ultimately will interrupt the background task because that command has to do something in the foreground.

I suggest that you think more carefully what you really want to do and perhaps find some other way to do it.

I'm not sure to use background task but since they did not run in during the loding the program, the people suggest this.
Sorry my explanation in english is not good.
Please I need your urgent help on how to solve the problem.

saeed wrote:

I'm not sure to use background task but since they did not run in during the loding the program, the people suggest this.
Sorry my explanation in english is not good.
Please I need your urgent help on how to solve the problem.


OK. I think what you want is to have a function run when your program loads. Here is an example of how you do that ...

* Open a new experiment file
* Open the procedure window
* Put this code in the procedure window after the // pragma designation ...

SayHelloWorld()

Function SayHelloWorld()

      DoAlert/T="Hello World" 0, "Hello World!"
      return 0
end


* Save the experiment as helloworld
* Quit Igor Pro
* Open the helloworld experiment

You should see the Alert with Hello World!

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
After reading all your posts so far, I think I sort of get where you want to go, but there are some misconceptions about how things work in Igor or with programming in general.
1) I am pretty sure now that you don't need a background task. A background task is only for doing things very often and continuously (example: checking the state of some experimental device). Also, this is rather advanced and difficult to do right.
2) The data and a graphical representation of this data (graph, table) are separate, but connected things in Igor. In short: Modify the data => the graph updates automatically. You don't need to build a graph every time you want to change something. Create a graph once (Display ... etc.) and just work with the data from there on.
3) To change something in a meaningful way you need to think about how to pass the needed parameters to the function doing all this. Write a simple function which does what you want first. E.g.:
Function ChangeROI(mywave,startX,endX,startY,endY)
    Wave mywave
    Variable startX,endX,startY,endY
    Duplicate/O/R=(startX,endX )(startY,endY) mywave, roi
End

4) Before writing a user interface (buttons etc.), first write functions which work in the command line (see above example)! Starting with an user interface only complicates things as you see now, since both the problems from your functions plus the problems from you interface code pile up. I am pretty sure you can do all the things for now just using the command line and using the Data Browser. If you get to the point where everything works as expected and you want to automate things further, you may think about writing a panel.

Please consider reading chapter 'I' and at least a bit of chapter 'IV-3' of the manual. The time invested will be rewarded very quickly, as most things you are struggling with now will become clear then.

Ok then, from the code posted in this thread, I think you first want to display your data (your image) after loading it. You might want to consider making your code more flexible, since now everything is hard-coded into the function (e.g., the path to your data, the name of the wave). There are many ways to do that. I also recommend separating the loading process and the displaying process. As I wrote above, the data and the graph are two separate things. Here are some simple starting points:
Function LoadMyData(datapath)
    String datapath

    GBLoadWave/O/N/T={16,80} datapath
    wave wave0
    Redimension/N=(640,480) wave0      //cut Image
End

Now execute LoadMyData("D:DATA:temporalIgor.txt") in the command window to load your data.

Function DisplayData(mywave)
    wave mywave

    Display
    AppendImage mywave
    SetAxis/A/R left
End

Now execute DisplayData(wave0) in the command window to display the image. If you have renamed the wave at some point of course you need to write the appropriate name between the brackets.
These are very crude functions which don't offer many benefits from doing it manually. So I recommend to first streamline these two steps. Later we can think about how to modify the data using functions.
Thank you very much both!
chozo wrote:

3) To change something in a meaningful way you need to think about how to pass the needed parameters to the function doing all this. Write a simple function which does what you want first. E.g.:
Please consider reading chapter 'I' and at least a bit of chapter 'IV-3' of the manual. The time invested will be rewarded very quickly, as most things you are struggling with now will become clear then.
So I recommend to first streamline these two steps. Later we can think about how to modify the data using functions.

I already did your comments, so now with this code I can see my different graphs (binnig and ROI), I do not use function for each part, is it correct way? :
Function Parameters()

DoWindow mytable
if(V_flag == 1)
  KillWindow mytable //kill the window
endif
LoadWave/J/D/W/N=Atomparameters/K=0 "D:DATA:Parameters.txt"
Edit /N=mytable
AppendToTable Atomparameters0
End
//--------------------------------------------------------------------------------------------------------------------------------------------------------
//Upload Image  from LabVIEW
Function AjustaImagen()
DoWindow MyGraph //V_flag is set to 1 if window exists; 0 otherwise
if(V_flag == 1)
  KillWindow MyGraph//kill the window
endif
GBLoadWave/O/N=wave/T={16,80}/W=1 "D:DATA:temporalIgor.txt"
Redimension/N=(640,480) wave0      //resize Image
Duplicate wave0      //Duplicate the original image

DoWindow new
if(V_flag == 1)
  KillWindow new//kill the window
endif
         //imageinterpolate/pxsz={3,3}  pixelate wave0
    //imageinterpolate/pxsz={2,2}  pixelate wave0
    imageinterpolate/pxsz={1,1}  pixelate wave0
   
Display /N=new      
AppendImage M_PixelatedImage
SetAxis/A/R left
ModifyImage M_PixelatedImage ctab= {*,*,Rainbow,0}
DoWindow Graph
if(V_flag == 1)
  KillWindow Graph //kill the window
endif
end
function changeROI(mywave,startx,endx,starty,endy)
wave mywave
variable startx,endx,starty,endy
Make/N=1/D Atomparameters0
Duplicate/O/R=[Atomparameters0[5],Atomparameters0[7]][Atomparameters0[6],Atomparameters0[8]] wave0,ROI
Display /N=Graph
AppendImage ROI
ModifyImage ROI ctab= {*,*,Rainbow,0}
SetAxis/A/R left
End

But as I told I like to contorol the my program. In above code since I running my program from LabVIEW and send to Igor, the graphs are loaded datas in few moment until I stop program and it is good but there are some problem:
1)All of graphs and table appear in same position in the Igor window if it sounds funny problem for you but I really need to solve it and see those in separate position not overhand.
2)I want to turn off the part of program like ROI in during loading data and turn on back (it is main problem which I told before) like using some button.
3)You can see in my code I use this code 3 times :imageinterpolate/pxsz={1,1}  pixelate wave0 for having different number of pixelate but if we can control this number with popupmenu it looks better.
Best regards
I can solve the first problem (number 1) with easy solution. :)
But I am really need your help for rest.
Thanks
I have a very difficult time to read your code. I would have an easier time if you would learn to indent the code lines as in many of the examples in the manual. Also, leave line spaces between different functions.

I will second a suggestion made explicitly by Chozo. Create your functions one at a time. Test each one manually by calling them from the command line. Once you get that far, you can create one master command to run each command. For example ...

Function MyMainProgram()

          variable rtnvalue
          rtnvalue = GettheDatafromLabView()
          if (rtnvalue == 0)
              RuntheROIFunction()
          endif
          return 0
end

Function GetDatafromLabView()

         // ... do what is needed here
         // if successful, return 0   ... otherwise ... return -1
         return 0
end

Function RuntheROIFunction()
...
end


As far as using a button or popmenu to make settings, I suggest that you should do this development in a separate experiment file. Creating a panel with inputs can be time consuming and frustrating. Start from examples in the manual. Make changes to see what happens. Develop the example to make what you need.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
It seems to me that you are struggling with the workings of functions in general, as you are not using input parameters at all. I can only stress what I said earlier: At least read the first few pages of chapter 'IV' of the manual, especially the section 'Function Syntax'. You will only waste your time if you want to learn these things by asking questions in the forum.

But anyway, J. J. Weimer has already posted a suggestion for your problem 2. Problem 3 could be solved by using function parameters. But that depends a bit on how you work. Here's a suggestion:
function Pixelate(mywave, amount)
    wave mywave // input wave
    variable amount // input parameter
    imageinterpolate/pxsz={amount,amount}  pixelate mywave
end

Note how the wave and the parameter gets used in the function. Run Pixelate(wave0, 1), Pixelate(wave0, 2), and Pixelate(wave0, 3) after you have displayed M_Pixelate for the first time to see what is going on. That doesn't help much for now, but if you can use these kind of functions you can think about how to easily integrate that in your workflow.
jjweimer wrote:
I have a very difficult time to read your code. I would have an easier time if you would learn to indent the code lines as in many of the examples in the manual. Also, leave line spaces between different functions.

Getting unindented code from customers was the reason Larry added Edit->Adjust Indentation :)

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
chozo wrote:
Problem 3 could be solved by using function parameters. But that depends a bit on how you work. Here's a suggestion:
function Pixelate(mywave, amount)
    wave mywave // input wave
    variable amount // input parameter
    imageinterpolate/pxsz={amount,amount}  pixelate mywave
end


The problem is the Igor don't accept string like {amount,amount} instead of number Otherwise I wrote this function before. Explain error don't show me a good guidance. by the other hand I got a lot of things about function but they can't help me to solve my problem!
:(
saeed wrote:
....
The problem is the Igor don't accept string like {amount,amount} instead of number Otherwise I wrote this function before. Explain error don't show me a good guidance. by the other hand I got a lot of things about function but they can't help me to solve my problem!
:(


You can call Chozo's function from the command line. For example (presuming wave0 exists in the current data folder) ...

Pixelate(wave0,10)

The problem as I see it is, perhaps you may need to take some more time to go through the tutorials and RTFM (read the friendly manual). Some of the things you keep coming back to ask about are explained rather well there. I also have a nagging sense that you need to learn how to break your big software problem in to smaller logical steps. Just as you cannot build a house without a master plan, you cannot write a functioning software package without a reasonable flowchart. Do you have one?

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
jjweimer wrote:


You can call Chozo's function from the command line. For example (presuming wave0 exists in the current data folder) ...

Pixelate(wave0,10)

The problem as I see it is, perhaps you may need to take some more time to go through the tutorials and RTFM (read the friendly manual). Some of the things you keep coming back to ask about are explained rather well there.


My problem was the bad installing of Igor because of that with the correct code I saw errors, now I use Pixelate(wave0,10) and get good result. But if I want to running this function without writing in command line, what can I do? How can I evoke this function after another function? Also I want to run it many times but with command line i can execute it only one time.

Best regards
saeed wrote:
... Also I want to run it many times but with command line i can execute it only one time.


Function RunItManyTimes()

    variable hmt = 1, ic
    Prompt hmt, "How many times?"
    DoPrompt "Run the command multiple times", hmt
    if (v_flag)
        return -1
    endif
   
    for (ic=0;ic<hmt;ic+=1)
        print "This is run number ", ic+1
    endfor

    return 0
end


DisplayHelpTopic "DoPrompt"

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville