create a mat file that appends structure data using MATLAB C APIs

조회 수: 2 (최근 30일)
Sheikh Imran
Sheikh Imran 2023년 10월 25일
답변: Suman 2024년 7월 25일
Am currently trying to create a C applications that could write a MAT file using the MATLAB C API. Below is the reference code.
I have a structure people inside structure family. and the number of struct family data is dynamic i.e. the size can vary.
Writing a known size of struct family works with simple change in the code in writecomplexstructure function
i want to create a function writecomplexstructure that could write data to matfile for every family struct family member
struct people {
int age;
int phone_number;
int id;
};
struct family {
int flatno;
people family_members[3];
};
void writecomplexstructure(MATFile* mat, int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[], int i, family family_data) {
int flatno, family_members;
static bool once = false;
const char* field_names[] = { "flatno", "family_members" };
mwSize dims[2] = { 1, 1 };
plhs[0] = mxCreateStructArray(2, dims, 2, field_names);
once = true;
/* Create a 1-by-n array of structs. */
flatno = mxGetFieldNumber(plhs[0], "flatno");
family_members = mxGetFieldNumber(plhs[0], "family_members");
mxArray* flatno_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetDoubles(flatno_value) = family_data.flatno;
mxArray* family_value = mxCreateDoubleMatrix(1, 1, mxREAL);
mwSize dims1[2] = { 1,3 };
const char* field_names_2[] = { "age","phone_number","id" };
mxArray* structure_value = mxCreateStructArray(2, dims1, 3, field_names_2);
for (int j = 0; j < 3; j++) {
mxArray* age_value = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray* phone_value = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray* id_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetDoubles(age_value) = family_data.family_members[j].age;
*mxGetDoubles(phone_value) = family_data.family_members[j].phone_number;
*mxGetDoubles(id_value) = family_data.family_members[j].id;
int age_field = mxGetFieldNumber(structure_value, "age");
int phone_number_field = mxGetFieldNumber(structure_value, "phone_number");
int id_field = mxGetFieldNumber(structure_value, "id");
mxSetFieldByNumber(structure_value, j, age_field, age_value);
mxSetFieldByNumber(structure_value, j, phone_number_field, phone_value);
mxSetFieldByNumber(structure_value, j, id_field, id_value);
}
mxSetFieldByNumber(plhs[i], 0, flatno, flatno_value);
mxSetFieldByNumber(plhs[i], 0, family_members, structure_value);
}
int main()
{
MATFile* pmat;
const char* file = "mattest1.mat";
char str[256];
int status;
printf("Creating file %s...\n\n", file);
pmat = matOpen(file, "w");
if (pmat == NULL) {
printf("Error creating file %s\n", file);
printf("(Do you have write permission in this directory?)\n");
return(EXIT_FAILURE);
}
struct family family_data[] = { {200, {{30, 12345, 1},{31, 23456, 2},{0,0,0}}},
{201, {{30, 45235, 1},{0,0,0},{0,0,0}}} ,
{202, {{30, 98253, 1},{31, 11924, 2},{32,10022,0}}},
{203, {{30, 99922, 1},{31, 22299, 2},{0,0,0}}},
{204, {{30, 99922, 1},{31, 22299, 2},{0,0,0}}},
{205, {{30, 99922, 1},{31, 22299, 2},{0,0,0}}} };
//mxArray* phs = mxCreateDoubleMatrix(1, 1, mxREAL);
const mxArray* phr = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray* phs = mxCreateDoubleMatrix(1, 1, mxREAL);
for (int i = 0; i < 4; i++) {
writecomplexstructure(pmat, 1, &phs, 0, &phr, i, family_data[i]);
status = matPutVariable(pmat, "LocalDouble", phs);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n",
__FILE__, __LINE__);
return(EXIT_FAILURE);
}
}
mxDestroyArray(phs);
if (matClose(pmat) != 0) {
printf("Error closing file %s\n", file);
return(EXIT_FAILURE);
}
}
Please help!

답변 (1개)

Suman
Suman 2024년 7월 25일
Hi Imran,
My understanding is that you want to append the family_data values to an existing MAT file. The probem lies with the way you are writing to the MAT file in matPutVariables function call. For each struct in family_data[], since you are using the same name "LocalDouble" in matPutVariable call, it will replace the existing data.
What you can do instead is that fill all of the data in the mxArray, and then write to the file. Or you can use diffrent mxArray name in matPutVariable for each family_data[ i ], if you want to append that data to the MAT file.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by