Skip to content

pop_importbids fails silently or crashes when input bidsfolder is a MATLAB string object (requires char) #246

Description

Description:

The function pop_importbids fails to correctly locate or parse datasets when the input path bidsfolder is provided as a MATLAB string (double quotes "..."), which is the default text type in modern MATLAB versions. It only functions correctly when the input is a char array (single quotes '...').

The current documentation header describes the input simply as [string], which is ambiguous in the context of modern MATLAB (where it usually implies the string class, but legacy code expects char).


Steps to Reproduce:

Provide a valid BIDS folder path as a double-quoted string.

Run pop_importbids.

% Assume valid BIDS folder at this path, we can clone one from OpenNeuro
bidsPath = "C:\data\my_bids_dataset"; % MATLAB string object

% This fails (crashes or returns 'No dataset found')
[STUDY, ALLEEG] = pop_importbids(bidsPath);

% This works
[STUDY, ALLEEG] = pop_importbids(char(bidsPath));

Root Cause Analysis:

The crash occurs due to legacy indexing logic that assumes character array behavior. Specifically, around line 605 inside the file loop:

% Existing code assumes eegFileRaw is char
infoData = bids_importjson([ eegFileRaw(1:end-8) '_' modality '.json' ], ...);
  • If char: eegFileRaw(1:end-8) correctly slices the filename string (removing the last 8 characters).

  • If string: eegFileRaw is a scalar string object (1x1). end evaluates to 1. The range 1:end-8 becomes 1:-7 (empty). This results in an empty array being passed to bids_importjson, causing downstream errors (e.g., "Index exceeds array elements" or file not found).


Suggested Fix:

  1. Documentation Update (Low Effort) Explicitly specify in the help section that inputs must be character arrays.
% Inputs:
%   bidsfolder - [char array] Path to the BIDS folder (e.g., 'C:\data\exp'). 
%                Do not use MATLAB string objects ("...").
  1. Robust Input Handling (Recommended) Add a simple cast at the beginning of pop_importbids to ensure compatibility with modern MATLAB scripts without requiring user intervention:
if nargin > 0
    bidsfolder = char(bidsfolder);
end

Additional Context on Type Dependency:

While this report focuses on pop_importbids, it is worth noting that string vs char incompatibilities appear to be systemic within the ecosystem invoked here. For example, if we write eegFileRaw = char(eegFileRaw) at line 604 (to fix the line 605 crash), downstream functions like pop_saveset may subsequently fail due to strict type checks (e.g., strcmpi failures on string properties). Explicit input sanitization (casting to char) at the entry point of pop_importbids would effectively shield the internal logic from these external type mismatches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions