Hi Mahendrababu,
The programming language C, typically uses file pointers to keep track of the current position in the file and manage the end-of-file (EOF) condition in usual I/O operations whereas, MAT files use mxArray structures to manage data in memory before writing it to the .MAT file in a single operation:
Here a few workarounds that could be helpful in resolving the issue that you are facing:
1. Writing Data Dynamically to a MAT File
Typically, you need to have all your data in a buffer before writing it to a MAT file using the MAT API. However, one way to handle data dynamically is to incrementally write data by maintaining a buffer that grows as new data comes in. Here is an example approach:
- Include libraries for matlab API, mantain pointers for open mat file and data output as MATFile and mxArray, and initialize output data array with its current size as buffer_size:
int buffer_size = 10; // Initial buffer size
pmat = matOpen("example.mat", "w");
mx_field_value = mxCreateDoubleMatrix(buffer_size, 1, mxREAL);
buff = mxGetPr(mx_field_value);
- To avoid using pre-allocation of data before writing to MAT file in a single operation, size of output data array can be doubled every time item count count_MAT exceeds current size buffer_size of array, pointed by mx_field_value.
// Function to add data dynamically
void addData(double new_data) {
if (count_MAT >= buffer_size) {
// Resize buffer if necessary
// Set new size of output data array
mxSetM(mx_field_value, buffer_size);
// Get new pointer to output data array
buff = mxGetPr(mx_field_value);
// Add new data to output array
buff[count_MAT] = new_data;
- An example usage, for writing 200 values is described below, where the output data array is resized again and finally trimmed to actual count of data elements in it.
for (int i = 0; i < 200; i++) {
// Trim the buffer to actual data size
mxSetM(mx_field_value, count_MAT);
- Finally write to MAT file using pointer pmat, and release output data array and MAT file pointers from heap memory.
matPutVariable(pmat, "count", new_data);
mxDestroyArray(mx_field_value);
2. Appending Data to an Existing MAT File
Appending data to an existing MAT file while maintaining the same columns and rows can be tricky because the MAT API does not directly support appending data. Instead, you need to read the existing data, append the new data, and then write it back. Here's an example:
- Similar to first step of previous section, starting by initializing pointers to existing and new data along with open MAT file. Then, iterating over final data new_buff add both existing and new values, before writing to MAT file as discussed earlier.
mxArray *existing_data, *new_data;
double *existing_buff, *new_buff;
int existing_rows, new_rows;
// Open MAT file for reading and writing
existing_data = matGetVariable(pmat, "count");
existing_buff = mxGetPr(existing_data);
existing_rows = mxGetM(existing_data);
new_data = mxCreateDoubleMatrix(existing_rows + new_rows, 1, mxREAL);
new_buff = mxGetPr(new_data);
for (int i = 0; i < existing_rows; i++) {
new_buff[i] = existing_buff[i];
// Append new sample data
for (int i = 0; i < new_rows; i++) {
new_buff[existing_rows + i] = (double)(existing_rows + i);
3. Dynamically Creating Matrix Size and Writing Data
Handling an unknown number of rows dynamically can be managed by resizing your matrix as needed. This is similar to the approach mentioned in the first point. You can start with an initial buffer size and then resize it as more data comes in. The key is to deicde the intial buffer size, manage the buffer size dynamically and ensure you write the final size to the MAT file.
Hope that helped!
Rahul