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

58 visualizaciones (últimos 30 días)
Pratima Malhotra
Pratima Malhotra el 8 de Jul. de 2019
Comentada: Cloud el 7 de Oct. de 2022
how to convert .mat file to .m file in matlab R2019a version
  1 comentario
Walter Roberson
Walter Roberson el 8 de Jul. de 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.

Iniciar sesión para comentar.

Respuestas (2)

Stephan
Stephan el 8 de Jul. de 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
Cloud el 7 de Oct. de 2022
When I directly open a MAT file in MatLab, the software generated this snippet automatically.
function import_mat_file(fileToRead1)
%IMPORTFILE(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}));
end
You may add function to save extract variables to M file.
My runtime environment:
OS: Windows 10
MatLab: R2022a
  6 comentarios
Walter Roberson
Walter Roberson el 7 de Oct. de 2022
str = 'αφγα'
str = 'αφγα'
bytes = unicode2native(str)
bytes = 1×8
206 177 207 134 206 179 206 177
whos bytes
Name Size Bytes Class Attributes bytes 1x8 8 uint8
Notice that bytes here is numeric but not double precision.
Likewise, technically str is numeric but not double precision (character vectors are uint16 internally)
vars{i}, newData1.(vars{i}) key and value format,
If you need to represent a cell array and a struct, then those would not be numeric.
Question: for your purposes would it be good enough to jsonencode ?
When I open the MAT file in MatLab, it looks like bit for bit accuracy.
Yes, .mat files store exact bits. The question I had was whether the .m file that is generated from the .mat needs to exactly reproduce the data from the .mat file, or whether losing the last bit of double precision numbers would be acceptable ?
Cloud
Cloud el 7 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by