{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "46815371",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "893f9c61",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from os.path import dirname, join as pjoin\nimport scipy.io as sio",
            "outputs": []
        },
        {
            "id": "461f0016",
            "cell_type": "markdown",
            "source": "Get the filename for an example .mat file from the tests/data directory.\n",
            "metadata": {}
        },
        {
            "id": "7081f9f2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data')\nmat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')",
            "outputs": []
        },
        {
            "id": "eda18e2f",
            "cell_type": "markdown",
            "source": "Load the .mat file contents.\n",
            "metadata": {}
        },
        {
            "id": "edbed248",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mat_contents = sio.loadmat(mat_fname, spmatrix=False)",
            "outputs": []
        },
        {
            "id": "c890257c",
            "cell_type": "markdown",
            "source": "The result is a dictionary, one key/value pair for each variable:\n",
            "metadata": {}
        },
        {
            "id": "30b186e4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sorted(mat_contents.keys())",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "['__globals__', '__header__', '__version__', 'testdouble']"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5426ab75",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mat_contents['testdouble']",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.        , 0.78539816, 1.57079633, 2.35619449, 3.14159265,\n        3.92699082, 4.71238898, 5.49778714, 6.28318531]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "df635fe6",
            "cell_type": "markdown",
            "source": "By default SciPy reads MATLAB structs as structured NumPy arrays where the\ndtype fields are of type `object` and the names correspond to the MATLAB\nstruct field names. This can be disabled by setting the optional argument\n`struct_as_record=False`.\n\nGet the filename for an example .mat file that contains a MATLAB struct\ncalled `teststruct` and load the contents.\n",
            "metadata": {}
        },
        {
            "id": "e0f0583c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat')\nmatstruct_contents = sio.loadmat(matstruct_fname)\nteststruct = matstruct_contents['teststruct']\nteststruct.dtype",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "dtype([('stringfield', 'O'), ('doublefield', 'O'), ('complexfield', 'O')])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f8c3514f",
            "cell_type": "markdown",
            "source": "The size of the structured array is the size of the MATLAB struct, not the\nnumber of elements in any particular field. The shape defaults to 2-D\nunless the optional argument `squeeze_me=True`, in which case all length 1\ndimensions are removed.\n",
            "metadata": {}
        },
        {
            "id": "83565331",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "teststruct.size",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "1"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "cd6f9872",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "teststruct.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(1, 1)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2d700ce8",
            "cell_type": "markdown",
            "source": "Get the 'stringfield' of the first element in the MATLAB struct.\n",
            "metadata": {}
        },
        {
            "id": "b245a584",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "teststruct[0, 0]['stringfield']",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array(['Rats live on no evil star.'],\n  dtype='<U26')"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "48eeccce",
            "cell_type": "markdown",
            "source": "Get the first element of the 'doublefield'.\n",
            "metadata": {}
        },
        {
            "id": "a6cfc258",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "teststruct['doublefield'][0, 0]",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 1.41421356,  2.71828183,  3.14159265]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "008861bb",
            "cell_type": "markdown",
            "source": "Load the MATLAB struct, squeezing out length 1 dimensions, and get the item\nfrom the 'complexfield'.\n",
            "metadata": {}
        },
        {
            "id": "d54959be",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True)\nmatstruct_squeezed['teststruct'].shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "()"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1c9e1d1b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "matstruct_squeezed['teststruct']['complexfield'].shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "()"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "97b12c61",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "matstruct_squeezed['teststruct']['complexfield'].item()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1.41421356+1.41421356j,  2.71828183+2.71828183j,\n    3.14159265+3.14159265j])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}