# Importing and Exporting Tensor Data

>> Tensor Toolbox >> Working with Tensors >> Importing and Exporting Tensor Data

You can import and export tensor data in ASCII format for the following data types:

- `tensor`
- `matrix`
- `sptensor`
- `ktensor`

## Contents

- Tensor/Matrix Data
- Sptensor Data
- Ktensor Data
- Formatting Output - Data Values
- Formatting Output - Ktensor Lambda Values

## Tensor/Matrix Data

In the case of a tensor, the first three lines give details about the tensor. The format for a 4 x 3 x 2 tensor is as follows...

      tensor
      3
      4 3 2
      <value of A(1,1,1)>
      <value of A(2,1,1)>
      <value of A(3,1,1)>
      <value of A(4,1,1)>
      <value of A(1,2,1)>
      <value of A(2,2,1)>
      <value of A(3,2,1)>
      <value of A(4,2,1)>
      ...
      <value of A(2,3,2)>
      <value of A(3,3,2)>
      <value of A(4,3,2)>

A matrix is formatted the same as a 2-way tensor except that the first line says `matrix` rather than `tensor`.

    rng('default'); %<- Setting random seed for reproducibility of this script
    Y1 = tenrand([4 3 2]);
    export_data(Y1,'Y.tensor');
    Y2 = import_data('Y.tensor');
    assert(isequal(Y1,Y2)) %<-- test if equal when reading tensor from file
    type 'Y.tensor'

    tensor
    3
    4 3 2
    8.1472368639317894e-01
    9.0579193707561922e-01
    1.2698681629350606e-01
    9.1337585613901939e-01
    6.3235924622540951e-01
    9.7540404999409525e-02
    2.7849821886704840e-01
    5.4688151920498385e-01
    9.5750683543429760e-01
    9.6488853519927653e-01
    1.5761308167754828e-01
    9.7059278176061570e-01
    9.5716694824294557e-01
    4.8537564872284122e-01
    8.0028046888880011e-01
    1.4188633862721534e-01
    4.2176128262627499e-01
    9.1573552518906709e-01
    7.9220732955955442e-01
    9.5949242639290300e-01
    6.5574069915658684e-01
    3.5711678574189554e-02
    8.4912930586877711e-01
    9.3399324775755055e-01

## Sptensor Data

In the case of an sptensor, the first four lines give details about the sptensor. The format for a 4 x 3 x 2 sptensor with 10 nonzeros is as follows...

      sptensor
      3
      4 3 2
      3
      i1 j1 k1
      i2 j2 k2 <value of A(i2,j2,k2)>
      i3 j3 k3 <value of A(i3,j3,k3)>

    X1 = sptensor([1 1 1;2 2 2; 4 3 2],rand(3,1),[4 3 2]);
    export_data(X1,'X.sptensor');
    X2 = import_data('X.sptensor');
    assert(isequal(X1,X2)) %<-- test if equal when reading tensor from file
    type 'X.sptensor'

    sptensor
    3
    4 3 2
    3
    1 1 1 6.7873515485777347e-01
    2 2 2 7.5774013057833345e-01
    4 3 2 7.4313246812491618e-01

## Ktensor Data

In the case of an ktensor, the first four lines give details about the ktensor. The format for a 4 x 3 x 2 ktensor with rank=2 is as follows, which contains the lambda values and factor matrices, U, (one per tensor dimension), ...

      ktensor
      3
      4 3 2
      2
      <value of lambda1> <value of lambda2>
      matrix
      2
      4 2
      <value of U{1}(1,1)> <value of U{1}(1,2)
      ...
      <value of U{1}(4,1)> <value of U{1}(4,2)
      matrix
      2
      3 2
      <value of U{2}(1,1)> <value of U{2}(1,2)
      ...
      <value of U{2}(3,1)> <value of U{2}(3,2)
      matrix
      2
      2 2
      <value of U{3}(1,1)> <value of U{3}(1,2)
      <value of U{3}(2,1)> <value of U{3}(2,2)

    K1 = ktensor(rand(2,1), rand(4,2), rand(3,2), rand(2,2));
    export_data(K1,'K.ktensor');
    K2 = import_data('K.ktensor');
    assert(isequal(K1,K2)) %<-- test if equal when reading tensor from file
    type 'K.ktensor'

    ktensor
    3
    4 3 2
    2
    3.9222701953416816e-01 6.5547789017755664e-01
    matrix
    2
    4 2
    1.7118668781156177e-01 4.6171390631153941e-02
    7.0604608801960878e-01 9.7131781235847536e-02
    3.1832846377420676e-02 8.2345782832729264e-01
    2.7692298496088996e-01 6.9482862297581705e-01
    matrix
    2
    3 2
    3.1709948006086053e-01 4.3874435965639824e-01
    9.5022204883835493e-01 3.8155845709300840e-01
    3.4446080502908760e-02 7.6551678814900237e-01
    matrix
    2
    2 2
    7.9519990113706318e-01 4.8976439578823106e-01
    1.8687260455437860e-01 4.4558620071089949e-01

## Formatting Output - Data Values

When exporting tensor data your can specify the format of the data values using character vector that follows the convention used in `fprintf`. For example, this can be useful when exporting tensors that contain only integers, resulting in a file that is smaller in size. The optional `fmt_data` parameter to `export_data` specifies the format string, with `'%.16`' being the default value.

    X3 = sptensor([1 1 1;2 2 2; 4 3 2],[1;2;3],[4 3 2]);
    export_data(X3,'X_fmt_data.sptensor','fmt_data','%d');
    X4 = import_data('X_fmt_data.sptensor');
    assert(isequal(X3,X4)) %<-- test if equal when reading tensor from file
    type 'X_fmt_data.sptensor'

    sptensor
    3
    4 3 2
    3
    1 1 1 1
    2 2 2 2
    4 3 2 3

## Formatting Output - Ktensor Lambda Values

When exporting `ktensor` data your can specify the format of the `lambda` values using character vector that follows the convention used in `fprint`. For example, this can be useful when exporting tensors that contain only integers, resulting in a file that is smaller in size. The optional `fmt_lambda` parameter to `export_data` specifies the format string, with `'%.16'` being the default value.

    K3 = ktensor([1; 2], 4*ones(4,2), 3*ones(3,2), 2*ones(2,2));
    export_data(K3,'K_fmt_data_lambda.ktensor','fmt_data','%d','fmt_lambda','%d');
    K4 = import_data('K_fmt_data_lambda.ktensor');
    assert(isequal(K3,K4)) %<-- test if equal when reading tensor from file
    type 'K_fmt_data_lambda.ktensor'

    ktensor
    3
    4 3 2
    2
    1 2
    matrix
    2
    4 2
    4 4
    4 4
    4 4
    4 4
    matrix
    2
    3 2
    3 3
    3 3
    3 3
    matrix
    2
    2 2
    2 2
    2 2