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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:43:47+00:00 2026-06-09T23:43:47+00:00

I’m trying to populate a structure in a mex function. I have the basics

  • 0

I’m trying to populate a structure in a mex function. I have the basics of the structure created in the mexFunction, but its getting a little confusing when a field in the structure is an array and i want to populate each element in this array as the ‘for’ loop iterates.

....
mxArray *value;
mwSize dims[2] = {16,8};
int numFields = 2;  
const char *fieldNames[] = {"array1", "array2"}; 
plhs[2] = mxCreateStructArray(2, dims, numFields, fieldNames);

for (int i = 0; i < 16; i++)
{
    for (int j = 0; j < 8; j ++)
    {
        value1 = (some calculation);
        value = mxCreateDoubleMatrix(1,18,mxREAL); 
        *mxGetPr(value[sampleIndex]) = value1;
        mxSetField(plhs[2], i, "array1", value);
        // i want to set the array1[j] element

        value2 = (some other calculation);
        value = mxCreateDoubleMatrix(1,8,mxREAL); 
        *mxGetPr(value) = value2;
        mxSetField(plhs[2], i, "array2", value);


    }
}
....

The fields “array1” and “array2” are both vectors of the same size (lets say 8 elements) i am not sure how to populate each individual element. How do i set the field name by array index ‘j’.

To aid in the explanation, this is how the structure should look:
structure is an array of 16 elements, each element has 2 fields, each field has 8 elements each

structure(0).array1 = [1 2 3 4 5 6 7 8];
structure(0).array2 = [11 12 13 14 15 16 17 18];

structure(1).array1 = [21 22 23 24 25 26 27 28];
structure(1).array2 = [211 212 213 214 215 216 217 218];

structure(2).array1 = [31 32 33 34 35 36 37 38];
structure(2).array2 = [311 312 313 314 315 316 317 318];

structure(3).array1 = [41 42 43 44 45 46 47 48];
structure(3).array2 = [411 412 413 414 415 416 417 418];
...

The values are just arbitrary.

  • 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-09T23:43:49+00:00Added an answer on June 9, 2026 at 11:43 pm

    Consider the following C code:

    structArrayMEX.c

    #include "mex.h"
    
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {   
        mxArray *x, *y;
        double *xData, *yData;
        mwIndex idx, i;
        int counter = 1;
    
        // create a 16x1 structure array, each with two fields x and y
        const char *fieldNames[] = {"x", "y"}; 
        mxArray *s = mxCreateStructMatrix(16, 1, 2, fieldNames);
    
        // fill structure array
        for (idx=0; idx<16; idx++) {
            // create x and y matrices each of size 1x8
            x = mxCreateDoubleMatrix(1, 8, mxREAL);
            y = mxCreateDoubleMatrix(1, 8, mxREAL);
    
            // fill x and y matrices: x[i], y[i]
            xData = mxGetPr(x);
            yData = mxGetPr(y);
            for (i=0; i<8; i++) {
                xData[i] = counter;
                yData[i] = counter + 100;
                counter++;
            }
    
            // set fields: s(idx).x = x and s(idx).y = y
            mxSetField(s, idx, "x", x);
            mxSetField(s, idx, "y", y);
        }
    
        // return structure array
        plhs[0] = s;
    }
    

    The returned structure array:

    >> s = structArrayMEX();
    >> whos s
      Name       Size            Bytes  Class     Attributes
    
      s         16x1              4096  struct         
    
    >> s(1)
    ans = 
        x: [1 2 3 4 5 6 7 8]
        y: [101 102 103 104 105 106 107 108]
    >> s(16)
    ans = 
        x: [121 122 123 124 125 126 127 128]
        y: [221 222 223 224 225 226 227 228]
    

    The above MEX-function should be equivalent to the following MATLAB code

    vals = reshape(1:16*8, [8 16])';
    s = struct('x',num2cell(vals,2), 'y',num2cell(vals+100,2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to select an H1 element which is the second-child in its group
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.