How to create a time series from C++ using the MEX files?

2 次查看(过去 30 天)
So I am trying to write a MAT-file from a CPP standalone application because it seems the easiest to method to execute a standalone Simulink model, which then will output to another MAT-file to then be read back into the C++ standalone application. I managed to create my struct and all it's variables which are written to a MAT-file, but I have to make them be a time-series for the Simulink model to execute.
I can't find anywheres describing how to create a time-series double (for example) from C++ code, so any help is appreciated. If anyone knows if there is also a better method for executing a Simulink model from a C++ application rather than a from-file input, feel free to weigh in as well.

采纳的回答

Pratyush
Pratyush 2024-7-12
Hi Justin,
Creating a time-series data structure in a MAT-file from a C++ application involves understanding the MAT-file API provided by MATLAB. The time-series data in MATLAB is typically represented using the 'timeseries' object, which contains time and data vectors. You can follow these steps to create a MAT-file with time-series data from C++:
  1. Include the MAT-file API Header: Ensure you include the necessary header for MAT-file operations.
  2. Create the MAT-file: Open a MAT-file for writing.
  3. Create Time and Data Arrays: Define the time and data arrays. For simplicity, let's create a time vector t and a corresponding data vector y.
  4. Create mxArray for Time and Data: Convert the C++ arrays to MATLAB arrays.
  5. Create the Struct for Time-Series Data: Create a struct array to hold the time and data arrays.
  6. Write to MAT-file: Write the struct to the MAT-file.
  7. Cleanup: Close the MAT-file and clean up the allocated memory.
If you are looking for other methods to execute a Simulink model from a C++ application, consider the following:
  1. MATLAB Engine API for C++: This allows you to run MATLAB commands directly from C++.
  2. Simulink Coder: Generate C/C++ code from Simulink models for integration.
  1 个评论
Justin
Justin 2024-7-12
@Pratyush Awesome thanks for the info. As for claification on "other methods to execute a Simulink model from a C++ application", I am using Simulink Coder, I just was having an issue working out the InBus element and compiling the Cpp files with my console application that would be used to set the external inputs. So I instead opted for just compiling to the executable using Simulink Coder and trying the method of writing to a MAT file, running the model, then reading the model output, all from within my own Cpp standalone console application.
Quick question, you say "Create the Struct for Time-Series Data: Create a struct array to hold the time and data arrays.", is this just supposed to be the CreateMxStructArray 1x1 with the 2 fields being mxArrays as variables?
That being said I've opted to attempt again to compile the cpp files with my application using the InBus and just calling the method for setting external inputs, unfortuantely I'm receiving various linker issues when trying to compile. So having the information on how to write a time series from C++ to a MAT file is appreciated because I may return to that.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by