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

1 visualización (últimos 30 días)
Maria Drozdova
Maria Drozdova el 16 de Mzo. de 2017
Editada: Aniket el 8 de Abr. de 2025
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!

Respuestas (1)

Aniket
Aniket el 8 de Abr. de 2025
Editada: Aniket el 8 de Abr. de 2025
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.

Categorías

Más información sobre MATLAB Compiler SDK en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by