Dear all,
I have built up a rather lengthy macro and am splitting it up into discrete user-defined functions for debugging simplicity. I think I understand how to operate them (from: http://rsb.info.nih.gov/ij/developer/macro/macros.html#functions); can someone explain to me if I have the wrong idea please (regarding passing things to/from them really)? macro "Test" { open(); img = getImageID(); setAutoThreshold(); run("Analyze Particles...", "size=5000-Infinity circularity=0.00-1.00 show=Masks exclude clear include"); mask = getImageID; myfunction(img, mask); // Passes img and mask to function "myfunction" } function myfunction(img, mask) { // img and mask passed to "myfunction" some measurements on img and mask return results; // "results" returned to macro "Test" } Thanks, Andy |
I have a user-defined function that does various measurements:
function measurements() { some measurements ... out = ""; out = Name + "," + area + "," + ...; // String container for all results return out; } Now in my main macro it states that "out" is an "undefined variable", although I am returning it from function measurements()?! macro "Test" { dir1 = getDirectory("Choose Source Directory"); list = getFileList(dir1); numberFile = list.length; setBatchMode(true); compResults = newArray(numberFile); for (i=0; i<numberFile; i++) { some stuff ... measurements(); compResults[i] = out; // THIS IS WHERE IT BUMS OUT?! } Any clues out there?! Thanks, Andy On Wed, 2006-08-02 at 14:04 +0200, Andy Weller wrote: > Dear all, > > I have built up a rather lengthy macro and am splitting it up into > discrete user-defined functions for debugging simplicity. > > I think I understand how to operate them (from: > http://rsb.info.nih.gov/ij/developer/macro/macros.html#functions); can > someone explain to me if I have the wrong idea please (regarding passing > things to/from them really)? > > macro "Test" { > open(); > img = getImageID(); > setAutoThreshold(); > run("Analyze Particles...", "size=5000-Infinity circularity=0.00-1.00 > show=Masks exclude clear include"); > mask = getImageID; > myfunction(img, mask); // Passes img and mask to function "myfunction" > } > > function myfunction(img, mask) { // img and mask passed to "myfunction" > some measurements on img and mask > return results; // "results" returned to macro "Test" > } > > Thanks, Andy |
Hi Andy,
2 ways... 1) define out as variable in the beginning var out=""; this will give you some kind of global variable. 2) using return values main ... result = myfunction(); ... function myfunction(){ ... out="whatever"; return out; } both ways should work... Falk BTW: Thx, for the focus macros on the wiki page! -----Ursprüngliche Nachricht----- Von: ImageJ Interest Group [mailto:[hidden email]] Im Auftrag von Andy Weller Gesendet: Mittwoch, 2. August 2006 15:22 An: [hidden email] Betreff: Re: User-defined functions I have a user-defined function that does various measurements: function measurements() { some measurements ... out = ""; out = Name + "," + area + "," + ...; // String container for all results return out; } Now in my main macro it states that "out" is an "undefined variable", although I am returning it from function measurements()?! macro "Test" { dir1 = getDirectory("Choose Source Directory"); list = getFileList(dir1); numberFile = list.length; setBatchMode(true); compResults = newArray(numberFile); for (i=0; i<numberFile; i++) { some stuff ... measurements(); compResults[i] = out; // THIS IS WHERE IT BUMS OUT?! } Any clues out there?! Thanks, Andy On Wed, 2006-08-02 at 14:04 +0200, Andy Weller wrote: > Dear all, > > I have built up a rather lengthy macro and am splitting it up into > discrete user-defined functions for debugging simplicity. > > I think I understand how to operate them (from: > http://rsb.info.nih.gov/ij/developer/macro/macros.html#functions); can > someone explain to me if I have the wrong idea please (regarding > passing things to/from them really)? > > macro "Test" { > open(); > img = getImageID(); > setAutoThreshold(); > run("Analyze Particles...", "size=5000-Infinity circularity=0.00-1.00 > show=Masks exclude clear include"); mask = getImageID; myfunction(img, > mask); // Passes img and mask to function "myfunction" > } > > function myfunction(img, mask) { // img and mask passed to "myfunction" > some measurements on img and mask > return results; // "results" returned to macro "Test" > } > > Thanks, Andy _____________________ Confidentiality _____________________ This electronic transmission is strictly confidential and intended solely for the addressee. It may contain information which is covered by legal, professional or other privilege. If you are not the intended addressee, you must not disclose, copy or take any action in reliance of this transmission. If you have received this transmission in error, please notify us and delete the received data as soon as possible. This footnote also confirms that this email message has been swept for the presence of computer viruses. _______________________________________________________ |
In reply to this post by Andy Weller
Andy Weller wrote:
> I have a user-defined function that does various measurements: > > function measurements() { > some measurements > ... > out = ""; > out = Name + "," + area + "," + ...; // String container for all results > return out; > } > > Now in my main macro it states that "out" is an "undefined variable", > although I am returning it from function measurements()?! > > macro "Test" { > dir1 = getDirectory("Choose Source Directory"); > list = getFileList(dir1); > numberFile = list.length; > setBatchMode(true); > compResults = newArray(numberFile); > for (i=0; i<numberFile; i++) { > some stuff > ... > measurements(); > compResults[i] = out; // THIS IS WHERE IT BUMS OUT?! > } Hello Andy, this "out" is still undefined, even if your function returns a value (the "out" string it has just built), the "out" variable is not defined in the macro, it's a local variable in your function. just like this: macro "test" { //doit();//wrong: x is locally defined in doit(), not defined here. x=doit(); //correct print(x); } function doit() { x=10; return x; } If you need a global variable, you have to define it first before your macro var out="" ; .... macro "test" { ... print(out); } beware, the "out" variable modified in your function call will now be the global one. Sebastien |
Hi all,
(I will document the response to this on the Docs website soon.) For some reason I can't set global variables outside my macro?! The macro just doesn't start. What if I want to return 2 variables instead of just the 1 - or is this bad practise? (I guess I can call a function from within a function.) macro "test" { x, y=doit(); print(x, y); } function doit() { x=10; y=20; return x, y; } ?!? Cheers, Andy On Wed, 2006-08-02 at 15:56 +0200, seb wrote: > Andy Weller wrote: > > I have a user-defined function that does various measurements: > > > > function measurements() { > > some measurements > > ... > > out = ""; > > out = Name + "," + area + "," + ...; // String container for all results > > return out; > > } > > > > Now in my main macro it states that "out" is an "undefined variable", > > although I am returning it from function measurements()?! > > > > macro "Test" { > > dir1 = getDirectory("Choose Source Directory"); > > list = getFileList(dir1); > > numberFile = list.length; > > setBatchMode(true); > > compResults = newArray(numberFile); > > for (i=0; i<numberFile; i++) { > > some stuff > > ... > > measurements(); > > compResults[i] = out; // THIS IS WHERE IT BUMS OUT?! > > } > > Hello Andy, > > this "out" is still undefined, even if your function returns a value > (the "out" string it has just built), the "out" variable is not defined > in the macro, it's a local variable in your function. > > just like this: > > macro "test" > { > //doit();//wrong: x is locally defined in doit(), not defined here. > x=doit(); //correct > print(x); > } > > function doit() > { > x=10; > return x; > } > > > If you need a global variable, you have to define it first before your > macro > var out="" ; > .... > macro "test" > { > ... > print(out); > } > > beware, the "out" variable modified in your function call will now be > the global one. > > Sebastien |
Andy Weller wrote:
> Hi all, > > (I will document the response to this on the Docs website soon.) > > For some reason I can't set global variables outside my macro?! The > macro just doesn't start. Hi Andy, If you use the macro editor and want to run it, try to install it first (usually Ctl-I) and start it from the menu. > > What if I want to return 2 variables instead of just the 1 - or is this > bad practise? (I guess I can call a function from within a function.) > > macro "test" > { > x, y=doit(); > print(x, y); > } > > function doit() > { > x=10; > y=20; > return x, y; > } > > ?!? Your function can't return multiple values, but you can return an array: function foobar() { output=newArray("foo","bar"); return output; } macro "test" { strarray=foobar(); print (strarray[0]+"\n"+strarray[1]); } Sebastien |
Unfortunately I still can't define a global variable. Here's my code:
var someText = ""; macro "Test" { test(); } function test() { someText = "Here is some text"; } It just bums out and doesn't do anything at all!? Andy On Wed, 2006-08-02 at 19:15 +0200, seb wrote: > Andy Weller wrote: > > Hi all, > > > > (I will document the response to this on the Docs website soon.) > > > > For some reason I can't set global variables outside my macro?! The > > macro just doesn't start. > > Hi Andy, > > If you use the macro editor and want to run it, try to install it first > (usually Ctl-I) and start it from the menu. > > > > > What if I want to return 2 variables instead of just the 1 - or is this > > bad practise? (I guess I can call a function from within a function.) > > > > macro "test" > > { > > x, y=doit(); > > print(x, y); > > } > > > > function doit() > > { > > x=10; > > y=20; > > return x, y; > > } > > > > ?!? > > > > Your function can't return multiple values, but you can return an array: > > function foobar() > { > output=newArray("foo","bar"); > return output; > } > > macro "test" > { > strarray=foobar(); > print (strarray[0]+"\n"+strarray[1]); > } > > Sebastien |
I don't have experience with macros, but I would say that this code
should have no visible effect, since there is no output command, like for example print, in it. Shouldn't it be something like: var someText = ""; macro "Test" { test(); print someText; } function test() { someText = "Here is some text"; } Volker Andy Weller a écrit : > Unfortunately I still can't define a global variable. Here's my code: > > var someText = ""; > macro "Test" { > test(); > } > > function test() { > someText = "Here is some text"; > } > > It just bums out and doesn't do anything at all!? > > Andy > > On Wed, 2006-08-02 at 19:15 +0200, seb wrote: > >> Andy Weller wrote: >> >>> Hi all, >>> >>> (I will document the response to this on the Docs website soon.) >>> >>> For some reason I can't set global variables outside my macro?! The >>> macro just doesn't start. >>> >> Hi Andy, >> >> If you use the macro editor and want to run it, try to install it first >> (usually Ctl-I) and start it from the menu. >> >> >>> What if I want to return 2 variables instead of just the 1 - or is this >>> bad practise? (I guess I can call a function from within a function.) >>> >>> macro "test" >>> { >>> x, y=doit(); >>> print(x, y); >>> } >>> >>> function doit() >>> { >>> x=10; >>> y=20; >>> return x, y; >>> } >>> >>> ?!? >>> >> >> Your function can't return multiple values, but you can return an array: >> >> function foobar() >> { >> output=newArray("foo","bar"); >> return output; >> } >> >> macro "test" >> { >> strarray=foobar(); >> print (strarray[0]+"\n"+strarray[1]); >> } >> >> Sebastien >> > > -- passerelle antivirus du campus CNRS de Montpellier -- |
In reply to this post by Andy Weller
Andy Weller wrote:
> Unfortunately I still can't define a global variable. Here's my code: > > var someText = ""; > macro "Test" { > test(); > } > > function test() { > someText = "Here is some text"; > } > > It just bums out and doesn't do anything at all!? > Hi, I'm not sure that your macro really demonstrates whether or not the global variable is defined - perhaps if you print the result of calling your "test()" function? In any event you might try the saving and installing the following. You can see that the function "test()" has access to the global variable "specialText" even though it was only passed the name of the calling macro. var specialText = " which is a macro"; macro "Test1" { myNewText = test("Test1"); print(myNewText); } macro "Test2" { myNewText = test("Test2"); print(myNewText); } function test( caller ) { someText = "Here is some text from " + caller + specialText; return someText; } Note that you really have to install these using the Plugins>Macro>Install after which two macros will appear under the Plugins>Macro menus. If you try to run these from the built-in macro editor I don't think ImageJ will know where the entry point is. Hope this is helpful. Ben |
In reply to this post by Volker Baecker
Hi Volker,
Yes I am aware of this. Is was meant purely to demonstrate how I am calling up the global variable outside of the macro (this, I guess, is where my problem lies). It is a much more complicated macro than this. Thanks, Andy On Thu, 2006-08-03 at 16:25 +0200, Volker Bäcker wrote: > I don't have experience with macros, but I would say that this code > should have no visible effect, since there is no output command, like > for example print, in it. Shouldn't it be something like: > > var someText = ""; > macro "Test" { > test(); > print someText; > } > > function test() { > someText = "Here is some text"; > > } > > > Volker |
In reply to this post by Andy Weller
This works for me. Maybe the problem was having the macro and function
both named test? var someText = ""; macro "Test" { testText(); print(someText); } function testText() { someText = "Here is some text"; } At 04:09 PM 08/03/06 +0200, you wrote: >Unfortunately I still can't define a global variable. Here's my code: > >var someText = ""; >macro "Test" { >test(); >} > >function test() { >someText = "Here is some text"; >} > >It just bums out and doesn't do anything at all!? > >Andy > >On Wed, 2006-08-02 at 19:15 +0200, seb wrote: > > Andy Weller wrote: > > > Hi all, > > > > > > (I will document the response to this on the Docs website soon.) > > > > > > For some reason I can't set global variables outside my macro?! The > > > macro just doesn't start. > > > > Hi Andy, > > > > If you use the macro editor and want to run it, try to install it first > > (usually Ctl-I) and start it from the menu. > > > > > > > > What if I want to return 2 variables instead of just the 1 - or is this > > > bad practise? (I guess I can call a function from within a function.) > > > > > > macro "test" > > > { > > > x, y=doit(); > > > print(x, y); > > > } > > > > > > function doit() > > > { > > > x=10; > > > y=20; > > > return x, y; > > > } > > > > > > ?!? > > > > > > > > Your function can't return multiple values, but you can return an array: > > > > function foobar() > > { > > output=newArray("foo","bar"); > > return output; > > } > > > > macro "test" > > { > > strarray=foobar(); > > print (strarray[0]+"\n"+strarray[1]); > > } > > > > Sebastien ____________________________________________________________________________ Michael Cammer Analytical Imaging Facility Albert Einstein Coll. of Med. URL: http://www.aecom.yu.edu/aif/ |
Free forum by Nabble | Edit this page |