0

I have Matlab code that is generating an array of variable size depending on some input parameter, N. Dimensions 1 and 2 are fixed, and the array's size in the 3rd dimension is N, i.e. size(A) = [x, y, N]. N can take integer values from 1 upwards.

Thus in the case N=1, I am indexing into the array as follows:

B = A(:, :, 1)

Here A reduces to a 2D matrix, but Matlab can handle indexing at 1 into higher dimensions.

Once this code is compiled using the Matlab Compiler SDK, running the executable causes an error, as it seems the runtime cannot handle indexing at 1 into dimension 3. Printing size(A) gives [x, y, 0] and thus a badsubscript error.

I guess I could add logic to handle indexing into A in the case that N=1, but that seems like a cludge as N=1 isn't exactly a special case in any other context. But I was hoping there was a better way to do this?

  • If `size(A)` gives `[x, y, 0]` then `N` is 0, not 1, and you're indexing out of bounds. – Cris Luengo Aug 11 '21 at 19:50
  • What's the difference then between an array with size [x, y, 0] and [x, y, 1]? In matlab, printing size(A) just gives [x, y] when N = 1, but I could equally index A(x, y, 1, 1, 1, 1, ...) and matlab would read that as valid. It's only in the compiled code that the size is printed as [x, y, 0], and now the behaviour is different in that when N = 1, trying to index A(x, y, N) goes out of bounds. Is there a fix to maintain the original (standard matlab) behaviour in the compiled code, or do I have to include specific handling to only index in the 3rd dimension if N > 1? – TheSuperLemming Aug 12 '21 at 11:46
  • 1
    I don’t know why your array is different in the compiled code. But think of how many elements fit in an array: [x,y] or [x,y,1,1,1] has x\*y elements, [x,y,0] has x\*y\*0=0 elements. – Cris Luengo Aug 12 '21 at 12:17
  • 1
    There’s definitely something different about how you run your code. You will need to debug. The error happens before the point where MATLAB complains about illegal indexing. The actual error happens when you create the array. – Cris Luengo Aug 12 '21 at 12:19
  • 1
    You say `N` is an input parameter. Are you sure it’s being inputted correctly? Print out the value after input, to verify that step happens as you expect. – Cris Luengo Aug 12 '21 at 12:21
  • Thanks Cris, there was an error upstream that was causing the array to be initialised with size [x, y, 0] rather than [x, y, N]. I've corrected this and the indexing now works as expected. – TheSuperLemming Aug 12 '21 at 17:42

0 Answers0