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

64 vues (au cours des 30 derniers jours)
Pratima Malhotra
Pratima Malhotra le 8 Juil 2019
Commenté : Cloud le 7 Oct 2022
how to convert .mat file to .m file in matlab R2019a version
  1 commentaire
Walter Roberson
Walter Roberson le 8 Juil 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.

Connectez-vous pour commenter.

Réponses (2)

Stephan
Stephan le 8 Juil 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 le 7 Oct 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 commentaires
Walter Roberson
Walter Roberson le 7 Oct 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 le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by