Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move variables into and out of a structure akin to LOAD and SAVE in MATLAB?

Is there a quick way (i.e. one line) to dump a collection of variables "in" a structure, using the variable names as the structure fields? The "load" function basically does this but saving and loading to a temporary file seems ugly.

For example:

clear
a = 'adsf'
b = rand(10);

x = var2struct(a,b)

x.a
x.b

or better yet:

x = var2struct(['a';'b'])

Also, what about the reverse (i.e. dumping the field values to the current scope as variables named after the fields)?:

clear
x.a='asdf'
x.b=rand(10);
dumpstruct(x)
a
b 

Also, here's a related newsgroup thread.

like image 690
mathtick Avatar asked Aug 12 '10 18:08

mathtick


People also ask

How do I save a variable in MATLAB?

MATLAB® saves the variables to the file, pqfile. mat , in the current folder. You also can use command syntax to save the variables, p and q .

How do I save a MATLAB variable as a text file?

You can save variables using the "save" command, with the /ascii option. The format options are limited to either short or long floating point format with the "save" command.

What is the use of MAT file in MATLAB?

Description. Use a MAT-file object to access and change variables in a MAT-file without loading the file into memory. You can load or save parts of variables. Partial loading and saving of variables using a MAT-file object requires less memory than the load and save commands.


1 Answers

Aside from using LOAD and SAVE, there is no built-in function that I know of to do this. However, you could just make your own functions, like so:

function s = var2struct(varargin)
  names = arrayfun(@inputname,1:nargin,'UniformOutput',false);
  s = cell2struct(varargin,names,2);
end

function struct2var(s)
  cellfun(@(n,v) assignin('base',n,v),fieldnames(s),struct2cell(s));
end

Working from the base workspace, you can use these functions like so:

a = 'adsf'
b = rand(10);
x = var2struct(a,b);
clear a b
struct2var(x);

A couple notes:

  • If you would rather specify the arguments to var2struct as the variable names instead of the variables themselves, here is an alternative function:

    function s = var2struct(varargin)
      values = cellfun(@(n) evalin('base',n),varargin,'UniformOutput',false);
      s = cell2struct(values,varargin,2);
    end
    

    And you would use this from the base workspace as follows:

    x = var2struct('a','b');
    

    Unfortunately, you can only use this version of the function to get variables from the base workspace, not the workspace of a function.

  • One caveat with the struct2var function above is that it will always create the variables in the base workspace, not the workspace of the function calling struct2var. To create variables in a workspace other than the base, you would have to use this line in that workspace instead of calling struct2var:

    cellfun(@(n,v) assignin('caller',n,v),fieldnames(x),struct2cell(x));
    
like image 143
gnovice Avatar answered Sep 28 '22 03:09

gnovice