# Write a two-dimensional array (matrix) to text files?

**URL:** <https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674>\
**Category:** Coding Questions\
**Created:** [April 11, 2020, 7:24pm UTC](https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674 "2020-04-11T19:24:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardano](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ardano/32/8787_2.png) [@ardano](https://discourse.processing.org/u/ardano)\
**Post date:** [April 11, 2020, 7:24pm UTC](https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674/1 "2020-04-11T19:24:47Z")

</div>

Hi everyone!  
I am an absolute beginner in Processing.  
I’m trying to figuring how to write a 12x12 two-dimensional array of integers to a .txt file but I don’t have any idea…  
Can someone please suggest me how?  
The full code is actually quite long and I don’t think it’s really important (just long, boring maths), but here’s the latest few lines where I finally print everything to the console:

```auto
  for (int i = 0; i < 12; i ++) { 
   for (int j = 0; j < 12; j ++) { 
     print (matrixpos[j][i] + 1 + " "); 
     if (j==11) {
println(); 
        }
     }
  }

```

…I guess the saving part should be added here, but again, I’m a beginner so I’m not sure…

Thank you all for help, any idea will be really appreciated.  
See ya!  
A.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 11, 2020, 8:26pm UTC](https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674/2 "2020-04-11T20:26:01Z")

</div>

Hello,

Consider:  
[https://processing.org/tutorials/data/](https://processing.org/tutorials/data/)

> **[Table / Reference](https://processing.org/reference/table)**
>
> Table objects store data with multiple rows and columns, much like in a traditional spreadsheet. Tables can be generated from scratch, dynamically, or using data from an existing file. Tables c…

> **Resources**
>
> I encourage you to review the resources available here:
> 
> - Processing website has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - Those available with the Processing PDE (IDE).  
> An exploration of the tabs will reveal what is available; there are libraries and examples there.
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 11, 2020, 11:33pm UTC](https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674/3 "2020-04-11T23:33:31Z")

</div>

This is what I’ve come up w/: 🤓

```auto
/**
 * Save & Load TSV as int[][] (v1.0.1)
 * GoToLoop (2020/Apr/11)
 *
 * https://Discourse.Processing.org/t/
 * write-a-two-dimensional-array-matrix-to-text-files/19674/3
 */

static final TableType TABLE_TYPE = TableType.TSV;
static final String FILENAME = "Matrix" + '.' + TABLE_TYPE.EXT;
static final int[] COL_TYPES = { Table.INT, Table.INT, Table.INT };
static final int ROWS = 12, COLS = 12;

int[][] matrixPos = new int[ROWS][COLS];

void setup() {
  // Save matrixPos[][] to disk:
  final String fullPath = dataPath(FILENAME);
  final String[] arr1d = intArr2dToStrArr1d(matrixPos, TABLE_TYPE.SEP);
  saveStrings(fullPath, arr1d);

  // Load matrixPos[][] from disk:
  final Table t = loadTable(FILENAME, TABLE_TYPE.EXT);
  t.setColumnTypes(COL_TYPES);
  matrixPos = tableToIntArr2d(t);

  // Display matrixPos[][]'s content:
  for (final int[] arr : matrixPos) println(str(arr));

  exit();
}

static final String[] intArr2dToStrArr1d(final int[][] arr2d, final char sep) {
  if (arr2d == null || arr2d.length == 0) return new String[0];

  final int len = arr2d.length;
  final String[] arr1d = new String[len];

  for (int i = 0; i < len; arr1d[i] = join(str(arr2d[i++]), sep));

  return arr1d;
}

static final int[][] tableToIntArr2d(final Table t) {
  if (t == null || t.getRowCount() == 0) return new int[0][0];

  final int rows = t.getRowCount(), arr2d[][] = new int[rows][];

  for (int i = 0; i < rows; arr2d[i] = t.getIntRow(i++));

  return arr2d;
}

static enum TableType {
  CSV("csv", ','), TSV("tsv", '\t');

  final String EXT;
  final char SEP;

  TableType(final String ext, final char sep) {
    EXT = ext;
    SEP = sep;
  }
}

```

I’ve relied on many Processing functions, such as: 🕶

- **str()** -\> [Processing.org/reference/strconvert\_.html](http://Processing.org/reference/strconvert_.html)
- **join()** -\> [Processing.org/reference/join\_.html](http://Processing.org/reference/join_.html)
- **saveStrings()** -\> [Processing.org/reference/saveStrings\_.html](http://Processing.org/reference/saveStrings_.html)
- **loadTable()** -\> [Processing.org/reference/loadTable\_.html](http://Processing.org/reference/loadTable_.html)

---

<div class="post-metadata">

**Author:** ![ardano](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ardano/32/8787_2.png) [@ardano](https://discourse.processing.org/u/ardano)\
**Post date:** [April 12, 2020, 2:30pm UTC](https://discourse.processing.org/t/write-a-two-dimensional-array-matrix-to-text-files/19674/4 "2020-04-12T14:30:13Z")

</div>

Done!  
I was not familiar with the table object.  
Thank you all for your help and for every suggestion.
