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 7812521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:32:07+00:00 2026-06-02T04:32:07+00:00

I am currently writing a MEX function that will have to work with a

  • 0

I am currently writing a MEX function that will have to work with a cell array in MATLAB. The MEX file is written in C.

Essentially, the input to my function will be a cell array where each entry is a numeric matrix with real values. A simple example is:

C = cell(1,2);
C{1} = ones(10,10);
C{2} = zeros(10,4);

I would like to be able to access the numeric arrays C{1} and C{2} in my MEX file. Ideally, I would like to do this without having to create a second copy of the data in my MEX File (i.e. get pointers for them).

Using the previous example, my current approach is as follows:

/* declare a pointer variable to the incoming cell array after it is passed to the MEX function */
mxArray C_CELL = (mxArray *) mxGetData(prhs[0]) 

/* declare  a 2 x 1 array of pointers to access the cell array in C */
double *myarray[2] //

/* point towards the contents of C_CELL */
myarray[0] = mxGetPr(C_CELL[0])
myarray[1] = mxGetPr(C_CELL[1])

Unfortunately this seems to yield “invalid use of undefined type ‘struct mxArray_tag'” errors.

  • 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-02T04:32:08+00:00Added an answer on June 2, 2026 at 4:32 am

    Lots of issues in that code sample:

    • You’re missing trailing semicolons on every single line
    • mxArrays must be defined as pointers: e.g., mxArray *C_CELL.

    But based upon the error message you’re getting, I suppose you have those items correct in the source you’re using.

    Your main problem is that you’re assigning the output of mxGetData to an mxArray *. The way mxGetData works is that it returns a raw (void *) pointer to your matrix contents. In this case, your matrix contains an array of mxArray pointers, with two levels of pointer redirection.

    As such, C_CELL must be defined as mxArray **C_CELL. Note that doing this without any sanity checks or bound checking will rapidly lead to segfaults. Using mxGetCell as @David Heffernan suggests is a safer and more surefire way to do this (assuming you’ve checked that your prhs[0] contains a cell array).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a MEX file in C to speed up MATLAB code that
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in
I am currently writing a simple, timer-based mini app in C# that performs an
I'm currently writing a simple .sh script to parse an Exim log file for
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
I'm currently writing some general purpose .net libraries that contain usual helper classes. For
I'm currently writing an application that allows one to store images, and then tag
I'm currently writing a application that needs to capture some images. I start the
I am currently writing a distributed system using hazelcast. I have a few questions
Background I am currently writing a function which needs to accept a command as

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.