Writing data Dynamically in to .mat file using c++ - Append data to existing mat file

21 次查看(过去 30 天)
Hello, I have question reagrding writing .MAT file and printing data dynamically using MAT API.
1. As per my understanding, To print data in to the mat file, we need to have whole data in buffer before writing, Is there any way or API to write data in to mat file similar to normal file writing dynamically?
(while printing last row data we need to have previous rows data with us to loop and print)
mxArray* mx_field_value;
double* buff;
int count_MAT =0;
mx_field_value = mxCreateDoubleMatrix(count_MAT, 1, mxREAL); buff = mxGetPr(mx_field_value);
for (i = 0; i < count_MAT; i++)buff[i] = (double)detmat[i].target_count; // How can we avoid this loop?
matPutVariable(pmat, "count", mx_field_value);
count_MAT++;
2. How to Append data in to exiting mat file with same coulmns and rows of same fields? If we created Double matrix data is overwriting?
3. we don't know the number of rows before started writing in to mat file, we will get total number of rows count dynamically. In this case can we create matrix size dynamically and write data dynamically?

回答(2 个)

Rahul
Rahul 2024-8-13,5:46
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 usemxArraystructures 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:
#include "mat.h"
#include "matrix.h"
MATFile *pmat;
mxArray *mx_field_value;
double *buff;
int count_MAT = 0;
int buffer_size = 10; // Initial buffer size
// Open MAT file
pmat = matOpen("example.mat", "w");
// Create initial buffer
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
buffer_size *= 2;
// 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;
// Increase buffer size
count_MAT++;
}
  • 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.
// Example usage
for (int i = 0; i < 200; i++) {
addData((double)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.
// Write to MAT file
matPutVariable(pmat, "count", new_data);
// Clean up
mxDestroyArray(mx_field_value);
matClose(pmat);
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
// Read existing data
existing_data = matGetVariable(pmat, "count");
existing_buff = mxGetPr(existing_data);
existing_rows = mxGetM(existing_data);
// New data to append
new_rows = 100;
new_data = mxCreateDoubleMatrix(existing_rows + new_rows, 1, mxREAL);
new_buff = mxGetPr(new_data);
// Copy existing 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

Walter Roberson
Walter Roberson 2024-8-13,6:01
Your only hope is if MATLAB happens to export the matfile API as a library. In that case, provided you are using a -v7.3 .mat file, you would be able to dynamically add data.
Note: it is usually a lot less internal work to append additional columns than to append new rows. You might consider transposing your array.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by