Importing array from text file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Importing array from text file

Martin Jaekel
Hi,

I wrote the following macro to label cell nuclei in my embryo pictures. I
import x and y coordinate values into arrays xcoor and ycoor from text
files. These text files are created by simply copy pasting Excel columns
into a TextEdit window on a mac and saving them as .txt.


macro "map nuclei"
{

// ------ Read info of open image ------------------------------------

IID = getImageID();
run("RGB Color");
Yfact = getHeight();
Xfact = getWidth();

// ------ Read .txt file of x coordinates into array -----------------

string = File.openAsString("");
xlines = split(string, "\n");
n_xlines = lengthOf(xlines);

xcoor=newArray(n_xlines);
for (n=0; n<n_xlines; n++)
    {
    xcoor[n] = xlines[n];
    }

// ------ Read .txt file of y coordinates into array -----------------

string = File.openAsString("");
ylines = split(string, "\n");

n_ylines = lengthOf(ylines);
ycoor=newArray(n_ylines);
for (m=0; m<n_ylines; m++)
    {
    ycoor[m]= ylines[m];
    }

// ------ Map nuclear positions on embryo picture --------------------

setColor(255, 50, 0);
setLineWidth(1);

for (i=0; i<n_xlines; i++)
    {
    X=xcoor[i]/100*Xfact-2;
    Y=ycoor[i]/100*Yfact-2;
    selectImage(IID);
    drawOval(X, Y, 4, 4);
    }
}


The values from the text file (I copy-paste columns from Excel into Text
Edti and Save) get stored in the appropriate arrays (xcoor, and ycoor). But
on line 48

'X=xcoor[i]/100*Xfact-2;'

a weird error occurs

';' expected in line 48
X = xcoor[i] </> 100 * Xfact -2;

Why can it not divide the xcoor value?
If I add commas to the .txt file manually and then copy-past them into the
macro (instead of reading them in automatically) the macro does not have
this problem. Any ideas?


Thanks martin
Reply | Threaded
Open this post in threaded view
|

Re: Importing array from text file

NehadHirmiz
Hey Martin,
I was trying to do a similar thing but I could not do any sort of caluclations. I think when you read a text file in the way your proposed it is registered as boolean. you should look into how to read it as integers.

I hope this helps,
Nehad
Reply | Threaded
Open this post in threaded view
|

Re: Importing array from text file

Albrecht Sigler
This post was updated on .
Hi Martin,

'xcoor' is an array.  Therefore, 'xcoor[n]' will be interpreted as a string in line 48:

    X =  xcoor[n] / 100 * Xfact -2;

Trying to divide the string by a number causes the error you described.  If however you have in your equation first a number, the numerical value of 'xcoor[n]' is being used (rather than the string itself), and your calculation should work as expected:

   X = 0.01 * xcoor[n] * Xfact -2;

best, Albrecht