Remove structure

  • Follow


Hi,

Is there a simple way to get the names of variables removing their struture?

For exemple, for a structure :
conf.a.name_1 (= 1);
conf.b.name_2 (= 'hello');
conf.c.d.e.name_3 = ([1 1]);
I would like to get 'name_1' 'name_2' 'name_3' so as to define:
name_1 (= 1);
name_2 (= 'hello');
name_3 = ([1 1]);

Thanks
0
Reply Pierre 12/9/2009 2:33:01 PM

"Pierre " <pierre.dandre@external.thalesaleniaspace.com> wrote in message 
news:hfocet$59b$1@fred.mathworks.com...
> Hi,
>
> Is there a simple way to get the names of variables removing their 
> struture?
>
> For exemple, for a structure :
> conf.a.name_1 (= 1);
> conf.b.name_2 (= 'hello');
> conf.c.d.e.name_3 = ([1 1]);
> I would like to get 'name_1' 'name_2' 'name_3' so as to define:
> name_1 (= 1);
> name_2 (= 'hello');
> name_3 = ([1 1]);

DON'T DO THIS.

See Q4.6 in the newsgroup FAQ for an explanation why this is a Bad Idea.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 12/9/2009 2:48:48 PM


> 
> DON'T DO THIS.
> 
> See Q4.6 in the newsgroup FAQ for an explanation why this is a Bad Idea.
> 
> -- 
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 
> 
Thanks for the quick and logical answer. In fact, I want to get the name only and dont want to assign it. In the example, I would like to get the strings 'name_1' 'name_2' 'name_3'. 

If I have well understood the purpose of the fieldnames function, I can only look at "level" 1. I woud have 'a' 'b' 'c' with my example. In my case, I don't know the number of "levels" in my structure, which can vary from one chain to another one.
0
Reply Pierre 12/9/2009 3:19:02 PM

> DON'T DO THIS.
> 
> See Q4.6 in the newsgroup FAQ for an explanation why this is a Bad Idea.
> 
> -- 
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 
> 

Thanks you for your quick and logical answer. 

In fact, I would like to get the names only, that is to say 'name_1', 'name_2' and 'name_3' in my previous example.

I don't know if the function fieldnames can easely deal with it. It seems to me that it only provides the "level 1" names (i.e. 'a' 'b' 'c' in my example). As I don't know the number of levels of the structure (which can vary from one "branch" to another), I can't simply use the fieldnames function.
Thks, Pierre
0
Reply Pierre 12/9/2009 3:32:03 PM

"Pierre " <pierre.dandre@external.thalesaleniaspace.com> wrote in message 
news:hfof56$4hj$1@fred.mathworks.com...
>>
>> DON'T DO THIS.
>>
>> See Q4.6 in the newsgroup FAQ for an explanation why this is a Bad Idea.
>>
>> -- 
>> Steve Lord
>> slord@mathworks.com
>> comp.soft-sys.matlab (CSSM) FAQ: 
>> http://matlabwiki.mathworks.com/MATLAB_FAQ
>>
> Thanks for the quick and logical answer. In fact, I want to get the name 
> only and dont want to assign it. In the example, I would like to get the 
> strings 'name_1' 'name_2' 'name_3'.
>
> If I have well understood the purpose of the fieldnames function, I can 
> only look at "level" 1. I woud have 'a' 'b' 'c' with my example. In my 
> case, I don't know the number of "levels" in my structure, which can vary 
> from one chain to another one.

The "DON'T DO THIS" was in reaction to your comment that "I would like to 
get 'name_1' 'name_2' 'name_3' so as to define:".  If you just want the 
strings, not to create variables, then use dynamic field names and 
recursion, something like:

conf.a.name_1 = 1;
conf.b.name_2 = 'hello';
conf.c.d.e.name_3 = [1 1];

fn = fieldnames(conf);
for whichfield = 1:numel(fn)
    fprintf('Processing conf.%s\n', fn{whichfield});
    if isstruct(conf.(fn{whichfield}))
        fprintf('nested struct -- need to recurse\n');
    else
        fprintf('leaf node -- no need to recurse\n');
    end
end

Make a function out of the last piece of this code so you can recurse down 
the structure 'tree'.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 12/9/2009 3:59:59 PM

4 Replies
390 Views

(page loaded in 0.034 seconds)

Similiar Articles:













7/28/2012 12:16:20 AM


Reply: