Error with mex file ... ....Index exceeds matrix dimensions. Index value 3 exceeds valid range [1-2] of array gesgroup

StackOverflow https://stackoverflow.com/questions/23522921

  •  17-07-2023
  •  | 
  •  

Question

I have a function file to mex file. But it is giving error when running the mex file. Following is the code.

In the code I have initialised 3 vectors named group,horgroup,gesgroup and declared gesgroup as varsize.

group = zeros(1,2);    
horgroup = zeros(1,2); 
gesgroup = zeros(1,2); 
coder.varsize('gesgroup');

in few lines group and horgroup are calculated as arrays of size (1 * 2) say group = [1 2] and horgroup = [3 4] later i need to merge group and horgroup into gesgroup using

gesgroup = [group, horgroup];
gesgroup(gesgroup==0) = NaN;

this code is not giving any error while generating a mex file but when I am running the mex file its giving error in above two lines, saying "Index exceeds matrix dimensions. Index value 3 exceeds valid range [1-2] of array gesgroup"

Let me know if I need to change anything in the code and generate the mex file again.

Was it helpful?

Solution

The issue is in initialisation gesgroup is initialised with size 1x2 but the line

gesgroup = [group, horgroup];

will make it 1x4 ( as both group and horgroup are 1x2), you need to initialise it as such or as variable size
e.g.

gesgroup = zeros(1,4); 

or

coder.varsize('gesgroup')

I think the problem has occurred as the size is determined before coder.varsize('gesgroup') is reached, so the array is already fixed size

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top