mxArray dynamic memory allocation

1 view (last 30 days)
Fabian Jonsson
Fabian Jonsson on 19 Dec 2013
I want to read a struct (called ROAD) from a .mat file and create a map<string,double> from it so that the map is a reflection of the ROAD. The fields in the Matlab struct are arrays of different lengths, and this is the current state of the code.
I have a string vector called ROAD_fields that are just a list of the field names of the matlab struct. I also have a map<string,double> called ROAD that I want to fill.
std::vector<std::string> *ROAD_fields;
std::map<std::string, double> ROAD;
Here is the implementation so far:
void Road::getRoadFromFile() {
MATFile *file = matOpen(this->filename.c_str(),"r");
mxArray *roadstruct = matGetVariable(file, "ROAD");
int i;
mxArray* field;
for(i=0; i<this->ROAD_fields->size(); i++) {
field = mxGetFieldByNumber(roadstruct,0,i);
this->ROAD[this->ROAD_fields->at(i).c_str()] = this->extractField(field);
}
};
double Road::extractField(mxArray *ptr) {
return *(double*) mxGetPr(ptr);
};
I get an access violation in the extractField call and I guess it's due to the fact that the arrays have different lengths? I think I need to reallocate memory somehow, but I haven't been able to figure out how.
Please help!

Answers (0)

Community Treasure Hunt

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

Start Hunting!