cargar una variable de un archivo .mat en una variable con un nombre diferente

Say I have a .mat file with several instances of the same structure, each in a different variable name.

I want to process each instance found in a file (which I find using whos('-file' ...). I was hoping that load would let me specify the destination name for a variable so that I didn't have to worry about collisions (and so that I didn't have to write self-modifying code a la eval).

The brute force way to do this appears to be creating a helper function that, using variables with names that hopefully don't conflict with the .mat contents, does something like:

  1. Hace un whos on the file to get the contained names.
  2. Iteratively load each contained structure.
  3. Utiliza materiales de eval to assign the loaded structure into, say, a cell array (where one column of the array contains the .mat file's structure names and a corresponding column with the actual contents of each structure from the .mat file).

Is there no more elegant way to accomplish the same thing?

preguntado el 01 de febrero de 12 a las 22:02

2 Respuestas

Of course you can! Just use load with an output argument.

x = 1;
save foo;

ls = load('foo.mat');
ls.x

Respondido 02 Feb 12, 03:02

nice. I repeatedly looked at the load manual page wondering "what is the return value of a 'structure array' supposed to be?" Now I know! - jhfrontz

usted puede load the data in a MAT file into a structure

ws = load('matlab.mat');

the fields of the structure ws will be the variables in the MAT file. You can then even have a cell array of structures

ws{1}=load('savedWorkSpace_1.mat');
ws{2}=load('savedWorkSpace_2.mat');

Ejemplo

>> x = 1;
>> save ws_1;
>> x = 2;
>> y = 1;
>> save ws_2
>> clear
>> ws{1} = load('ws_1.mat')
>> ws{2} = load('ws_2.mat')
>> ws{1}.x
    x = 1
>> ws{2}.x
    x = 2

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.