From: Yoni on
Say I define a bunch of variables:

height = 7
width = 8
label = 'awesome'
otherStuff = [1 2 3 4]

and I want to turn them all into a struct, such that

myStruct.height = height
myStruct.width = width

etc…

Then I later want to be able to re-extract individual variables from it, using the field names as variable names. Is there an elegant way to do this? Only ways I can think of now are extremely kludgy. So for variables → struct:

save myStruct height width label otherStuff;
myStruct = load(‘myStruct’)

while for the conversion back, it seems like I’d have to use eval with a generous helping of sprintf, and probably a good deal of constraints on how complex the struct is.

It seems to me that there could be some real utility in being able to do this, but unless I’m missing something, it doesn’t seem possible. Am I missing something?
From: sscnekro on
> and I want to turn them all into a struct, such that
> myStruct.height = height
> Then I later want to be able to re-extract individual variables from it, using the field names as variable names.

Hi there, structures too are an issue for me so I hope some Experts will respond to this thread.

As for feeding stuff into structure, I know - - apparently as you do also - - two ways. One is this struct() described in hlp doc which seems crazy for using unless you don't find a smart way of listing all the contents you want, I don't know any. I mean, e.g. how to feed all stuff in workspace into a structure without the need of writing down a list. The other way I know is t load stuff from an existing file into a structure.

As for using structures, just recently I learned (from Oleg) that once a structure S is created,
mynames = fieldnames(S); % captures names of what is in S
S.(mynames{ii}) % refers to ii-th stuff, useful in a loop
What is your particular purpose of using structures? Maybe this helps a bit.

PS The little I learned and used around structures I received here in the newsgroup. From the hlp doc I don't really know to imagine how to make use of those commands in real-life-problem context. To my rescue, the Experts in the newsgroup proved excellent teachers.
From: Matt J on
"Yoni " <yonatan.mazuz(a)duke.edu> wrote in message <i038b0$nja$1(a)fred.mathworks.com>...
> Say I define a bunch of variables:
>
> height = 7
> width = 8
> label = 'awesome'
> otherStuff = [1 2 3 4]
>
> and I want to turn them all into a struct, such that
>
> myStruct.height = height
> myStruct.width = width
================

Then you would have been better off introducing the variables this way in the first place

myStruct.height = 7
myStruct.width = 8

An automated conversion to struct won't save you much in keystrokes, since you'll at least have to type all the variable names a 2nd time as input to the conversion function.



> Then I later want to be able to re-extract individual variables from it, using the field names as variable names. Is there an elegant way to do this?
=====================

It's dangerous to introduce variables into a workspace non-explicitly, which is why I wrote this tool as a compromise

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

However, if you're sure you're prepared to bear the risks, you can also use it in conjunction with eval

>> clear
>> myStruct.height=7; myStruct.width=8;
>> eval(structvars(myStruct).')
>> height, width

height =

7


width =

8
From: us on
"Yoni " <yonatan.mazuz(a)duke.edu> wrote in message <i038b0$nja$1(a)fred.mathworks.com>...
> Say I define a bunch of variables:
>
> height = 7
> width = 8
> label = 'awesome'
> otherStuff = [1 2 3 4]
>
> and I want to turn them all into a struct, such that
>
> myStruct.height = height
> myStruct.width = width
>
> etc&#8230;
>
> Then I later want to be able to re-extract individual variables from it, using the field names as variable names. Is there an elegant way to do this? Only ways I can think of now are extremely kludgy. So for variables &#8594; struct:
>
> save myStruct height width label otherStuff;
> myStruct = load(&#8216;myStruct&#8217;)
>
> while for the conversion back, it seems like I&#8217;d have to use eval with a generous helping of sprintf, and probably a good deal of constraints on how complex the struct is.
>
> It seems to me that there could be some real utility in being able to do this, but unless I&#8217;m missing something, it doesn&#8217;t seem possible. Am I missing something?

one of the solutions

% create a file, eg, v2s.m
function s=v2s(varargin)
% help/error checks removed...
for i=1:nargin
s.(inputname(i))=varargin{i};
end
end

% at the command prompt
foo=pi;
goo=magic(3);
s=v2s(foo,goo)
%{
% s =
foo: 3.1416
goo: [3x3 double]
%}

us
From: Yoni on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i05h9c$6s1$1(a)fred.mathworks.com>...
> "Yoni " <yonatan.mazuz(a)duke.edu> wrote in message <i038b0$nja$1(a)fred.mathworks.com>...
> > Say I define a bunch of variables:
> >
> > height = 7
> > width = 8
> > ...
> > and I want to turn them all into a struct, such that
> >
> > myStruct.height = height
> > myStruct.width = width
>
> Then you would have been better off introducing the variables this way in the first place
>
> myStruct.height = 7
> myStruct.width = 8
>
> An automated conversion to struct won't save you much in keystrokes, since you'll at least have to type all the variable names a 2nd time as input to the conversion function.

You make a great point. I think I was more hoping to save keystrokes at the struct-exploding end of things. i.e. typing &#8220;height&#8221; into a conversion function&#8217;s arg list is way lazier than typing &#8220;height = myStruct.height&#8221; later.



> > Then I later want to be able to re-extract individual variables from it, using the field names as variable names. Is there an elegant way to do this?
> =====================
>
> It's dangerous to introduce variables into a workspace non-explicitly,&#8230;

Yeah, I&#8217;ve had a feeling it was something real computer scientists would frown upon, but I wasn&#8217;t entirely sure why.

> &#8230;which is why I wrote this tool as a compromise
> &#8230;
> However, if you're sure you're prepared to bear the risks, you can also use it in conjunction with eval

And the thread you cite in the FEX listing cleared up the danger of poofing variables. So I think I might use your tool in its intended way, by having it generate code that I can copy and paste later.

OTOH, I think that packing variables into a struct would be less dangerous. Well, okay, I guess if the struct name and some of the fields already existed before calling the packing function, and then you referenced those after creating the new struct, the interpreter might get confused. For my purposes, though, it seems okay: packing the variables into a struct would literally be the last line of code in a given function. So I may try something like what us said.