Posted by
Matti Christoph on
URL: http://imagej.273.s1.nabble.com/retrieve-variables-from-txt-file-and-run-command-tp5021572p5021620.html
Dear Michael,
it worked, thank you! The issue was apparently the
string2 = replace(file, delimiter, "@");
which I included to be sure that there are no issues with the replace,
but after removing all goes fine.
However, i have a follow up question. I give the user the option, to
save the new set values as standard settings (saves the array with new
data as a txt file using a for loop and print(file lines[i]);) and I
want to give the user the option, to re-load "default values". This is
achieved by opening a second txt file (default_settings.txt), again add
it to an array and save this one as the settings.txt file. So far so good.
file = File.open(pathset);
for(i=0; i<lines.length; i++) {
print(file, lines[i]);
}
I then want to re-open the macro (call it xy.ijm), and tried this with
the runMacro("xy.ijm"), however there it gives me an error that the file
from which it reads the values is empty.
wait(2000);
runMacro("xy.ijm");
Even adding a wait(2000) after printing the new values and opening the
macro again didn't work. If I put an "exit()" after the print, and then
start the macro again by hand, the new values are propperly loaded. I
can leave it at that, however a totally automated way of closing and
re-starting the macro would be nice. Or even, say, go back to line xy
and start again.
Do you have any clues how to achieve this?
Best wishes,
Chris
Am 14.12.2018 um 16:40 schrieb Michael Schmid:
> 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--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html