set variable to data that is loaded

조회 수: 14 (최근 30일)
Kevin
Kevin 2011년 10월 25일
Hello all,
Hopefully a very easy question. I am loading a file named matfile.mat that's value is double. I want to set my variable as the data that is loading, but when I put "x=load(matfile)" it returns "x=matfile: [1001x14 double]". I know I can type in x.matfile to access the data manually, but I run into issues when figuring out how to do this for my function. Any help is greatly appreciated!

채택된 답변

Daniel Shub
Daniel Shub 2011년 10월 25일
x = load('matfile.mat');
temp = fieldnames(x);
x = x.(temp{1});
  댓글 수: 3
Kevin
Kevin 2011년 10월 25일
Ah, also the {} are needed too.
Daniel Shub
Daniel Shub 2011년 10월 25일
The {} make/index CELL arrays. They are like [], but have some advantages (and disadvantages).

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

추가 답변 (1개)

Grzegorz Knor
Grzegorz Knor 2011년 10월 25일
If you are sure that you load only one matrix:
fName = fieldnames(x);
eval(['x = x.' fName{1} ';'])
Another possibility:
x = cell2mat(struct2cell(x))
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 10월 25일
You can eliminate the eval with dynamic field names.
Kevin
Kevin 2011년 10월 25일
Thanks too Grzegorz!

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by