Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:22:03+00:00 2026-06-04T08:22:03+00:00

I have been confused by a very strange mex error just now . .

  • 0

I have been confused by a very strange mex error just now . . .

Boiling my problem right down to its core, we end up with the following simple mex code. It just displays if given structure fields are empty or not …

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{    
    int numElements  = mxGetNumberOfElements(prhs[0]);
    int numFields = mxGetNumberOfFields(prhs[0]);

    mxArray* tmpData;
    const char* tmpName;

    for (int structIdx=0; structIdx<numElements; ++structIdx)
    {
        for (int fieldIdx=0; fieldIdx<numFields; ++fieldIdx)
        {
            tmpData = mxGetFieldByNumber(prhs[0], structIdx, fieldIdx);
            tmpName = mxGetFieldNameByNumber(prhs[0], fieldIdx);

            if (mxIsEmpty(tmpData))
                mexPrintf("struct(%i).%s is empty\n", structIdx+1, tmpName );
            else
                mexPrintf("struct(%i).%s contains data\n", structIdx+1, tmpName );
        }
    }    
}

If we compile this code and call it structcrash then the following matlab code . .

clc
x.a=1;
x.b=2;
x(2).a=3;
x(2).b=4;

structcrash(x);

…gives the output we might expect…

  • struct(1).a contains data
  • struct(1).b contains data
  • struct(2).a contains data
  • struct(2).b contains data

If we give the mex function a structure containing an empty field like so…

clc
y.a = [];
structcrash(y);

… then we also get the expected output …

  • struct(1).a is empty

Now, things get very strange if you use code like this …

clc
y(2).b = 4;
structcrash(y);

If we inspect the y structure, is is now a 2 element structure with 2 fields in each element. y(1).a is empty as we specified above, and y(1).b has been automatically created and given an empty value when we added the b field. Similarly, y(2).a was automatically created when we increased the structure size by adding y(2).b. The structure looks perfectly logical, however using as an input to the mex file results in a segfault.

By selectively commenting-out various lines of code, I can confirm that the command that causes the segfault is mxIsEmpty(tmpData).

Can anyone else replicate this error and am I doing something fundamentally wrong here? It looks like a bug in the mex API code to me, but I wanted to check here first. Thanks

EDIT: Based on @David Heffernan’s advice I modified the code as follows

        if(tmpData!=NULL) {
            if (mxIsEmpty(tmpData))
                mexPrintf("struct(%i).%s is empty\n", structIdx+1, tmpName );
            else
                mexPrintf("struct(%i).%s contains data\n", structIdx+1, tmpName );
        }

…and the segfault no longer occurs. However, this is still very ominous. If you create two structures like in the following example and examine them using the workspace view, f and g look absolutely identical in every way. I cannot find any way in which they differ using standard matlab programming commands.

>> f(2).a=123;
>> g(1).a=[];
>> g(2).a=123

… but the whos command reveals a difference …

  Name      Size            Bytes  Class     Attributes

  f         1x2               192  struct              
  g         1x2               296  struct 

… and my updated mex function obviously does too …

>>structcrash(f)
struct(2).a contains data
>> structcrash(g)
struct(1).a is empty
struct(2).a contains data

So the moral of this story is that the Matlab IDE makes structs look nice and square by inserting fields in all structs when you insert a new field into a particular struct element. However, in reality, in the underlying memory, this is not the case.

Beware!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:22:05+00:00Added an answer on June 4, 2026 at 8:22 am

    What is happening is that mxGetFieldByNumber is returning NULL which you then pass to mxIsEmpty and so produce the seg fault. The documentation states that mxGetFieldByNumber returns NULL if there is no value assigned to the specified field.

    To solve this you will need to guard against passing NULL to mxIsEmpty:

    if (tmpData == NULL || mxIsEmpty(tmpData))
        mexPrintf("struct(%i).%s is empty\n", structIdx+1, tmpName);
    else
        mexPrintf("struct(%i).%s contains data\n", structIdx+1, tmpName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always been kind of confused by threads, and my class right now
I am very confused right now. I have VS2008 at my job right now
We have been discussing this at work and now I am even more confused
I have been very confused on how to handle the releasing of an NSMutableArray
I'm sure this would have been asked before, but I'm very confused! Say I
I have been trying to learn multi-threaded programming in C# and I am confused
I'm confused about this. Most of us have been told that there isn't any
So I am a little confused, I have been looking around trying to determine
As the question states. The best example of this I have right now is
I have been trying to use OleDbDataAdapter to update a DataTable but got confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.