Operating on numbers from text file

Folks,

This will probably be an easy fix, but solution is eluding me. From a folder, I’m reading text file that accompanies a tif file in order to retrieve scale information that is incorporated in image analysis. Issue is when I’ve isolated needed numeric value, it appears Fiji is still treating number as a string value. This is evident by trying to do simple mathematical operations.

Do I need to make a variable declaration of some kind? Original text file, code, and log output is attached.

Many thanks.

Neil

orig text file
code
output

You may try:

parseFloat(string)
Converts the string argument to a number and returns it. Returns NaN (Not a Number) if the string cannot be converted into a number. Use the isNaN() function to test for NaN. For examples, see ParseFloatIntExamples.

(Taken from here.)

Many thanks for the input. However, that has not worked either.

Neil

Please make accessible for us the orginal text-file, perhaps zipped.

Just to clarify, you did something like

st = "this-12";
start = split(st,"-");
second = parseFloat(start[1]);
add = second+3;
print(add);

?
Something like t his would not be expected to work, and returns 123.

st = "this-12";
start = split(st,"-");
second = start[1];
parseFloat(second);
add = second+3;
print(add);

Thanks for the text-file. It helped somehow …

It uses a rare text format, namely

  • “Windows CRLF” line breaks
    that are responsible for row-number 28 (not 14)
  • Unicode “UTF-16 Little Endian” text coding
    that is responsible for the NaN-decoding of the float number

The simplest way to deal with this issue is to save the text file in Unicode UTF-8 format with Unix line feeds.

1 Like

The original code lines

a = pixsize[1];
print(a);
b = a + 3;
print(b);

should be replaced by:

a = pixsize[1];
aa = "";
for ( i=0; i<lengthOf(a)-1; i+=2 ) aa += substring( a, i+1, i+2 );
a = parseFloat(aa);
print(a);
b = a + 3;
print(b);
1 Like

Many thanks for the input. Per your earlier post, your second snippet was what I used.
I did incorporate your solution which seems to have worked.
To be honest, I’m still trying to understand how the extra lines of code are working, though. It looks like the for loop is going through the “496.0938” string and taking each character and equating to “aa”, but not sure how this is used with parseFloat to get the desired result.

First make sure you understand the difference beetween UTF-16 and UTF-8.

Digits, i.e. 1, 2, 3, etc, are single byte string characters, also in UTF-16 that is a two-byte format. Java and the ImageJ-macro language use UTF-8, i.e. a single byte format. Consequently, a string of digits in UTF-16 has non-printing (empty bytes) characters between each single byte character (digit) when represented in UTF-8 format. The for-loop takes every second character from the UTF-16 string, hence leaving out the empty bytes, i.e. the non-printing characters.

2 Likes

Great!

I appreciate the insight and education. Now that I know what is happening, I hand stepped through the loop to get a grasp of the output. Makes a lot more sense.

I again thank you for the help.

1 Like