### Install mat7.3 Library using pip Source: https://github.com/skjerns/mat7.3/blob/main/README.md Instructions for installing the mat7.3 library using pip, including the standard installation and installation from the GitHub repository for the most recent version. ```bash pip install mat73 ``` ```bash pip install git+https://github.com/skjerns/mat7.3 ``` -------------------------------- ### Installation Source: https://context7.com/skjerns/mat7.3/llms.txt Instructions for installing the mat73 library using pip, including installation from GitHub for the development version. ```APIDOC ## Installation Install mat73 using pip. ```bash pip install mat73 ``` For the latest development version, install directly from GitHub: ```bash pip install git+https://github.com/skjerns/mat7.3 ``` ``` -------------------------------- ### Loading MATLAB Sparse Matrices with mat73 Source: https://context7.com/skjerns/mat7.3/llms.txt Shows how to load sparse matrices from MATLAB files, which are converted to scipy.sparse.csc_matrix objects in Python. Includes examples of converting to a dense array and accessing elements. ```python # Sparse matrices (requires scipy) sparse = data['sparse_'] # scipy.sparse.csc_matrix dense = sparse.toarray() # Convert to dense numpy array print(sparse[1, 4]) # Access sparse element print((sparse != 0).sum()) # Count non-zero elements ``` -------------------------------- ### Working with Structs and Nested Data in mat73 Source: https://context7.com/skjerns/mat7.3/llms.txt Provides examples of accessing data within MATLAB structs and struct arrays loaded by mat73. Demonstrates dictionary-like access for simple structs and list-of-dictionaries for struct arrays, including deeply nested structures. ```python import mat73 data = mat73.loadmat('complex_data.mat', use_attrdict=True) # Simple struct - access like a dict struct = data['mystruct'] print(struct['fieldname']) print(struct.fieldname) # With use_attrdict=True # Struct arrays - MATLAB struct(1).field becomes Python struct[0]['field'] structarr = data['structarr_'] for i, item in enumerate(structarr): print(f"Item {i}: f1={item['f1']}, f2={item['f2']}") # Deeply nested structures res = data['Res'] hrv = res['HRV'] param = hrv['Param'] frequency = hrv['Frequency'] statistics = hrv['Statistics'] # Access nested data with attrdict value = data.Res.HRV.Data.RR # numpy array print(value.shape) # e.g., (4564,) # Complex nested cell/struct combinations raw = data['raw1'] h_smooth = raw['HSmooth'] # List of complex arrays for i, arr in enumerate(h_smooth): print(f"Channel {i}: shape={arr.shape}, dtype={arr.dtype}") ``` -------------------------------- ### Enable MATLAB-style attribute access Source: https://context7.com/skjerns/mat7.3/llms.txt Shows how to use the use_attrdict parameter to enable dot notation for accessing dictionary keys, mimicking MATLAB struct syntax. ```python import mat73 data = mat73.loadmat('data.mat', use_attrdict=True) struct = data['structure'] value1 = struct[0]['varname'] value2 = struct[0].varname result = data.data.struct_.test structarr = data.structarr_ first_item = structarr[0] field_value = first_item.f1 keys_var = data['keys'] keys_method = data.keys ``` -------------------------------- ### Load MATLAB 7.3 .mat file with attribute dictionary access Source: https://github.com/skjerns/mat7.3/blob/main/README.md Shows how to load a MATLAB 7.3 .mat file using the `use_attrdict=True` option, which allows accessing struct entries as attributes in addition to dictionary keys. ```python data_dict = mat73.loadmat('data.mat', use_attrdict=True) struct = data_dict['structure'] # assuming a structure was saved in the .mat struct[0].var1 == struct[0]['var1'] # it's the same! ``` -------------------------------- ### Perform selective variable loading Source: https://context7.com/skjerns/mat7.3/llms.txt Demonstrates how to use the only_include parameter to load specific variables or subtrees, improving performance for large files. ```python import mat73 import time data = mat73.loadmat('data.mat', only_include='structure') data = mat73.loadmat('data.mat', only_include=['data', 'keys']) data = mat73.loadmat('data.mat', only_include='Res/HRV/Param') data = mat73.loadmat('data.mat', only_include=['var/subvar/subsubvar', 'tree1/']) start = time.time() data = mat73.loadmat('large_file.mat', only_include='specific_var') selective_time = time.time() - start ``` -------------------------------- ### Load specific variables from MATLAB 7.3 .mat file Source: https://github.com/skjerns/mat7.3/blob/main/README.md Illustrates how to use the `only_include` parameter to load only specific variables or entire directory trees from a MATLAB 7.3 .mat file, which can significantly reduce loading times for large files. ```python data_dict = mat73.loadmat('data.mat', only_include='structure') struct = data_dict['structure'] # now only structure is loaded and nothing else data_dict = mat73.loadmat('data.mat', only_include=['var/subvar/subsubvar', 'tree1/']) tree1 = data_dict['tree1'] # the entire tree has been loaded, so tree1 is a dict with all subvars of tree1 subsubvar = data_dict['var']['subvar']['subsubvar'] # this subvar has been loaded ``` -------------------------------- ### Load MATLAB 7.3 files with loadmat Source: https://context7.com/skjerns/mat7.3/llms.txt Demonstrates the primary function for loading .mat files into Python dictionaries, including handling numeric types and file objects. ```python import mat73 data = mat73.loadmat('data.mat') print(data.keys()) array_data = data['myarray'] string_data = data['mystring'] struct_data = data['mystruct'] arr_double = data['arr_double'] print(arr_double.dtype) arr_int = data['arr_int32'] print(arr_int.dtype) with open('data.mat', 'rb') as f: data = mat73.loadmat(f) ``` -------------------------------- ### Loading MATLAB Numeric and String Types with mat73 Source: https://context7.com/skjerns/mat7.3/llms.txt Demonstrates loading various numeric types (double, single, int, uint) and string/char types from MATLAB files into Python. It shows how these are preserved with their original data types or converted to standard Python types. ```python double_val = data['double_'] # np.float64 single_val = data['single_'] # np.float32 int8_val = data['int8_'] # np.int8 int16_val = data['int16_'] # np.int16 int32_val = data['int32_'] # np.int32 int64_val = data['int64_'] # np.int64 uint8_val = data['uint8_'] # np.uint8 uint16_val = data['uint16_'] # np.uint16 uint32_val = data['uint32_'] # np.uint32 uint64_val = data['uint64_'] # np.uint64 bool_val = data['bool_'] # Python bool arr_bool = data['arr_bool'] # np.array with dtype=bool char_val = data['char_'] # str: 'x' string_val = data['string_'] # str: 'tasdfasdf' char_arr = data['arr_char'] # str: 'test' ``` -------------------------------- ### Error Handling in mat73 for MATLAB File Loading Source: https://context7.com/skjerns/mat7.3/llms.txt Details common error scenarios when loading MATLAB files with mat73, including handling incorrect file formats (pre-7.3), file not found errors, and managing warnings for missing variables or unsupported file extensions. ```python import mat73 import scipy.io # Wrong file format - MATLAB pre-7.3 files try: data = mat73.loadmat('old_matlab_file.mat') except TypeError as e: print(f"Error: {e}") # "old_matlab_file.mat is not a MATLAB 7.3 file. # Load with scipy.io.loadmat() instead." data = scipy.io.loadmat('old_matlab_file.mat') # File not found try: data = mat73.loadmat('nonexistent.mat') except FileNotFoundError: print("File does not exist") # Variable not found warning (with only_include) # Logs warning but returns empty dict for missing vars data = mat73.loadmat('data.mat', only_include='nonexistent_var') # WARNING: Variable "nonexistent_var" was specified to be loaded but could not be found. print(len(data)) # 0 # Loading non-.mat extension files (warns but attempts to load) data = mat73.loadmat('data.npt', verbose=True) # WARNING: Can only load MATLAB .mat file, this file type might be unsupported # Disable verbose warnings data = mat73.loadmat('data.mat', verbose=False) ``` -------------------------------- ### Load MATLAB 7.3 .mat file into Python dictionary Source: https://github.com/skjerns/mat7.3/blob/main/README.md Demonstrates the basic usage of the mat73 library to load a MATLAB 7.3 .mat file into a Python dictionary. This is the primary function for reading the data. ```python import mat73 data_dict = mat73.loadmat('data.mat') ``` -------------------------------- ### Supported MATLAB Data Types Source: https://context7.com/skjerns/mat7.3/llms.txt Information on the automatic conversion of MATLAB data types to their Python/numpy equivalents, ensuring proper dtype preservation. ```APIDOC ## Supported MATLAB Data Types Automatic conversion of MATLAB types to Python/numpy equivalents with proper dtype preservation. ```python import mat73 import numpy as np data = mat73.loadmat('testfile.mat') ``` ``` -------------------------------- ### loadmat with only_include - Selective Variable Loading Source: https://context7.com/skjerns/mat7.3/llms.txt Load only specific variables or variable trees from a .mat file. This option significantly reduces loading time for large files when only certain variables are needed. ```APIDOC ## loadmat with only_include - Selective Variable Loading Load only specific variables or variable trees from a .mat file. Significantly reduces loading time for large files when only certain variables are needed. ```python import mat73 # Load only a single variable data = mat73.loadmat('data.mat', only_include='structure') struct = data['structure'] # Only 'structure' is loaded # Load multiple specific variables data = mat73.loadmat('data.mat', only_include=['data', 'keys']) print(len(data)) # Returns 2 (only two variables loaded) # Load nested variable paths - load specific subvariables data = mat73.loadmat('data.mat', only_include='Res/HRV/Param') param = data['Res']['HRV']['Param'] # Only this path was loaded # Load entire subtrees data = mat73.loadmat('data.mat', only_include=['var/subvar/subsubvar', 'tree1/']) tree1 = data['tree1'] # Entire tree1 dict with all subvars subsubvar = data['var']['subvar']['subsubvar'] # Specific nested var # Performance benefit example import time # Full load - slower for large files start = time.time() data = mat73.loadmat('large_file.mat') full_time = time.time() - start # Selective load - much faster start = time.time() data = mat73.loadmat('large_file.mat', only_include='specific_var') selective_time = time.time() - start print(f"Full: {full_time:.3f}s, Selective: {selective_time:.3f}s") ``` ``` -------------------------------- ### loadmat with use_attrdict - MATLAB-Style Attribute Access Source: https://context7.com/skjerns/mat7.3/llms.txt Enable attribute-style access to dictionary keys, mimicking MATLAB's struct syntax where `struct.field` is equivalent to `struct['field']`. This allows for more intuitive access to nested data structures. ```APIDOC ## loadmat with use_attrdict - MATLAB-Style Attribute Access Enable attribute-style access to dictionary keys, mimicking MATLAB's struct syntax where `struct.field` is equivalent to `struct['field']`. ```python import mat73 # Load with attribute dictionary enabled data = mat73.loadmat('data.mat', use_attrdict=True) # Access nested structures using MATLAB-like dot notation struct = data['structure'] # These are equivalent: value1 = struct[0]['varname'] value2 = struct[0].varname # Same result, MATLAB-style! # Works for deeply nested structures result = data.data.struct_.test # Access nested fields # Equivalent to: data['data']['struct_']['test'] # Access array of structs structarr = data.structarr_ first_item = structarr[0] field_value = first_item.f1 # Access field 'f1' of first struct # Note: Built-in dict methods like keys(), pop() cannot be overwritten # Use bracket notation for variables named after dict methods keys_var = data['keys'] # Access variable named 'keys' keys_method = data.keys # Returns the dict keys() method ``` ``` -------------------------------- ### loadmat - Load MATLAB 7.3 Files Source: https://context7.com/skjerns/mat7.3/llms.txt The primary function for loading MATLAB 7.3 HDF5 files into a Python dictionary. It automatically converts MATLAB types to appropriate Python/numpy equivalents. ```APIDOC ## loadmat - Load MATLAB 7.3 Files The primary function for loading MATLAB 7.3 HDF5 files into a Python dictionary. Automatically converts MATLAB types to appropriate Python/numpy equivalents. ```python import mat73 # Basic usage - load entire .mat file data = mat73.loadmat('data.mat') # Access variables from the loaded dictionary print(data.keys()) # List all variables in the file array_data = data['myarray'] # Access numpy array string_data = data['mystring'] # Access string struct_data = data['mystruct'] # Access struct as dict # Example with numeric data types # MATLAB: arr = [1.1, 1.2, 0.3]; % double array # Python result: arr_double = data['arr_double'] # Returns np.float64 array print(arr_double.dtype) # np.float64 # MATLAB: arr_int = int32([1, 2, 3]); # Python result: arr_int = data['arr_int32'] # Returns np.int32 array print(arr_int.dtype) # np.int32 # Load from file object instead of path with open('data.mat', 'rb') as f: data = mat73.loadmat(f) ``` ``` -------------------------------- ### Loading MATLAB Complex, Cell, and Struct Types with mat73 Source: https://context7.com/skjerns/mat7.3/llms.txt Illustrates loading complex numbers, cell arrays, and structs from MATLAB files. Cell arrays are converted to Python lists, and structs are converted to dictionaries, preserving nested data structures. ```python complex_val = data['complex_'] # np.complex128: (2+3j) cell = data['cell_'] print(cell[0]) # np.array([1.1, 2.2]) print(cell[5]) # 'test' print(cell[6]) # ['subcell', 0] - nested cell struct = data['struct_'] print(struct['test']) # [1, 2, 3, 4] structarr = data['structarr_'] # Array of structs print(structarr[0]) # {'f1': ['some text'], 'f2': ['v1']} print(structarr[1]['f2']) # ['v2'] nan_val = data['nan_'] # np.nan missing_val = data['missing_'] # None empty_arr = data['empty_'] # [] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.