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

조회 수: 58 (최근 30일)
Pratima Malhotra
Pratima Malhotra 2019년 7월 8일
댓글: Cloud 2022년 10월 7일
how to convert .mat file to .m file in matlab R2019a version
  댓글 수: 1
Walter Roberson
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
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.

Cloud
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
Walter Roberson 2022년 10월 7일
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 2022년 10월 7일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by