Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading a variable from a .mat file into a differently named variable

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. Does a whos on the file to get the contained names.
  2. Iteratively load each contained structure.
  3. Uses 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?

like image 236
jhfrontz Avatar asked Feb 01 '12 22:02

jhfrontz


People also ask

How do you load a MAT file to a variable in MATLAB?

If filename is a MAT-file, then load(filename) loads variables in the MAT-file into the MATLAB® workspace. If filename is an ASCII file, then load(filename) creates a double-precision array containing data from the file.

How do I save a .MAT file as a variable?

To save a subset of your workspace variables to a MAT-file, select the variables in the Workspace browser, right-click, and then select Save As. You also can drag the selected variables from the Workspace browser to the Current Folder browser.

How do I save a mat in a different folder?

mat . To save to another directory, use the full pathname for the filename . If filename is the special string stdio , the save command sends the data as standard output. save filename var1 var2 ...

How do I save a specific variable in MATLAB?

save( filename ) saves all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename . If filename exists, save overwrites the file. save( filename , variables ) saves only the variables or fields of a structure array specified by variables .


2 Answers

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

x = 1;
save foo;

ls = load('foo.mat');
ls.x
like image 165
Andrey Rubshtein Avatar answered Nov 07 '22 07:11

Andrey Rubshtein


you can 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');

Example

>> 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
like image 5
Azim J Avatar answered Nov 07 '22 06:11

Azim J