Skip to content

sub2ind no longer accepts NaNs in MATLAB R2024a, relevant to mrSub2ind #65

Description

@austinchkuo

As of R2024a, MATLAB's sub2ind function throws an error when it encounters NaNs, presumably because of new functionality (see: https://www.mathworks.com/help/matlab/ref/sub2ind.html for more information). This function previously did not check for NaNs in prior iterations of MATLAB (R2023b and earlier), and MathWorks' current documentation makes no mention of this additional requirement.

To replicate: open any mrLoadRet session in MATLAB R2024a or later

Error stack:

Error using sub2ind (line 71)
Out of range subscript.

Error in mrSub2ind (line 28)
  linear = sub2ind(dims,x,y,z);

Error in refreshMLRDisplay>getROIImageCoords (line 702)
    baseCoordsLinear = mrSub2ind(baseDims,baseCoordsHomogeneous(1,:),baseCoordsHomogeneous(2,:),baseCoordsHomogeneous(3,:));

Error in refreshMLRDisplay>displayROIs (line 824)
    [x y s] = getROIImageCoords(view,roi{r}.roiBaseCoords,sliceIndex,baseNum,baseCoordsHomogeneous,imageDims);

Error in refreshMLRDisplay>dispBase (line 544)
  [roi,v] = displayROIs(v,hAxis,slice,sliceIndex,baseNum,base.coordsHomogeneous,base.dims,rotate,verbose);

Error in refreshMLRDisplay (line 152)
  [v img base roi overlays curSliceBaseCoords] = dispBase(gui.axis,v,baseNum,gui,true,verbose);

Error in mrOpenWindow (line 207)
    refreshMLRDisplay(view.viewNum);

Error in mrLoadRet (line 51)
v = mrOpenWindow('Volume',mrLastView);

Relevant change from sub2ind:

R2024a: sub2ind accepts mix of scalars and vectors for subscript inputs
sub2ind now leverages scalar expansion and accepts a mix of scalars and vectors for subscript inputs. For example, sub2ind(sz,[1 2 3],2) is now the same as sub2ind(sz,[1 2 3],[2 2 2]). Previously, the subscript inputs were required to be the same size.

Relevant code from sub2ind (lines 69-72):

if ~isempty(v) && (anynan(v) || min(v,[],'all') < 1 || max(v,[],'all') > siz_i)
    % Verify subscripts are within range
    error(message('MATLAB:sub2ind:IndexOutOfRange'));
end

As a result, the functionality of mrSub2ind no longer works in versions of MATLAB starting from R2024a, due to assigning x(badCoords), y(badCoords), and z(badCoords) a value of NaN.

Relevant code from mrSub2ind (lines 21-28):

badCoords = find((x < 1) | (x > dims(1)) | ...
		 (y < 1) | (y > dims(2)) | ...
		 (z < 1) | (z > dims(3)));
x(badCoords) = nan;
y(badCoords) = nan;
z(badCoords) = nan;

linear = sub2ind(dims,x,y,z);

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions