how to convert .mat file to .m file in matlab R2019a version

38 views (last 30 days)
how to convert .mat file to .m file in matlab R2019a version
  1 Comment
Walter Roberson
Walter Roberson on 8 Jul 2019
This is not possible in the general case. It is, however, easy in some cases, especially if the .mat contains a single variable that is a numeric vector or 2D array.

Sign in to comment.

Answers (2)

Stephan on 8 Jul 2019
You can not convert *.mat to *.m files. The *.mat format saves variables from workspace and the *.m file format saves Matlab code. So there is no way to convert it into each other. It are different formats for different use.

Cloud on 7 Oct 2022
When I directly open a MAT file in MatLab, the software generated this snippet automatically.
function import_mat_file(fileToRead1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 06-Oct-2022 18:07:48
% Import the file
newData1 = load('-mat', fileToRead1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
You may add function to save extract variables to M file.
My runtime environment:
OS: Windows 10
MatLab: R2022a
Cloud on 7 Oct 2022
@Walter Roberson Thanks for sharing details including web link:
For your purposes would it be good enough to jsonencode ?
I haven't tried the function.
Whether losing the last bit of double precision numbers would be acceptable ?
I think it depends on application. For me double precision is good enough.

Sign in to comment.


Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by