Posted by
Michael Schmid on
URL: http://imagej.273.s1.nabble.com/retrieve-variables-from-txt-file-and-run-command-tp5021572p5021573.html
Hi Chris,
you can have a list of commands for 'run' in a file.
The following works:
path="/tmp/filename.txt";
txt = File.openAsRawString(path);
delimiter= '\n';
lines = split(txt, delimiter);
for (i=0; i<lines.length; i++)
run(lines[i]);
And the file may contain:
Blobs (25K)
Green
Smooth
Invert
There should be no quotes, no spaces, no blank lines in the file.
It will open the blobs sample, apply the Green LUT, smooth and invert it.
Like this, it only works with 'run' commands that need no further
arguments, like
run("Green"): //apply green LUT
If you want 'run' commands with arguments, you can use e.g. tabs to
separate the name and argument:
path="/tmp/filename.txt";
txt = File.openAsRawString(path);
delimiter= '\n';
lines = split(txt, delimiter);
for (i=0; i<lines.length; i++) {
parts = split(lines[i],"\t");
if (parts.length ==1)
run(parts[0]);
else if (parts.length ==2)
run(parts[0], parts[1]);
}
Now you can process this file:
Blobs (25K)
Gaussian Blur... sigma=10
What cannot be done so easily this way is calling macro commands other
than 'run'.
Michael
________________________________________________________________
On 14.12.18 15:31, Matti Christoph wrote:
> Dear all,
>
> I have a macro, helping to annotate images with scale bar, channel
> names, doing a montage and so on. So far, the initial dialogue gives the
> user several options, and is filled with default values.
>
> I like to have a text file with those variables and recall them
> automatically when the Macro is started, and yes, that works.
>
> setOption("ExpandableArrays", true);
>
> mp = getDirectory("macros");
> pathset = mp+"CnZsettings.txt";
> string2 = newArray();
> file = File.openAsRawString(pathset);
> delimiter= '\n';
> string2 = replace(file, delimiter, "@");
> values = newArray();
> values = split(string2, "@");
>
> However, when taking the value from the text file (e.g. Green) and
> assigning to a variable:
>
> a=values[0];
>
> the run(a); or SetColor(a); to change either the channel colour or the
> foreground colour don't work, giving me /Unrecognised command: "Green"/.
> Retrieving however numerical values from the same .txt file and using
> them e.g. for the scale bar do work.
>
> Do you have any suggestion where to look for a solution? I believe that
> I somehow have to change the "type" of the variable or something?
>
> I already tried a="\""+values[0]+"\""; but this didn't work, giving me
> /Unrecognised command: ""Green""/.
>
> I am very happy if one of you could help me.
>
> Best,
>
> Chris
>
>
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html