Reusing code snippets?

Dear Igor users,

I am using the exact same lines of code from time to time. Especially for nested conditional loops it would be helpful not to copy&paste the exact same stuff over and over again and, if I change something about it, I have to copy&paste it again.

Is there a way to save these code snippets or reference to it without externalizing it as a function where I have to assign variables and strings and so on?
I read about using "STRUCT" but since this is meant for the frequent use of variables and strings and not commands, I think it does not really help. Assigning each command to a separate string as mentioned in the manual is not so useful for bigger code snippets.

Maybe I am missing something completely obvious here but I would really appreciate your help!

Best regards,

Martin
I have the impression, this is within one larger procedure file. In that case, would you be able to write a function call?

Function LargerProcedure(...)

    ...
    rtn = LoopCode(a,b)
    ...
    rtn = LoopCode(c,d)
    ...
end

Function LoopCode(x,y)
    variable x
    wave y

    variable ic, sv = 0

    for (ic=0;ic<x;ic+=1)
       sv += y[ic]
    endfor
    return sv
end


Could you provide an example in this way?

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

you totally got it right! I forgot to mention that I was talking about reusing code within one single function.
I was thinking about the mentioned kind of "general purpose" called functions before. I could provide some example code of what I mean but there is nothing complicated in there. The only thing is that I have more than 10 different variables and strings in use that I would have to change in order to create a function that I could reuse then.

If you are using the same code twice for example, it would reduce clutter significantly if you had not to copy&paste everything. On the other hand it is quite some effort to rewrite everything as a called function. I hope I do not strike you as whiny but I was looking for something easy like some kind of "Functional Structure", a simple reference that copies the same lines of code in another position so to say, that I could use without declaring anything which would work since it is only used within one single function.

But I guess there is nothing like that available?

Best regards,

Martin
Perhaps you could post a short example of the code that you want to streamline. Otherwise, I see no clear answer beyond what I have given.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Structures provide a way to package up a bunch of variables in a single entity which you can pass to a function as a single parameter. That's what they're for- a single clean container for a bunch of variables.

You are right to want to reduce several instances of identical code to a single function. Structures should help you with the huge-list-of-variables-to-pass problem. Because these engine functions are an implementation detail, and not intended to be used outside of the main function, make static so they don't appear as part of the published interface of your code.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Hello,

I apologize for the late response.

Probably you two are right. I thought about having something as easy as a reference, something like "paste marked code here".
But it makes sense to write some real general purpose functions that might help me for future projects as well. The additional effort is most likely well worth it. So thanks anyway! :)

Best regards,

Martin
Martin,

You could create a function that stores your code in a text string; the function uses putscraptext to put the copy the text to the clipboard and then uses DoIgorMenu to copy the clipboard contents to the current cursor position in the procedure editor. Additionally, you can add the function to a menu and assign a hot key combination to it. I use this method to paste a function template in a procedure when starting a new function. See example code below. Also in the Menu definition are two hot key combinations that I use for commentizing and decommentizing blocks of code.

I am mentioning this method as an alternative to the suggestions given so far, but I agree with those suggestions that, in your case, you are better off creating a separate function for your code snippet.

Jeff

Menu "Procedure", dynamic
    "Paste Function Template/1",/Q, PasteFunctionTemplate()
    "Commentize/8",/Q, Execute/P/Q/Z "DoIgorMenu \"Edit\", \"Commentize\""
    "Decommentize/9",/Q, Execute/P/Q/Z "DoIgorMenu \"Edit\", \"Decommentize\""
End

Function PasteFunctionTemplate()
    String text=""
    String sDate
   
//  sDate = Secs2Date(DateTime, 0)
    sDate = Secs2Date(DateTime, -2,"/")

    text += "\r"
    text += "//***********************************************************//\r"
    text += "//*****        Function Template       *****//\r"
    text += "//***********************************************************//\r"
    text += "// " + sDate +"                        //\r"
    text += "// --<Put function purpose, variables, information here, etc.>    //\r"
    text += "//***********************************************************//\r"
    text += "// --Called by:                    //\r"
    text += "// --Calls:                        //\r"
    text += "//***********************************************************//\r"
    text += "//Function  FunctionName() \r"
    text += "// <Insert Code Here>\r"
    text += "//\r"
    text += "//End\r"
    text += "//***********************************************************//\r"
    text += "//*****        End of Function Template    *****//\r"
    text += "//***********************************************************//\r"
   
    PutScrapText text
    DoIgorMenu "Edit", "Paste"
End
Hi Jeff,

That seems like a quite heavy workaround and I never knew that something like this was even possible. What exactly do you mean by "places the text to the current cursor position"? Would that really work while executing a function?

Best regards,

Martin
Irvine_audit wrote:

That seems like a quite heavy workaround and I never knew that something like this was even possible. What exactly do you mean by "places the text to the current cursor position"? Would that really work while executing a function?


Yes, it does work. Add my example code to a procedure window and compile it.

Then, with your procedure window open and set as the topmost window, move the flashing vertical cursor to the location where you want new text to begin. The cursor location can be moved with a mouse click or arrow keys.

Then use either the hot key combination (Control key + 1 [Windows OS]) or the menu to call the function for pasting the template. In my example this would be "PasteFunctionTemplate". Text will be pasted at the cursor location.
Interesting idea! I wonder if it might be worthwhile also in this way ...

* Store the code snippets as text files in a common folder (Igor Procedures / Common Snippets)
* Generate a function that calls a dialog box for a user to choose a code snippet text and paste it in place at the cursor

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