String concatenation

Posted by Andy Weller on
URL: http://imagej.273.s1.nabble.com/Saving-array-as-a-txt-file-tp3702004p3702005.html

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