how to convert .mat file to .m file in matlab R2019a version
59 次查看(过去 30 天)
显示 更早的评论
how to convert .mat file to .m file in matlab R2019a version
1 个评论
Walter Roberson
2019-7-8
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.
回答(2 个)
Stephan
2019-7-8
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.
0 个评论
Cloud
2022-10-7
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 个评论
Walter Roberson
2022-10-7
str = 'αφγα'
bytes = unicode2native(str)
whos bytes
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.
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
2022-10-7
@Walter Roberson Thanks for sharing details including web link:
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!