How to connect Matlab *.m file and c# project?

2 次查看(过去 30 天)
Good morning! I have c# project "Database" which contains information about patients (in a table). In one of the table cells there is a *.txt file with an ECG data. Using this data *.m file builds graphs. I need to connect this *.m file with c# project, for example, I click the button on c# form to open Matlab form.
One more problem. In c# each patient has his own *.txt file. When I push "Open Matlab" button (for certain patient, that means certain *.txt file is chosen), I need Matlab form to use one and the same *.txt file. Is it possible?
Thank you in advance!

回答(1 个)

Aniket
Aniket 2025-4-8
编辑:Aniket 2025-4-8
It is possible to integrate MATLAB with a C# project and pass in patient-specific .txt files dynamically using MATLAB COM Automation Server. This allows you to run MATLAB scripts/functions from your C# code. Please follow the below steps to achieve the same:
  1. Enable MATLAB COM Server: Ensure MATLAB is installed and registered as a COM server. Typically, it's available as: MWServer.Matlab
  2. Create the C# console application in your development environment. The reference to the MATLAB Type Library for C# is:
MLApp.MLApp matlab = new MLApp.MLApp();
Below is a complete code for C# Button handler:
using MLApp; // Add reference to MATLAB COM Object
private void btnOpenMatlab_Click(object sender, EventArgs e)
{
string txtFilePath = GetPatientTxtFile(); // Get path of selected patients ECG txt
// Start MATLAB session
MLApp.MLApp matlab = new MLApp.MLApp();
// Make MATLAB visible (optional)
matlab.Visible = 1;
// Change folder to location of your .m script
matlab.Execute(@"cd 'C:\Path\To\Your\MFiles'");
// Pass txt file path to MATLAB function (plotECG)
string cmd = $"plotECG('{txtFilePath.Replace("\\", "/")}')";
matlab.Execute(cmd);
}
You can find more details regarding using MATLAB functions from C# applications from this documentation:
I hope this helps achieve the desired functionality.

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler SDK 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by