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

조회 수: 7 (최근 30일)
Maria Drozdova
Maria Drozdova 2017년 3월 16일
편집: Aniket 2025년 4월 8일
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