From: hemke on
Hi,

I am trying to export some data I imported and then manipulated with
mathematica. I have a List of several Tables, i.e.

ListA={TableA, TableB, TableC, ...}

A typical Table looks like this:

TableA={{1,2},{4,5},...}.

The Tables in ListA all have the same number of columns but differing
number of rows. Now, I want to export these Tables into one tabulator
separated file with the same structure, i.e. line N in the output file
should contain the elements in row N of each Table, or in other words I
want the following:

TableA[[1,1]] TableA[[1,2]] TableB[[1,1]] TableB[[1,2]] ...
TableA[[2,1]] TableA[[2,2]] TableB[[2,1]] TableB[[2,2]] ...
.... ... ... ...
TableA[[M-1,1]] TableA[[M-1,2]] TableB[[P,1]] TableB[[P,2]] ...
TableA[[M,1]] TableA[[M,2]] ...

Here I chose (as an example), Length[TableA]=M and Length[TableB]=P with
M>P.

I tried it with Export["myfile.dat", ListA] but it does not produce the
desired output, instead it yields:

{TableA[[1,1]],TableA[[1,2]]} {TableA[[2,1]],TableA[[2,2]]} ...
{TableB[[1,1]],TableB[[1,2]]} {TableB[[2,1]],TableB[[2,2]]} ...
.... ...

(In the above, TableX[[i,j]] represents a number in the corresponding
element of a table, and not the text "TableX[[i,j]]"... ;) )

Ok, I hope I explained the problem sufficiently. Probably there is a
simple solution but I just don't see it.

Please help! Thanks!

Hemke

From: Bill Rowe on
On 6/29/10 at 8:27 AM, h.maeter(a)gmail.com (hemke) wrote:

>I am trying to export some data I imported and then manipulated with
>mathematica. I have a List of several Tables, i.e.

>ListA={TableA, TableB, TableC, ...}

>A typical Table looks like this:

>TableA={{1,2},{4,5},...}.

>The Tables in ListA all have the same number of columns but
>differing number of rows. Now, I want to export these Tables into
>one tabulator separated file with the same structure, i.e. line N in
>the output file should contain the elements in row N of each Table,
>or in other words I want the following:

>TableA[[1,1]] TableA[[1,2]] TableB[[1,1]] TableB[[1,2]] ...
>TableA[[2,1]] TableA[[2,2]] TableB[[2,1]] TableB[[2,2]] ...
>... ... ... ...
>TableA[[M-1,1]] TableA[[M-1,2]] TableB[[P,1]] TableB[[P,2]] ...
>TableA[[M,1]] TableA[[M,2]] ...

It appears that you want the output file to have one table
followed immediately by another. If so,

Export["myfile.dat", Join@@ListA]

will do the trick

Something a little fancier would be

sep=Table["",{Dimensions[First(a)ListA][[2]]}];
Export["myfile.dat", Join@@(Append[#, sep]&/@ListA)]

This will write a blank line after each table in the resulting file.


From: Sjoerd C. de Vries on
Hi Hemke,

My solution uses PadRight to make all matrices have the same lengths,
and ArrayFlatten to combine all matrices in a single rectangular one.

Some example matrices:

m = {{1, 2, 3}, {4, 5, 6}};
n = {{7, 8, 9}, {1, 2, 3}, {4, 5, 6}};
o = {{2, 4, 6}, {1, 3, 5}, {9, 7, 5}, {8, 6, 4}};

listA = {m, n, o};

The code:

mostRows = Max[Length /@ listA];
numColumns = Length[listA[[1, 1]]]; (*assumes, as given, that all
matrices have the same number of columns *)
newListA =
ArrayFlatten[{PadRight[#, {mostRows, numColumns}, Null] & /@
listA}];
Export["C:\\Documents and Settings\\Sjoerd\\Desktop\\New Text \
Document.tsv", newListA, "TSV"]

(The "TSV" part is superfluous if the filename already has this as
extension)

Cheers -- Sjoerd


On Jun 29, 2:27 pm, hemke <h.mae...(a)gmail.com> wrote:
> Hi,
>
> I am trying to export some data I imported and then manipulated with
> mathematica. I have a List of several Tables, i.e.
>
> ListA={TableA, TableB, TableC, ...}
>
> A typical Table looks like this:
>
> TableA={{1,2},{4,5},...}.
>
> The Tables in ListA all have the same number of columns but differing
> number of rows. Now, I want to export these Tables into one tabulator
> separated file with the same structure, i.e. line N in the output file
> should contain the elements in row N of each Table, or in other words I
> want the following:
>
> TableA[[1,1]] TableA[[1,2]] TableB[[1,1]] TableB[[1,2]] .=
...
> TableA[[2,1]] TableA[[2,2]] TableB[[2,1]] TableB[[2,2]] .=
...
> ... ... ... =
...
> TableA[[M-1,1]] TableA[[M-1,2]] TableB[[P,1]] TableB[[P,2]] ...
> TableA[[M,1]] TableA[[M,2]] =
...
>
> Here I chose (as an example), Length[TableA]=M and Length[TableB]=P w=
ith
> M>P.
>
> I tried it with Export["myfile.dat", ListA] but it does not produce the
> desired output, instead it yields:
>
> {TableA[[1,1]],TableA[[1,2]]} {TableA[[2,1]],TableA[[2,2]]} ...
> {TableB[[1,1]],TableB[[1,2]]} {TableB[[2,1]],TableB[[2,2]]} ...
> ... ...
>
> (In the above, TableX[[i,j]] represents a number in the corresponding
> element of a table, and not the text "TableX[[i,j]]"... ;) )
>
> Ok, I hope I explained the problem sufficiently. Probably there is a
> simple solution but I just don't see it.
>
> Please help! Thanks!
>
> Hemke


From: hemke on
Thanks a lot, Sjoerd! This is exactly what I was looking for. I wasn't
aware of the Null symbol...

Hemke

Sjoerd C. de Vries schrieb:
> Hi Hemke,
>
> My solution uses PadRight to make all matrices have the same lengths,
> and ArrayFlatten to combine all matrices in a single rectangular one.
>
> Some example matrices:
>
> m = {{1, 2, 3}, {4, 5, 6}};
> n = {{7, 8, 9}, {1, 2, 3}, {4, 5, 6}};
> o = {{2, 4, 6}, {1, 3, 5}, {9, 7, 5}, {8, 6, 4}};
>
> listA = {m, n, o};
>
> The code:
>
> mostRows = Max[Length /@ listA];
> numColumns = Length[listA[[1, 1]]]; (*assumes, as given, that all
> matrices have the same number of columns *)
> newListA =
> ArrayFlatten[{PadRight[#, {mostRows, numColumns}, Null] & /@
> listA}];
> Export["C:\\Documents and Settings\\Sjoerd\\Desktop\\New Text \
> Document.tsv", newListA, "TSV"]
>
> (The "TSV" part is superfluous if the filename already has this as
> extension)
>
> Cheers -- Sjoerd
>
>
> On Jun 29, 2:27 pm, hemke <h.mae...(a)gmail.com> wrote:
>> Hi,
>>
>> I am trying to export some data I imported and then manipulated with
>> mathematica. I have a List of several Tables, i.e.
>>
>> ListA={TableA, TableB, TableC, ...}
>>
>> A typical Table looks like this:
>>
>> TableA={{1,2},{4,5},...}.
>>
>> The Tables in ListA all have the same number of columns but differing
>> number of rows. Now, I want to export these Tables into one tabulator
>> separated file with the same structure, i.e. line N in the output file
>> should contain the elements in row N of each Table, or in other words I
>> want the following:
>>
>> TableA[[1,1]] TableA[[1,2]] TableB[[1,1]] TableB[[1,2]] .=
> ..
>> TableA[[2,1]] TableA[[2,2]] TableB[[2,1]] TableB[[2,2]] .=
> ..
>> ... ... ... =
> ...
>> TableA[[M-1,1]] TableA[[M-1,2]] TableB[[P,1]] TableB[[P,2]] ...
>> TableA[[M,1]] TableA[[M,2]] =
> ...
>> Here I chose (as an example), Length[TableA]=M and Length[TableB]=P w=
> ith
>> M>P.
>>
>> I tried it with Export["myfile.dat", ListA] but it does not produce the
>> desired output, instead it yields:
>>
>> {TableA[[1,1]],TableA[[1,2]]} {TableA[[2,1]],TableA[[2,2]]} ...
>> {TableB[[1,1]],TableB[[1,2]]} {TableB[[2,1]],TableB[[2,2]]} ...
>> ... ...
>>
>> (In the above, TableX[[i,j]] represents a number in the corresponding
>> element of a table, and not the text "TableX[[i,j]]"... ;) )
>>
>> Ok, I hope I explained the problem sufficiently. Probably there is a
>> simple solution but I just don't see it.
>>
>> Please help! Thanks!
>>
>> Hemke
>
>