How can i store a .mat file into a variable?

 채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 21일

0 개 추천

fid = fopen('YourMatFile.mat', 'r') ;
YourVariable = fread(fid, '*uint8').' ;
fclose(fid) ;
YourVariable is now a row vector of bytes that make up the mat file.
This kind of thing can be useful if you need to transmit a mat file over wireless or Ethernet, but otherwise has limited use.
.... But perhaps the answer you were looking for was just
YourVariable = load('YourMatFile.mat');

댓글 수: 4

sam  CP
sam CP 2017년 3월 21일
i just want to get a function like normal=xlsread('normal.xlsx'); Above is the syntax to read an excel file but here instead of , i have to read the .mat file
.mat files typically have multiple variables.
If you have exactly one variable stored in a .mat file, then
mat_filename = 'YourMatFile.mat';
temp = load(mat_filename);
fn = fieldnames(temp);
first_variable = fn{1};
normal = temp.(first_variable);
clear temp
This can be abbreviated to
FirstCell = @(C) C{1};
temp = load('YourMatFile.mat');
normal = temp.(FirstCell(fieldnames(temp)));
Kieran Graham
Kieran Graham 2020년 4월 22일
Hi, I am also having a similar problem. I have 23 variables stored in my temp.mat file. Is it possible to use these values by simply using load('temp.mat', 'variable1', 'variable2', ....) or will I need to recover the values 1 by 1?
Thanks
@Kieran Graham: the recommended approach is to simply load into an output variable
S = load(...);
All of your variables will be fields in the scalar structure S, and are trivial to access.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

질문:

2017년 3월 21일

댓글:

2020년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by