Thread Subject: Extracting variables from a structure?

Subject: Extracting variables from a structure?

From: Mark Hard

Date: 9 Feb, 2010 10:17:05

Message: 1 of 12

Hi, I'm new to structures but was forces to use one as I wanted to use a string as a variable name. What I am left with is a structure called "SETS" like so..

SETS =

    [1x1 struct] [1x1 struct] [1x1 struct]

>> SETS{1}

ans =

    nset_PickedSet2: [1 8 1]

>> SETS{2}

ans =

    nset_PickedSet5: [7 8]

>> SETS{3}

ans =

    nset_PickedSet6: [3 4]


What I want to have is variables...

nset_PickedSet2 = [1 8 1]
nset_PickedSet5 = [7 8]
nset_PickedSet6 = [3 4]

rather than having them in the structure.

Is this possible?

Any help is greatly appreciated.

Thanks.

Mark.

Subject: Extracting variables from a structure?

From: us

Date: 9 Feb, 2010 11:26:04

Message: 2 of 12

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hkrcn1$v0$1@fred.mathworks.com>...
> Hi, I'm new to structures but was forces to use one as I wanted to use a string as a variable name. What I am left with is a structure called "SETS" like so..
> SETS =
> [1x1 struct] [1x1 struct] [1x1 struct]
> >> SETS{1}
> ans =
> nset_PickedSet2: [1 8 1]
> >> SETS{2}
> ans =
> nset_PickedSet5: [7 8]
> >> SETS{3}
> ans =
> nset_PickedSet6: [3 4]
> What I want to have is variables...
> nset_PickedSet2 = [1 8 1]
> nset_PickedSet5 = [7 8]
> nset_PickedSet6 = [3 4]
> rather than having them in the structure.
> Is this possible?
> Any help is greatly appreciated.
> Thanks.
> Mark.

clearly NOT advisable...

http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

one of the solutions

% the data
     clear s; % <- save old stuff(!)...
     s{1}.a=1:3;
     s{2}.bb=1:4;
     s{3}.ccc=1:5;
% the engine
% - convert into CELL array of DOUBLEs
     c=cellfun(@(x) struct2array(x),s,'uni',false).'
%{
% c =
     [1x3 double]
     [1x4 double]
     [1x5 double]
%}
% now, access data by indexing, eg,
     c{1}
% ans = 1 2 3

us

Subject: Extracting variables from a structure?

From: Mark Hard

Date: 9 Feb, 2010 11:46:03

Message: 3 of 12

"us " <us@neurol.unizh.ch> wrote in message <hkrgob$ej9$1@fred.mathworks.com>...
> "Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hkrcn1$v0$1@fred.mathworks.com>...
> > Hi, I'm new to structures but was forces to use one as I wanted to use a string as a variable name. What I am left with is a structure called "SETS" like so..
> > SETS =
> > [1x1 struct] [1x1 struct] [1x1 struct]
> > >> SETS{1}
> > ans =
> > nset_PickedSet2: [1 8 1]
> > >> SETS{2}
> > ans =
> > nset_PickedSet5: [7 8]
> > >> SETS{3}
> > ans =
> > nset_PickedSet6: [3 4]
> > What I want to have is variables...
> > nset_PickedSet2 = [1 8 1]
> > nset_PickedSet5 = [7 8]
> > nset_PickedSet6 = [3 4]
> > rather than having them in the structure.
> > Is this possible?
> > Any help is greatly appreciated.
> > Thanks.
> > Mark.
>
> clearly NOT advisable...
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
>
> one of the solutions
>
> % the data
> clear s; % <- save old stuff(!)...
> s{1}.a=1:3;
> s{2}.bb=1:4;
> s{3}.ccc=1:5;
> % the engine
> % - convert into CELL array of DOUBLEs
> c=cellfun(@(x) struct2array(x),s,'uni',false).'
> %{
> % c =
> [1x3 double]
> [1x4 double]
> [1x5 double]
> %}
> % now, access data by indexing, eg,
> c{1}
> % ans = 1 2 3
>
> us

Thanks for your reply. Unfortunately I have to have the variable names as they are. Te variables aren't being made by a loop they are being extracted from a text file so is it still not advisable to extract them from the structure as there will be at most 5 or 6 of them?

Thanks again.

Mark.

Subject: Extracting variables from a structure?

From: us

Date: 9 Feb, 2010 12:24:03

Message: 4 of 12

"Mark Hard"
> Thanks for your reply. Unfortunately I have to have the variable names as they are. Te variables aren't being made by a loop they are being extracted from a text file so is it still not advisable to extract them from the structure as there will be at most 5 or 6 of them?
>
> Thanks again.
>
> Mark.

one of the solutions
- if you insist on the mnemonics...

% the data
     clear s; % <- save old stuff(!)...
     s{1}.a=1:3;
     s{2}.bb1=1:4;
     s{2}.bb2=1:10;
     s{3}.ccc=1:5;
% the engine
     c=cellfun(@(x) [fieldnames(x),struct2cell(x)],s,'uni',false);
     c=cat(1,c{:}).';
     c=struct(c{:});
% the result
     disp(c);
%{
       a: [1 2 3]
     bb1: [1 2 3 4]
     bb2: [1 2 3 4 5 6 7 8 9 10]
     ccc: [1 2 3 4 5]
%}

us

Subject: Extracting variables from a structure?

From: Mark Hard

Date: 9 Feb, 2010 13:22:02

Message: 5 of 12

"us " <us@neurol.unizh.ch> wrote in message <hkrk53$hi7$1@fred.mathworks.com>...
> "Mark Hard"
> > Thanks for your reply. Unfortunately I have to have the variable names as they are. Te variables aren't being made by a loop they are being extracted from a text file so is it still not advisable to extract them from the structure as there will be at most 5 or 6 of them?
> >
> > Thanks again.
> >
> > Mark.
>
> one of the solutions
> - if you insist on the mnemonics...
>
> % the data
> clear s; % <- save old stuff(!)...
> s{1}.a=1:3;
> s{2}.bb1=1:4;
> s{2}.bb2=1:10;
> s{3}.ccc=1:5;
> % the engine
> c=cellfun(@(x) [fieldnames(x),struct2cell(x)],s,'uni',false);
> c=cat(1,c{:}).';
> c=struct(c{:});
> % the result
> disp(c);
> %{
> a: [1 2 3]
> bb1: [1 2 3 4]
> bb2: [1 2 3 4 5 6 7 8 9 10]
> ccc: [1 2 3 4 5]
> %}
>
> us

Thanks again for your reply. In your example above what I want is to have a, bb1, bb2 and cc to show up a variables in the workspace where,

a = [1 2 3]
bb1 = [1 2 3 4]
bb2 = [1 2 3 4 5 6 7 8 9 10]
ccc: = [1 2 3 4 5]

rather than have them as part of a structure 'c' so that if I type say bb1 into the command window I'll get back..

bb1 =

     1 2 3 4

I hope I'm making sense.

Thanks again

Mark.

Subject: Extracting variables from a structure?

From: Matt J

Date: 9 Feb, 2010 14:24:04

Message: 6 of 12

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hkrhtr$rn4$1@fred.mathworks.com>...

> Thanks for your reply. Unfortunately I have to have the variable names as they are. Te variables aren't being made by a loop they are being extracted from a text file so is it still not advisable to extract them from the structure as there will be at most 5 or 6 of them?
=======================

If there will only be 5 or 6 of them, why not just extract them manually

nset_PickedSet2=SET{1}.nset_PickedSet2;
nset_PickedSet5=SET{2}.nset_PickedSet5;
etc...

Subject: Extracting variables from a structure?

From: Mark Hard

Date: 9 Feb, 2010 15:07:04

Message: 7 of 12

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hkrr64$e1r$1@fred.mathworks.com>...
> "Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hkrhtr$rn4$1@fred.mathworks.com>...
>
> > Thanks for your reply. Unfortunately I have to have the variable names as they are. Te variables aren't being made by a loop they are being extracted from a text file so is it still not advisable to extract them from the structure as there will be at most 5 or 6 of them?
> =======================
>
> If there will only be 5 or 6 of them, why not just extract them manually
>
> nset_PickedSet2=SET{1}.nset_PickedSet2;
> nset_PickedSet5=SET{2}.nset_PickedSet5;
> etc...

Well the number of the set can change everytime depending on the automatically generated text file. so I won't know the names I need to variables to be to input it into the code.

Subject: Extracting variables from a structure?

From: Matt J

Date: 9 Feb, 2010 15:22:05

Message: 8 of 12

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hkrnhq$neh$1@fred.mathworks.com>...

> > % the engine
> > c=cellfun(@(x) [fieldnames(x),struct2cell(x)],s,'uni',false);
> > c=cat(1,c{:}).';
> > c=struct(c{:});
> > % the result
> > disp(c);
> > %{
> > a: [1 2 3]
> > bb1: [1 2 3 4]
> > bb2: [1 2 3 4 5 6 7 8 9 10]
> > ccc: [1 2 3 4 5]
> > %}
> >
> > us
>
> Thanks again for your reply. In your example above what I want is to have a, bb1, bb2 and cc to show up a variables in the workspace where,
>
> a = [1 2 3]
> bb1 = [1 2 3 4]
> bb2 = [1 2 3 4 5 6 7 8 9 10]
> ccc: = [1 2 3 4 5]
>
> rather than have them as part of a structure 'c' so that if I type say bb1 into the command window I'll get back..
>
> bb1 =
>
> 1 2 3 4
>
========================

Once you have the structure c generated by us' engine, you can use my structvars()
tool on the FEX,

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

in conjunction with eval() to transfer the fields of c to separate variables:

>> c

c =

      a: [1 2 3]
    bb1: [1 2 3 4]
    bb2: [1 2 3 4 5 6 7 8 9 10]
    ccc: [1 2 3 4 5]

>> eval(structvars(c).')
>> a,bb1,bb2,ccc

a =

     1 2 3


bb1 =

     1 2 3 4


bb2 =

     1 2 3 4 5 6 7 8 9 10


ccc =

     1 2 3 4 5

I cannot emphasize enough, however, what a bad practice this is. It is a very bad idea to introduce variables into a MATLAB workspace without an explicit assignment statement. You should extricate yourself from the need for this at the earliest opportunity.

Subject: Extracting variables from a structure?

From: Walter Roberson

Date: 9 Feb, 2010 15:46:46

Message: 9 of 12

Mark Hard wrote:
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message

>> If there will only be 5 or 6 of them, why not just extract them manually

>> nset_PickedSet2=SET{1}.nset_PickedSet2;
>> nset_PickedSet5=SET{2}.nset_PickedSet5;
>> etc...

> Well the number of the set can change everytime depending on the
> automatically generated text file. so I won't know the names I need to
> variables to be to input it into the code.

If you do manage to create them as individual variables and you don't
know the names ahead of time, how is your code going to know which
variables to manipulate? Are you going to use a who bunch of
exist('PickedSet2','var') calls, or are you going to use who() and run
through the output? Wouldn't it be easier to just keep the structure and
use fieldnames() to know what got read in?

Subject: Extracting variables from a structure?

From: Mark Hard

Date: 9 Feb, 2010 16:44:04

Message: 10 of 12

Walter Roberson <roberson@hushmail.com> wrote in message <hks017$5ua$1@canopus.cc.umanitoba.ca>...
> Mark Hard wrote:
> > "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
>
> >> If there will only be 5 or 6 of them, why not just extract them manually
>
> >> nset_PickedSet2=SET{1}.nset_PickedSet2;
> >> nset_PickedSet5=SET{2}.nset_PickedSet5;
> >> etc...
>
> > Well the number of the set can change everytime depending on the
> > automatically generated text file. so I won't know the names I need to
> > variables to be to input it into the code.
>
> If you do manage to create them as individual variables and you don't
> know the names ahead of time, how is your code going to know which
> variables to manipulate? Are you going to use a who bunch of
> exist('PickedSet2','var') calls, or are you going to use who() and run
> through the output? Wouldn't it be easier to just keep the structure and
> use fieldnames() to know what got read in?

Thanks for the reply. The same variables names should be found again later on in the text file. When textscan finds that sting containing the variable name it will look at the sting after it in the text file and work on the variable accordingly. That will be my next step presuming I can get this working.

Subject: Extracting variables from a structure?

From: us

Date: 9 Feb, 2010 17:42:05

Message: 11 of 12

"Mark Hard" <GerryTheLeper@hotmail.com> wrote in message <hks3ck$h7$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <hks017$5ua$1@canopus.cc.umanitoba.ca>...
> > Mark Hard wrote:
> > > "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> >
> > >> If there will only be 5 or 6 of them, why not just extract them manually
> >
> > >> nset_PickedSet2=SET{1}.nset_PickedSet2;
> > >> nset_PickedSet5=SET{2}.nset_PickedSet5;
> > >> etc...
> >
> > > Well the number of the set can change everytime depending on the
> > > automatically generated text file. so I won't know the names I need to
> > > variables to be to input it into the code.
> >
> > If you do manage to create them as individual variables and you don't
> > know the names ahead of time, how is your code going to know which
> > variables to manipulate? Are you going to use a who bunch of
> > exist('PickedSet2','var') calls, or are you going to use who() and run
> > through the output? Wouldn't it be easier to just keep the structure and
> > use fieldnames() to know what got read in?
>
> Thanks for the reply. The same variables names should be found again later on in the text file. When textscan finds that sting containing the variable name it will look at the sting after it in the text file and work on the variable accordingly. That will be my next step presuming I can get this working.

yet another and very strong reason to use the STRUCT approach(!)...
this will make your next task extremely(!) simple...

us

Subject: Extracting variables from a structure?

From: Alexandra Chau

Date: 9 Feb, 2010 18:19:03

Message: 12 of 12

> > > Well the number of the set can change everytime depending on the
> > > automatically generated text file. so I won't know the names I need to
> > > variables to be to input it into the code.
> >
> > If you do manage to create them as individual variables and you don't
> > know the names ahead of time, how is your code going to know which
> > variables to manipulate? Are you going to use a who bunch of
> > exist('PickedSet2','var') calls, or are you going to use who() and run
> > through the output? Wouldn't it be easier to just keep the structure and
> > use fieldnames() to know what got read in?
>
> Thanks for the reply. The same variables names should be found again later on in the text file. When textscan finds that sting containing the variable name it will look at the sting after it in the text file and work on the variable accordingly. That will be my next step presuming I can get this working.

Solving problems like yours can be very confusing at first, especially when you don't know where to start. Now that you've seen a couple of solutions, I too would like to emphasize that this is a perfect scenario for using structures with dynamic field names. If you open the Matlab help and search for "creating field names dynamically", it will bring you to the page on structures, and hopefully to the part on dynamic field names, which will you give you a slightly better idea of how useful this can be.

When you're just doing some initial exploration of your data, creating simple variables like "Picked2 = [1 8 1]" is intuitive and straightforward, but when you get to the point of coding a solution that you will run on many many files, using a structure with dynamic field names can potentially really simplify your code and make it easier to debug.

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
dynamic field n... Alexandra Chau 9 Feb, 2010 13:24:06
poofing Matt J 9 Feb, 2010 10:16:29
struct us 9 Feb, 2010 07:29:08
fieldnames us 9 Feb, 2010 07:29:08
struct2cell us 9 Feb, 2010 07:29:08
indexing us 9 Feb, 2010 06:29:09
struct2array us 9 Feb, 2010 06:29:09
cellfun us 9 Feb, 2010 06:29:09
code us 9 Feb, 2010 06:29:09
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com