Example #1
0
        /// <summary>
        /// Reads Matrix dimensions.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns>Dimensions int array</returns>
        private int[] ReadDimension(Stream buf)
        {
            ISMatTag tag = new ISMatTag(buf);

            int[] dims = tag.ReadToIntArray();
            return(dims);
        }
Example #2
0
        /// <summary>
        /// Reads Matrix dimensions.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns>Dimensions int array</returns>
        int[] ReadDimension(Stream buf)
        {
            var tag  = new ISMatTag(buf);
            var dims = tag.ReadToIntArray();

            return(dims);
        }
Example #3
0
        /// <summary>
        /// Reads Matrix flags.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns>Flags int array</returns>
        private int[] ReadFlags(Stream buf)
        {
            ISMatTag tag = new ISMatTag(buf);

            int[] flags = tag.ReadToIntArray();
            return(flags);
        }
Example #4
0
        /// <summary>
        /// Reads Matrix flags.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns>Flags int array</returns>
        int[] ReadFlags(Stream buf)
        {
            var tag = new ISMatTag(buf);

            var flags = tag.ReadToIntArray();

            return(flags);
        }
Example #5
0
        /// <summary>
        /// Reads Matrix name.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns><c>string</c></returns>
        private string ReadName(Stream buf)
        {
            string s;

            ISMatTag tag = new ISMatTag(buf);

            char[] ac = tag.ReadToCharArray();
            s = new string(ac);

            return(s);
        }
Example #6
0
        /// <summary>
        /// Reads Matrix name.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns><c>string</c></returns>
        string ReadName(Stream buf)
        {
            string s;

            var tag = new ISMatTag(buf);
            var ac  = tag.ReadToCharArray();

            s = new string(ac);

            return(s);
        }
Example #7
0
        /// <summary>
        /// Reads data from the <c>BinaryReader</c> stream. Searches for either
        /// <c>miCOMPRESSED</c> data or <c>miMATRIX</c> data.
        /// </summary>
        /// <remarks>
        /// Compressed data is inflated and the product is recursively passed back
        /// to this same method.
        /// </remarks>
        /// <param name="buf">The input <c>BinaryReader</c> stream.</param>
        private void ReadData(Stream buf)
        {
            // read data
            ISMatTag tag = new ISMatTag(buf);

            switch (tag.Type)
            {
            case MatDataTypes.miCOMPRESSED:
                // inflate and recur
            {
                Stream uncompressed = Inflate(buf, tag.Size);
                ReadData(uncompressed);
                uncompressed.Close();
            }
            break;

            case MatDataTypes.miMATRIX:
                // read in the matrix
                int pos = (int)buf.Position;
                int red, toread;

                MLArray element = ReadMatrix(buf, true);

                if (element != null)
                {
                    _data.Add(element.Name, element);
                }
                else
                {
                    red          = (int)buf.Position - pos;
                    toread       = tag.Size - red;
                    buf.Position = buf.Position + toread;
                }
                red = (int)buf.Position - pos;

                toread = tag.Size - red;

                if (toread != 0)
                {
                    throw new MatlabIOException("Matrix was not read fully! " + toread + " remaining in the buffer.");
                }
                break;

            default:
                throw new MatlabIOException("Incorrect data tag: " + tag);
            }
        }
Example #8
0
        /// <summary>
        /// Reads <c>miMATRIX</c> from the input stream.
        /// </summary>
        /// <remarks>
        /// If reading was not finished (which is normal for filtered results)
        /// returns <c>null</c>.
        /// </remarks>
        /// <param name="buf">The <c>BinaryReader</c> input stream.</param>
        /// <param name="isRoot">When <c>true</c> informs that if this is a top level
        /// matrix.</param>
        /// <returns><c>MLArray</c> or <c>null</c> if matrix does not match <c>filter</c></returns>
        private MLArray ReadMatrix(Stream buf, bool isRoot)
        {
            // result
            MLArray  mlArray = null;
            ISMatTag tag;

            // read flags
            int[] flags      = ReadFlags(buf);
            int   attributes = (flags.Length != 0) ? flags[0] : 0;
            int   nzmax      = (flags.Length != 0) ? flags[1] : 0;
            int   type       = attributes & 0xff;

            // read Array dimension
            int[] dims = ReadDimension(buf);

            // read Array name
            string name = ReadName(buf);

            // If this array is filtered out return immediately
            if (isRoot && !_filter.Matches(name))
            {
                return(null);
            }

            // read data
            switch (type)
            {
            case MLArray.mxSTRUCT_CLASS:
                MLStructure mlStruct = new MLStructure(name, dims, type, attributes);

                BinaryReader br = new BinaryReader(buf);

                // field name length - this subelement always uses the compressed data element format
                tag = new ISMatTag(br.BaseStream);
                int maxlen = br.ReadInt32();

                // Read fields data as Int8
                tag = new ISMatTag(br.BaseStream);
                // calculate number of fields
                int numOfFields = tag.Size / maxlen;

                // padding after field names
                int padding = (tag.Size % 8) != 0 ? 8 - tag.Size % 8 : 0;

                string[] fieldNames = new string[numOfFields];
                for (int i = 0; i < numOfFields; i++)
                {
                    byte[] names = new byte[maxlen];
                    br.Read(names, 0, names.Length);
                    fieldNames[i] = ZeroEndByteArrayToString(names);
                }
                br.ReadBytes(padding);

                // read fields
                for (int index = 0; index < mlStruct.M * mlStruct.N; index++)
                {
                    for (int i = 0; i < numOfFields; i++)
                    {
                        // read matrix recursively
                        tag = new ISMatTag(br.BaseStream);
                        if (tag.Size > 0)
                        {
                            MLArray fieldValue = ReadMatrix(br.BaseStream, false);
                            mlStruct[fieldNames[i], index] = fieldValue;
                        }
                        else
                        {
                            mlStruct[fieldNames[i], index] = new MLEmptyArray();
                        }
                    }
                }
                mlArray = mlStruct;
                //br.Close();
                break;

            case MLArray.mxCELL_CLASS:
                MLCell cell = new MLCell(name, dims, type, attributes);
                for (int i = 0; i < cell.M * cell.N; i++)
                {
                    tag = new ISMatTag(buf);
                    if (tag.Size > 0)
                    {
                        MLArray cellmatrix = ReadMatrix(buf, false);
                        cell[i] = cellmatrix;
                    }
                    else
                    {
                        cell[i] = new MLEmptyArray();
                    }
                }
                mlArray = cell;
                break;

            case MLArray.mxDOUBLE_CLASS:
                mlArray = new MLDouble(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <double>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <double>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxSINGLE_CLASS:
                mlArray = new MLSingle(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <float>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <float>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxUINT8_CLASS:
                mlArray = new MLUInt8(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <byte>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <byte>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxINT8_CLASS:
                mlArray = new MLInt8(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <sbyte>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <sbyte>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxUINT16_CLASS:
                mlArray = new MLUInt16(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);

                tag.ReadToByteBuffer(((MLNumericArray <ushort>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <ushort>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxINT16_CLASS:
                mlArray = new MLInt16(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <short>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <short>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxUINT32_CLASS:
                mlArray = new MLUInt32(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);

                tag.ReadToByteBuffer(((MLNumericArray <uint>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <uint>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxINT32_CLASS:
                mlArray = new MLInt32(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <int>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <int>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxUINT64_CLASS:
                mlArray = new MLUInt64(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <ulong>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <ulong>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxINT64_CLASS:
                mlArray = new MLInt64(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                tag.ReadToByteBuffer(((MLNumericArray <long>)mlArray).RealByteBuffer,
                                     (IByteStorageSupport)mlArray);

                // read complex
                if (mlArray.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray <long>)mlArray).ImaginaryByteBuffer,
                                         (IByteStorageSupport)mlArray);
                }
                break;

            case MLArray.mxCHAR_CLASS:
                MLChar mlchar = new MLChar(name, dims, type, attributes);
                //read real
                tag = new ISMatTag(buf);
                char[] ac = tag.ReadToCharArray();
                for (int i = 0; i < ac.Length; i++)
                {
                    mlchar.SetChar(ac[i], i);
                }
                mlArray = mlchar;
                break;

            case MLArray.mxSPARSE_CLASS:
                MLSparse sparse = new MLSparse(name, dims, attributes, nzmax);

                // read ir (row indices)
                tag = new ISMatTag(buf);
                int[] ir = tag.ReadToIntArray();
                // read jc (column indices)
                tag = new ISMatTag(buf);
                int[] jc = tag.ReadToIntArray();

                // read pr (real part)
                tag = new ISMatTag(buf);
                double[] ad1 = tag.ReadToDoubleArray();
                int      n   = 0;
                for (int i = 0; i < ir.Length; i++)
                {
                    if (i < sparse.N)
                    {
                        n = jc[i];
                    }
                    sparse.SetReal(ad1[i], ir[i], n);
                }

                //read pi (imaginary part)
                if (sparse.IsComplex)
                {
                    tag = new ISMatTag(buf);
                    double[] ad2 = tag.ReadToDoubleArray();

                    int n1 = 0;
                    for (int i = 0; i < ir.Length; i++)
                    {
                        if (i < sparse.N)
                        {
                            n1 = jc[i];
                        }
                        sparse.SetImaginary(ad2[i], ir[i], n1);
                    }
                }
                mlArray = sparse;
                break;

            default:
                throw new MatlabIOException("Incorrect Matlab array class: " + MLArray.TypeToString(type));
            }
            return(mlArray);
        }
        /// <summary>
        /// Reads Matrix name.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns><c>string</c></returns>
        private string ReadName( Stream buf )
        {
            string s;

            ISMatTag tag = new ISMatTag( buf );
            char[] ac = tag.ReadToCharArray();
            s = new string(ac);

            return s;
        }
        /// <summary>
        /// Reads <c>miMATRIX</c> from the input stream.
        /// </summary>
        /// <remarks>
        /// If reading was not finished (which is normal for filtered results)
        /// returns <c>null</c>.
        /// </remarks>
        /// <param name="buf">The <c>BinaryReader</c> input stream.</param>
        /// <param name="isRoot">When <c>true</c> informs that if this is a top level
        /// matrix.</param>
        /// <returns><c>MLArray</c> or <c>null</c> if matrix does not match <c>filter</c></returns>
        private MLArray ReadMatrix( Stream buf, bool isRoot )
        {
            // result
            MLArray mlArray = null;
            ISMatTag tag;

            // read flags
            int[] flags = ReadFlags( buf );
            int attributes = (flags.Length != 0 ) ? flags[0] : 0;
            int nzmax = ( flags.Length != 0 ) ? flags[1] : 0;
            int type = attributes & 0xff;

            // read Array dimension
            int[] dims = ReadDimension( buf );

            // read Array name
            string name = ReadName( buf );

            // If this array is filtered out return immediately
            if( isRoot && !_filter.Matches(name) )
            {
                return null;
            }

            // read data
            switch (type)
            {
                case MLArray.mxSTRUCT_CLASS:
                    MLStructure mlStruct = new MLStructure(name, dims, type, attributes);

                    BinaryReader br = new BinaryReader(buf);

                    // field name length - this subelement always uses the compressed data element format
                    tag = new ISMatTag(br.BaseStream);
                    int maxlen = br.ReadInt32();

                    // Read fields data as Int8
                    tag = new ISMatTag(br.BaseStream);
                    // calculate number of fields
                    int numOfFields = tag.Size / maxlen;

                    // padding after field names
                    int padding = (tag.Size % 8) != 0 ? 8 - tag.Size % 8 : 0;

                    string[] fieldNames = new string[numOfFields];
                    for (int i = 0; i < numOfFields; i++)
                    {
                        byte[] names = new byte[maxlen];
                        br.Read(names, 0, names.Length);
                        fieldNames[i] = ZeroEndByteArrayToString(names);
                    }
                    br.ReadBytes(padding);

                    // read fields
                    for (int index = 0; index < mlStruct.M * mlStruct.N; index++)
                    {
                        for (int i = 0; i < numOfFields; i++)
                        {
                            // read matrix recursively
                            tag = new ISMatTag(br.BaseStream);
                            if (tag.Size > 0)
                            {
                                MLArray fieldValue = ReadMatrix(br.BaseStream, false);
                                mlStruct[fieldNames[i], index] = fieldValue;
                            }
                            else
                            {
                                mlStruct[fieldNames[i], index] = new MLEmptyArray();
                            }
                        }
                    }
                    mlArray = mlStruct;
                    //br.Close();
                    break;
                case MLArray.mxCELL_CLASS:
                    MLCell cell = new MLCell(name, dims, type, attributes);
                    for (int i = 0; i < cell.M * cell.N; i++)
                    {
                        tag = new ISMatTag(buf);
                        if (tag.Size > 0)
                        {
                            MLArray cellmatrix = ReadMatrix(buf, false);
                            cell[i] = cellmatrix;
                        }
                        else
                        {
                            cell[i] = new MLEmptyArray();
                        }
                    }
                    mlArray = cell;
                    break;
                case MLArray.mxDOUBLE_CLASS:
                    mlArray = new MLDouble(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).RealByteBuffer,
                        (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).ImaginaryByteBuffer,
                            (ByteStorageSupport)mlArray);
                    }
                    break;
                case MLArray.mxUINT8_CLASS:
                    mlArray = new MLUInt8(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).RealByteBuffer,
                        (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).ImaginaryByteBuffer,
                            (ByteStorageSupport)mlArray);
                    }
                    break;
                case MLArray.mxINT8_CLASS:
                    mlArray = new MLInt8(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).RealByteBuffer,
                        (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).ImaginaryByteBuffer,
                            (ByteStorageSupport)mlArray);
                    }
                    break;
                case MLArray.mxUINT64_CLASS:
                    mlArray = new MLUInt64(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).RealByteBuffer,
                        (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).ImaginaryByteBuffer,
                            (ByteStorageSupport)mlArray);
                    }
                    break;
                case MLArray.mxINT64_CLASS:
                    mlArray = new MLInt64(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).RealByteBuffer,
                        (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        tag.ReadToByteBuffer(((MLNumericArray<byte>)mlArray).ImaginaryByteBuffer,
                            (ByteStorageSupport)mlArray);
                    }
                    break;
                case MLArray.mxCHAR_CLASS:
                    MLChar mlchar = new MLChar(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    char[] ac = tag.ReadToCharArray();
                    for (int i = 0; i < ac.Length; i++)
                    {
                        mlchar.SetChar(ac[i], i);
                    }
                    mlArray = mlchar;
                    break;
                case MLArray.mxSPARSE_CLASS:
                    MLSparse sparse = new MLSparse(name, dims, attributes, nzmax);

                    // read ir (row indices)
                    tag = new ISMatTag(buf);
                    int[] ir = tag.ReadToIntArray();
                    // read jc (column indices)
                    tag = new ISMatTag(buf);
                    int[] jc = tag.ReadToIntArray();

                    // read pr (real part)
                    tag = new ISMatTag(buf);
                    double[] ad1 = tag.ReadToDoubleArray();
                    int n = 0;
                    for (int i = 0; i < ir.Length; i++)
                    {
                        if (i < sparse.N)
                        {
                            n = jc[i];
                        }
                        sparse.SetReal(ad1[i], ir[i], n);
                    }

                    //read pi (imaginary part)
                    if (sparse.IsComplex)
                    {
                        tag = new ISMatTag(buf);
                        double[] ad2 = tag.ReadToDoubleArray();

                        int n1 = 0;
                        for (int i = 0; i < ir.Length; i++)
                        {
                            if (i < sparse.N)
                            {
                                n1 = jc[i];
                            }
                            sparse.SetImaginary(ad2[i], ir[i], n1);
                        }
                    }
                    mlArray = sparse;
                    break;
                default:
                    throw new MatlabIOException("Incorrect Matlab array class: " + MLArray.TypeToString(type));
            }
            return mlArray;
        }
        /// <summary>
        /// Reads Matrix flags.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        /// <returns>Flags int array</returns>
        private int[] ReadFlags(Stream buf)
        {
            ISMatTag tag = new ISMatTag( buf );

            int[] flags = tag.ReadToIntArray();

            return flags;
        }
 /// <summary>
 /// Reads Matrix dimensions.
 /// </summary>
 /// <param name="buf"><c>BinaryReader</c> input stream</param>
 /// <returns>Dimensions int array</returns>
 private int[] ReadDimension( Stream buf )
 {
     ISMatTag tag = new ISMatTag( buf );
     int[] dims = tag.ReadToIntArray();
     return dims;
 }
        /// <summary>
        /// Reads data from the <c>BinaryReader</c> stream. Searches for either
        /// <c>miCOMPRESSED</c> data or <c>miMATRIX</c> data.
        /// </summary>
        /// <remarks>
        /// Compressed data is inflated and the product is recursively passed back
        /// to this same method.
        /// </remarks>
        /// <param name="buf">The input <c>BinaryReader</c> stream.</param>
        private void ReadData( Stream buf )
        {
            // read data
            ISMatTag tag = new ISMatTag(buf);
            switch (tag.Type)
            {
                case MatDataTypes.miCOMPRESSED:
                    // inflate and recur
                    ReadData(Inflate(buf, tag.Size));
                    break;
                case MatDataTypes.miMATRIX:
                    // read in the matrix
                    int pos = (int)buf.Position;
                    int red, toread;

                    MLArray element = ReadMatrix(buf, true);

                    if (element != null)
                    {
                        _data.Add(element.Name, element);
                    }
                    else
                    {
                        red = (int)buf.Position - pos;
                        toread = tag.Size - red;
                        buf.Position = buf.Position + toread;
                    }
                    red = (int)buf.Position - pos;

                    toread = tag.Size - red;

                    if (toread != 0)
                    {
                        throw new MatlabIOException("Matrix was not read fully! " + toread + " remaining in the buffer.");
                    }
                    break;
                default:
                    throw new MatlabIOException("Incorrect data tag: " + tag);

            }
        }