Saving *.mat files using C#

조회 수: 7 (최근 30일)
Yoni Stern
Yoni Stern 2022년 11월 13일
답변: Harsh 2025년 7월 24일
Hi all,
I have a simple 3D array of doubles that I need to save as a *.mat file, so that MATLAB (2018B) can later import it and work with it. I am looking for the equivalent in C# that may help me do the same.
My programmer tried to do so by saving the 3D array as binary, but when I try to UIOpen them into MATLAB I get:
"Error using load. Unable to read MAT-file
C:\...\Max_Temp.mat. Not a binary MAT-file. Try load -ASCII to read as text."
When I use a mat file which I saved using regular "save" command in MATLAB, it imports it with no problems.
Can anybody help my programmer use the right format so that the saved mat file is recognized by MATLAB?
Thanks!

답변 (1개)

Harsh
Harsh 2025년 7월 24일
The most reliable approach is to use MATLAB's official .NET interface. This requires having MATLAB installed on the machine where your C# code runs. Here's an example script to achieve your task-
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
// Create your 3D array
double[,,] myArray = new double[10, 20, 30]; // Your actual data here
// Convert to MATLAB array
MWNumericArray matlabArray = new MWNumericArray(myArray);
// Save to .mat file
MATLAB.save("C:\\path\\to\\your\\file.mat", "variableName", matlabArray);
You can find more information about this approach in the official documentation for MATLAB Engine API for .NET - https://www.mathworks.com/help/compiler_sdk/dotnet_assemblies.html

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by