Skip to content

Conversation

BenBrock
Copy link
Contributor

Implement Matlab Bindings for Binsparse

This PR adds Matlab bindings, supporting (hopefully) Matlab and Octave. These add the functions binsparse_read and binsparse_write, which read and write, respectively, sparse matrices to or from Binsparse files.

This PR returns matrices as a "native Matlab struct," which contains the component arrays that compose the sparse matrix. Is does not convert to or from a Matlab sparse matrix. (We probably want that, but it should be done at the Matlab layer.)

Below are some AI-generated notes on what's changed:

New Features:

Core MEX Functions:

  • binsparse_read.c - Read Binsparse matrix files into MATLAB structs
  • binsparse_write.c - Write MATLAB structs to Binsparse format files
  • Complete type support - All Binsparse types including complex numbers (BSP_COMPLEX_FLOAT32/64)
  • Full API coverage - Optional groups, JSON metadata, compression levels

Build Infrastructure:

  • build_matlab_bindings.m - Main MATLAB build script with auto-detection
  • build_octave_bindings.m - Main Octave build script using mkoctfile
  • compile_octave.sh - Command-line shell script for Octave compilation
  • Individual compilation scripts for both MATLAB and Octave

Utilities & Testing:

  • bsp_matrix_create.m - Helper for creating matrix structs
  • bsp_matrix_info.m - Matrix information display utility
  • test_binsparse_read.m / test_binsparse_write.m - Comprehensive test suites
  • README.md - Complete documentation with usage examples

API:

% Reading
matrix = binsparse_read('file.bsp.h5');
matrix = binsparse_read('file.bsp.h5', 'group_name');

% Writing  
binsparse_write('output.bsp.h5', matrix);
binsparse_write('output.bsp.h5', matrix, 'group', '{"meta": "data"}', 6);

Key Technical Features:

  • Efficient type conversion - Direct memcpy for compatible types, proper complex number handling
  • Memory management - Safe allocation/cleanup with comprehensive error handling
  • Cross-platform - Works on Windows, macOS, Linux with both MATLAB and Octave
  • Round-trip compatibility - Perfect read → write → read workflows

Configuration Updates:

  • Updated .gitignore for MATLAB/Octave artifacts
  • Modified .pre-commit-config.yaml to exclude MATLAB files from clang-format
  • Enhanced CMakeLists.txt for bindings support

This provides a complete, production-ready interface for MATLAB/Octave users to work with Binsparse matrices, enabling seamless integration into scientific computing workflows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant