From: d c on
Is there a quick way to dumping a collection of variables to a structure, using the variable names as the structure fields? The "load" function basically does this somehow? Sorry if this has been answered, I'm just finding a solution quickly.

i.e.

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

x = var2struct(a,b)

x.a
x.b

Also, what about the reverse? Dumping a.* variables to the current scope?
clear
x.a='asdf'
x.b=rand(10);
dumpstruct(x)
a
b
From: d c on
.... and I don't think

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cell2struct.html

can do this, unless I don't understand all the features of that function.
From: Matt J on
"d c" <myspamcatcher07(a)gmail.com> wrote in message <i41cft$1k5$1(a)fred.mathworks.com>...

> Also, what about the reverse? Dumping a.* variables to the current scope?
> clear
> x.a='asdf'
> x.b=rand(10);
> dumpstruct(x)
> a
> b
================


See this FEX tool:

http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables






> a = 'adsf'
> b = rand(10);
>
> x = var2struct(a,b)
>
> x.a
> x.b
======================

It's unlikely that you'd benefit from a tool like this. You'll expend just as many keystrokes by typing

x.a='adsf'
x.b=rand(10);
From: Matt J on

> > a = 'adsf'
> > b = rand(10);
> >
> > x = var2struct(a,b)
> >
> > x.a
> > x.b
> ======================
>
> It's unlikely that you'd benefit from a tool like this. You'll expend just as many keystrokes by typing
>
> x.a='adsf'
> x.b=rand(10);
====================


Although, here's a possible implementation:

function x=var2struct(varargin)


for ii=1:length(varargin)

x.(inputname(varargin{ii}))=varargin{ii};

end
From: Matt Fig on
You could use load, but you mention this and seem unhappy with this option. If you want to write your own functions, you could do:


function [] = struct2var(M)
F = fieldnames(M)
for ii = 1:length(F)
assignin('caller',F{ii},M.(F{ii}));
end


and for the reverse op:

function [M] = var2struct(varargin)
for ii = 1:length(varargin)
M.(inputname(ii)) = varargin{ii};
end


Note that there may be some typo's in the above as I did not test them but they should get you there.