- Enable MATLAB COM Server: Ensure MATLAB is installed and registered as a COM server. Typically, it's available as: MWServer.Matlab
- Create the C# console application in your development environment. The reference to the MATLAB Type Library for C# is:
How to connect Matlab *.m file and c# project?
1 view (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Aniket
on 8 Apr 2025
Edited: Aniket
on 8 Apr 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:
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 patient’s 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.
0 Comments
See Also
Categories
Find more on MATLAB Compiler SDK in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!