Skip to content

Conversation

@dimitri-yatsenko
Copy link
Member

Summary

Fix read_cell_array to handle edge cases from MATLAB that caused ValueError: cannot reshape array errors.

Problem

MATLAB cell arrays can contain:

  • Empty elements: {[], [], []}
  • Nested arrays of different sizes: {[1,2,3], [4,5,6,7,8]}
  • Mixed empty/non-empty elements

The previous implementation used np.array(result).reshape(shape) which fails because:

  1. NumPy can't create uniform arrays from ragged data
  2. Empty arrays have size 0 and can't reshape to (3,1)

Solution

Use dtype='object' to avoid NumPy's array homogeneity requirements:

# Before (fails on ragged arrays)
return np.array(result).reshape(shape, order="F")

# After (handles all cases)
arr = np.empty(n_elem, dtype=object)
arr[:] = result
return arr.reshape(shape, order="F")

Test plan

Closes #1056
Closes #1098


🤖 Generated with Claude Code

Fix read_cell_array to handle edge cases from MATLAB:
- Empty cell arrays ({})
- Cell arrays with empty elements ({[], [], []})
- Nested/ragged arrays ({[1,2], [3,4,5]})
- Cell matrices with mixed content

The fix uses dtype='object' to avoid NumPy's array homogeneity
requirements that caused reshape failures with ragged arrays.

Closes #1056
Closes #1098

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@github-actions github-actions bot added bug Indicates an unexpected problem or unintended behavior enhancement Indicates new improvements labels Jan 9, 2026
@dimitri-yatsenko dimitri-yatsenko merged commit 1aa68c0 into pre/v2.0 Jan 9, 2026
8 checks passed
@dimitri-yatsenko dimitri-yatsenko deleted the fix/1056-1098-matlab-cell-array branch January 9, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Indicates an unexpected problem or unintended behavior enhancement Indicates new improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants