Search code examples
matlabfilematrixoctave

Cannot save 2D and 3D matrices to a file


I cannot save a matrix to a file with Octave. Here is what happens:

A = [1 2 3; 4 5 6; 7 8 9]
dlmwrite("test.data", A)

Output:

A =

   1   2   3
   4   5   6
   7   8   9

No file created.

Trying to save a 3D matrix:

A = [1 2 3; 4 5 6; 7 8 9]
A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
dlmwrite("test.data", A)

Output:

A =

   1   2   3
   4   5   6
   7   8   9

A =

ans(:,:,1) =

   1   2   3
   4   5   6
   7   8   9

ans(:,:,2) =

   10   11   12
   13   14   15
   16   17   18

error: transpose not defined for N-D objects
error: called from
    dlmwrite at line 202 column 7
    test_3d at line 31 column 1

No file created.

So, my questions are:

  • Why is no file produced in either case?
  • What is the meaning of the error message about transpose, when I perform no such operation?
  • Does this only happen with Octave, or also with MATLAB?

Octave version: 6.3.0


Solution

  • I ran the test on my machine and god a similar error with Octave 5.1.0:

    In my case I got error:

    error: transpose not defined for N-D objects
    error: called from
        dlmwrite at line 195 column 7
    

    In dlmwrite.m (Octave\Octave-5.1.0.0\mingw64\share\octave\5.1.0\m\io\dlmwrite.m).script I found: enter image description here

    They are basically attempting to transpose the incoming array. The transpose operator is defined for two-dimensional arrays but not for three-dimensional arrays, which is why an error is returned.

    I ran the same test in MATLAB R2020a, which produced no error and output a file (test.data) with the following contents:

    1,2,3,10,11,12
    4,5,6,13,14,15
    7,8,9,16,17,18
    

    In either case, the documentation does not explicitly mention that the array must not have more than two dimensions, but it looks like the Matlab function is written to support more than two dimensions and the Octave equivalent does not.

    With regards to the questions:

    Why no file is produced in either case?

    Since Octave uses a script to implement dlmwrite, whenever an error is identified the remainder of the script is not parsed. Therefore, the file is not created

    What is the meaning of the error message about transpose, where I perform no such operation?

    The error message indicates that Octave attempted to transpose (flip) an array of more than two dimensions, which is not supported by the transpose operator(').

    Does this only happens with Octave, or also with MATLAB?

    This does not happen in MATLAB R2020a. It looks like they have built in support for multi-dimensional arrays in this function.

    It may be a good idea to create an Octave bug(https://www.gnu.org/software/octave/bugs). The dlmwrite function should either be updated to support more than two dimensions, or the documentation for the function (help dlmwrite) should explicitly state that the input parameter M must have no greater than two dimensions.