Quantcast
Channel: Math.NET Numerics - Math.NET
Viewing all articles
Browse latest Browse all 224

Octave doesn't read exported matlab matrices

$
0
0

@Lalo wrote:

I'm new on this forum. I have a little problem exporting matrices in matlab format, because octave 3.6.4 and octave 4.0.0 reply with this message "error:load:error uncompressing data element ".
To replicate this behaviour try this simple program

   static void Main(string[] args)
    {
        Matrix<double> pippo = CreateMatrix.Dense<double>(2, 2, 1.0);
        pippo[0, 1] = 2.0; pippo[1, 0] = 3.0; pippo[1, 1] = 4.5;

        MatlabWriter.Write("prova2.mat", p, "pippo");
   }

Perhaps the problem is in octave, because I can read using MatlabReader a Matrix exported by octave in compressed format.

So I added to MatlabWriter a function WritePlain to export matrices using not compressed format.
In MatlabWriter.cs

   public static void WritePlain<T>(string filePath, Matrix<T> matrix, string matrixName)
        where T : struct, IEquatable<T>, IFormattable
    {
        using (var stream = File.OpenWrite(filePath))
        {
            Formatter.FormatFilePlain(stream, new[] { Pack(matrix, matrixName) });
        }
    }

and a function FormatFilePlain in Formatter.cs

    /// <summary>
    /// Writes all matrix blocks to a stream not using compress format.
    /// </summary>
    internal static void FormatFilePlain(Stream stream, IEnumerable<MatlabMatrix> matrices)
    {
        using (var buffer = new BufferedStream(stream))
        using (var writer = new BinaryWriter(buffer))
        {
            // write header and subsystem data offset (116+8 bytes)
            var header = Encoding.ASCII.GetBytes(HeaderText + DateTime.Now.ToString(Resources.MatlabDateHeaderFormat));
            writer.Write(header);
            Pad(writer, 116 - header.Length + 8, 32);

            // write version (2 bytes)
            writer.Write((short)0x100);

            // write little endian indicator (2 bytes)
            writer.Write((byte)0x49);
            writer.Write((byte)0x4D);

            foreach (var matrix in matrices)
            {
                // write data type
                writer.Write((int)DataType.Matrix);
                
                writer.Write(matrix.Data.Length);
                writer.Write(matrix.Data);
            }

            writer.Flush();
            writer.Close();
        }
    }

Using this code, octave can read matrices generated in Mathnet.Numerics.
Perhaps this code could be useful to someone.
Thanks for all.
Lalo.

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 224

Trending Articles