Created nested structures using the Matlab API

2 views (last 30 days)
Jon
Jon on 25 Sep 2013
Commented: Jon on 26 Sep 2013
I'm currently working on a program that writes output data into an ASCII file, and then we later execute a Matlab command in batch mode to turn the ASCII files into .mat files. This takes a lot of time, and I have read about using the Matlab API libraries to create the .mat files for you. I've been somewhat successful with this as I've been able to create a .mat file full of scalar values, but have not been able to figure out nested structures. I know I can create a single structure with fieldnames, but then those fieldnames are expected to be a matrix or scalar. I would like to create a structure within that structure. For example, a structure called part, with fieldnames of gaussian and uniform, each of those with fieldnames of x and y, and each of those with fieldnames of stdDev and mean. I would greatly appreciate any help as I'm really stuck right now. Thanks!
  4 Comments
James Tursa
James Tursa on 25 Sep 2013
Edited: James Tursa on 26 Sep 2013
OK. I presume your data is in the form of native C/C++ memory (i.e., arrays or dynamically allocated memory using malloc and friends). Is this true? Would it be possible to change your code to use MATLAB Memory Manager memory instead (i.e., allocated using API functions like mxMalloc, mxCreateDoubleMatrix, etc)? That would make the mat file writing a lot easier.
Also, this statement you made is not true: "... those fieldnames are expected to be a matrix or scalar ..." The field elements can be any mxArray variable, including struct arrays or cell arrays or whatever. They are not restricted to be a numeric matrix or scalar.
The basic procedure is to simply link to the appropriate API libraries (I simply build as an Engine application for stuff like this even though I don't actually open up an Engine since this process will get the necessary libraries linked automatically) and then call the mx and mat functions you need.
Jon
Jon on 26 Sep 2013
This simulation is very complex, so what the users before me did is when the variable is initialized they saved off in a structure the name of that variable (structures and all i.e., "part.gaussian.y.stdDev"), the memory address, and its type.
What I have done so far is use matOpen to open up a .mat file. I then use a switch across the type provided as an input, and based on the variable type I read that memory address location and save it to an mxArray using mxCreateDoubleScalar. I assume this is what you are talking about in your first paragraph with MATLAB's Memory manager. I'm quite new to this so feel free to correct me. I'm currently in the process of writing a routine to parse the name string to create the structures I need using mxCreateStructArray.
I'm using the MATLAB C/C++ and Fortran API Reference guide as a reference for the available functions. When I was going through the guide and found the mxCreateStructArray is when I interpreted the the statement I made as far as the fieldnames being expected to be a matrix or scalar. I just don't see how you can create a structure within a structure using this routine. Is there another one that I'm not seeing?
If you have another suggestion on how to approach this problem I am very open because this is my first time trying to use these API libraries.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!