Saving array as a .txt file

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

Saving array as a .txt file

Weller Andrew Francis
Dear all,

I have a folder of images that I am opening, analysing and saving the
results as a .txt file in a macro.

Say, for example, I have the following results saved as an array:

n = nResults;
ax = newArray(n);
ay = newArray(n);
for (j=0; j<n; j++) {
   ax[j] = getResult("ax", j);
   ay[j] = getResult("ay", j);
   }

I then open a .txt file to save to where *all* results are saved:

f = File.open("/home/aweller/ImageJ/table.txt");
print(f, "Image_Name" + "\t" + "Area" + "\t" + "..." + "\n");
print(f, name + "\t" + area + "\t" + ... + "\n");

How can I append the array to this in an easy way so that I also save
each ax[j] and ay[j]?

I guess that I can have a for-loop somewhere as long as I don't start a
new line with the "\n" variable? Or does each time print(f, ...) closes,
it starts a newline?

I appreciate that I can do:

print(f, ax0 + "\t" + ax1 + "\t" + ax2 + "\t" ... + "\n");

but this seems slightly long-winded and I guess there a more 'automated'
way (in case my array size changes)?

Many thanks, Andy
Reply | Threaded
Open this post in threaded view
|

String concatenation

Andy Weller
OK, I figure a while-loop should suffice outside the for-loop to
concatenate the strings. The only trouble is is that I'm having
difficulty concatenating the strings, if this is at all possible?

For example, given these results in an array:

n = nResults;
ax = newArray(n);
ay = newArray(n);
for (j=0; j<n; j++) {
   ax[j] = getResult("ax", j);
   ay[j] = getResult("ay", j);
   }

I then want to do something like the following:

t = "";
s = "";
z = 0;
while (z<=n) {
   s = ax[z] + "\t";
   t += s;
   z = z+1;
   }

t will then contain my concatenated string that I can append to my
output results (below). When running this though I am told that a
"number or numeric function is expected". I thought that I declared them
with t = "", etc?!?

Any pointers greatly appreciated.

Thanks, Andy

On Wed, 2006-07-26 at 14:26 +0200, Andy Weller wrote:

> Dear all,
>
> I have a folder of images that I am opening, analysing and saving the
> results as a .txt file in a macro.
>
> Say, for example, I have the following results saved as an array:
>
> n = nResults;
> ax = newArray(n);
> ay = newArray(n);
> for (j=0; j<n; j++) {
>    ax[j] = getResult("ax", j);
>    ay[j] = getResult("ay", j);
>    }
>
> I then open a .txt file to save to where *all* results are saved:
>
> f = File.open("/home/aweller/ImageJ/table.txt");
> print(f, "Image_Name" + "\t" + "Area" + "\t" + "..." + "\n");
> print(f, name + "\t" + area + "\t" + ... + "\n");
>
> How can I append the array to this in an easy way so that I also save
> each ax[j] and ay[j]?
>
> I guess that I can have a for-loop somewhere as long as I don't start a
> new line with the "\n" variable? Or does each time print(f, ...) closes,
> it starts a newline?
>
> I appreciate that I can do:
>
> print(f, ax0 + "\t" + ax1 + "\t" + ax2 + "\t" ... + "\n");
>
> but this seems slightly long-winded and I guess there a more 'automated'
> way (in case my array size changes)?
>
> Many thanks, Andy
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation

Gabriel Landini
On Wednesday 26 July 2006 16:27, Andy Weller wrote:

>    s = ax[z] + "\t";

I haven't tried this...

s = "";
z = 0;
while (z<=n) {
   s+= ""+ax[z] + "\t";
   z++;
   }

G.
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation

Andy Weller
HMmm, no joy, unfortunately!

Cheers, Andy

On Wed, 2006-07-26 at 16:45 +0100, Gabriel Landini wrote:

> On Wednesday 26 July 2006 16:27, Andy Weller wrote:
>
> >    s = ax[z] + "\t";
>
> I haven't tried this...
>
> s = "";
> z = 0;
> while (z<=n) {
>    s+= ""+ax[z] + "\t";
>    z++;
>    }
>
> G.
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation

Gabriel Landini
On Wednesday 26 July 2006 17:19, Andy Weller wrote:
>  s = "";
>  z = 0;
>  while (z<=n) {
>     s+= ""+ax[z] + "\t";
>     z++;
>     }
> HMmm, no joy, unfortunately!

Actually this works:

ax=newArray(1,2,3,4,5,6);
n=5;
s = "";
z = 0;
while (z<=n) {
    s=s+ax[z] + "\t";
    print(s);
    z++;
   }

Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

Cheers,

Gabriel