Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Best way to loop through unknown structure

Subject: Best way to loop through unknown structure

From: Karl Trumstedt

Date: 15 Aug, 2007 09:05:40

Message: 1 of 5

Here is what I am using right now. I am wondering if there
is any other way to do it. Say that I want to convert each
structure field to one regular variable. What is the best
method do you think?

x = struct(...)

if isstruct(x)
    s = fieldnames(x);
    for i=1:length(s)
        assignin('caller', char(s(i)), x.(char(s(i)));
    end
end

/ Karl
Developer @ Lundin Mining AB
http://www.lundinmining.com/index.php
Developer @ Kolonn AB http://www.kolonn.se

Subject: Re: Best way to loop through unknown structure

From: Titus

Date: 15 Aug, 2007 09:19:35

Message: 2 of 5


"Karl Trumstedt" <karltru@gmail.com> schrieb im Newsbeitrag
news:f9ufl4$tb$1@fred.mathworks.com...
> Here is what I am using right now. I am wondering if there
> is any other way to do it. Say that I want to convert each
> structure field to one regular variable. What is the best
> method do you think?
>
> x = struct(...)
>
> if isstruct(x)
> s = fieldnames(x);
> for i=1:length(s)
> assignin('caller', char(s(i)), x.(char(s(i)));
> end
> end
>
> / Karl
> Developer @ Lundin Mining AB
> http://www.lundinmining.com/index.php
> Developer @ Kolonn AB http://www.kolonn.se

Hi,
the only suggestion I would have is to use a simpler call
into the cell arrays:
        assignin('caller', s{i}, x.(s{i}));
but that's all. Perhaps you could tell us, why you would
need something like that?

Titus



Subject: Re: Best way to loop through unknown structure

From: Karl Trumstedt

Date: 15 Aug, 2007 09:27:43

Message: 3 of 5

"Titus" <titus.edelhofer@mathworks.de> wrote in message
<f9ugf5$9v2$1@fred.mathworks.com>...
>
> "Karl Trumstedt" <karltru@gmail.com> schrieb im
Newsbeitrag
> news:f9ufl4$tb$1@fred.mathworks.com...
> > Here is what I am using right now. I am wondering if
there
> > is any other way to do it. Say that I want to convert
each
> > structure field to one regular variable. What is the
best
> > method do you think?
> >
> > x = struct(...)
> >
> > if isstruct(x)
> > s = fieldnames(x);
> > for i=1:length(s)
> > assignin('caller', char(s(i)), x.(char(s(i)));
> > end
> > end
> >
> > / Karl
> > Developer @ Lundin Mining AB
> > http://www.lundinmining.com/index.php
> > Developer @ Kolonn AB http://www.kolonn.se
>
> Hi,
> the only suggestion I would have is to use a simpler call
> into the cell arrays:
> assignin('caller', s{i}, x.(s{i}));
> but that's all. Perhaps you could tell us, why you would
> need something like that?
>
> Titus
>
>

Hi,
I'm mostly just experimenting :) Always nice to have more
matlab knowledge ;)

I also have a "few" structs with data that I would like to
make available for the user to use in the interactive
plotting tool. I haven't really played around with it, so
it might be possible to use the structs to plot, making it
unnecessary to convert the structs.

Cheers
/ Karl
Developer @ Lundin Mining AB
http://www.lundinmining.com/index.php
Developer @ Kolonn AB http://www.kolonn.se

Subject: Re: Best way to loop through unknown structure

From: Loren Shure

Date: 15 Aug, 2007 19:51:08

Message: 4 of 5

In article <f9ufl4$tb$1@fred.mathworks.com>, karltru@gmail.com says...
> Here is what I am using right now. I am wondering if there
> is any other way to do it. Say that I want to convert each
> structure field to one regular variable. What is the best
> method do you think?
>
> x = struct(...)
>
> if isstruct(x)
> s = fieldnames(x);
> for i=1:length(s)
> assignin('caller', char(s(i)), x.(char(s(i)));
> end
> end
>
> / Karl
> Developer @ Lundin Mining AB
> http://www.lundinmining.com/index.php
> Developer @ Kolonn AB http://www.kolonn.se
>

See this blog post today on looping through non-numeric arrays:

http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-
values/

I think you have an extra level of indexing that you can do without.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Re: Best way to loop through unknown structure

From: us

Date: 15 Aug, 2007 20:34:23

Message: 5 of 5

Karl Trumstedt:
<SNIP wants to infest his/her clean workspace with numerous
field data...

one of the (unconventional) solutions

% the data
     s.a=1:5;
     s.b=pi;
     s.c=magic(4);
% the engine
     fnam='foo.mat'; % <- use your own test file!
     save(fnam,'-struct','s');
% the result
     clear a b c s; % !
     load(fnam);
     a
     b
     c

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
struct us 15 Aug, 2007 16:35:07
structure us 15 Aug, 2007 16:35:07
fields us 15 Aug, 2007 16:35:07
save us 15 Aug, 2007 16:35:07
load us 15 Aug, 2007 16:35:07
code us 15 Aug, 2007 16:35:07
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics