Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
problem with function mxGetFieldByNumber

Subject: problem with function mxGetFieldByNumber

From: Toan Cao

Date: 5 Sep, 2013 21:47:06

Message: 1 of 1

I have a structure array of N =800 elements, each element is a structure of 4 fields. Each field is a 1D array. Third field of structure stores indices of neighbor structure elements of current structure. When i run this following code, Matlab is crashed.
#define p_graph prhs[1] // input structure array
mxArray *p_neig_inds, *p_gk;
double *neig_inds, *gk;
int nei_ind, test ;

for(k=0; k<800; k++){
     // Obtain indices of neighbor elements in third field of current structure k
     p_neig_inds = mxGetFieldByNumber(p_graph,k,2);
     neig_inds = (double *)mxGetData(p_neig_inds);
     // Get index of first neighbor element of current structure k
     nei_ind = neig_inds[0]; // Warning here for conversion from double to int
     // Get first field of first neighbor element
     p_gk = mxGetFieldByNumber(p_graph,nei_ind,0); // Error occurs here because of
                                                                          // variable "nei_ind"
     gk = (double *)mxGetData(p_gk);
I detect that Matlab is crashed because the variable "nei_ind" in function "mxGetFieldByNumber" is related to variable "neig_inds[0]". So, when i replace the "nei_ind" by a integer number (for example, current iteration : "k"), the problem does not occur.
When i run above code for small number of iteration (k<400), it works. Therefore, i think the problem relates to memory occupation, but i dont have a solution.
Can you give me some suggestions for this problem.
Thanks in advance

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us