set variable to data that is loaded

8 visualizaciones (últimos 30 días)
Kevin
Kevin el 25 de Oct. de 2011
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!

Respuesta aceptada

Daniel Shub
Daniel Shub el 25 de Oct. de 2011
x = load('matfile.mat');
temp = fieldnames(x);
x = x.(temp{1});
  3 comentarios
Kevin
Kevin el 25 de Oct. de 2011
Ah, also the {} are needed too.
Daniel Shub
Daniel Shub el 25 de Oct. de 2011
The {} make/index CELL arrays. They are like [], but have some advantages (and disadvantages).

Iniciar sesión para comentar.

Más respuestas (1)

Grzegorz Knor
Grzegorz Knor el 25 de Oct. de 2011
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 comentarios
Daniel Shub
Daniel Shub el 25 de Oct. de 2011
You can eliminate the eval with dynamic field names.
Kevin
Kevin el 25 de Oct. de 2011
Thanks too Grzegorz!

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by