Thread Subject: Struct 2 cell question

Subject: Struct 2 cell question

From: James

Date: 27 Jan, 2012 02:35:10

Message: 1 of 4

Is there a simple way to convert a struct array to a set of cell arrays with the field names as the variable names? Also the field names are dynamic as they are coming from a data file that can change.

For example:
I have:
S.EngSped = 500 510 520 510 530 525
S.pressure = 200 100 50 90 150 120
S.temp = 20 40 10 90 70 50

I want
EngSped = {500 510 520 510 530 525}
pressure = {200 100 50 90 150 120}
temp = {20 40 10 90 70 50}

Subject: Struct 2 cell question

From: Steven_Lord

Date: 27 Jan, 2012 14:32:28

Message: 2 of 4



"James " <cress_james@cat.com> wrote in message
news:jft2gu$gaa$1@newscl01ah.mathworks.com...
> Is there a simple way to convert a struct array to a set of cell arrays
> with the field names as the variable names? Also the field names are
> dynamic as they are coming from a data file that can change.
> For example:
> I have:
> S.EngSped = 500 510 520 510 530 525
> S.pressure = 200 100 50 90 150 120
> S.temp = 20 40 10 90 70 50
>
> I want
> EngSped = {500 510 520 510 530 525}
> pressure = {200 100 50 90 150 120}
> temp = {20 40 10 90 70 50}

Don't do this. It will make your code slower and harder to read and could
cause unexpected behavior that will be hard to diagnose. See question 6 in
the Programming section of the FAQ for additional explanation.

http://matlab.wikia.com/wiki/FAQ

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Struct 2 cell question

From: James

Date: 28 Jan, 2012 01:44:10

Message: 3 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <jfuchr$hcm$1@newscl01ah.mathworks.com>...
>
>
> "James " <cress_james@cat.com> wrote in message
> news:jft2gu$gaa$1@newscl01ah.mathworks.com...
> > Is there a simple way to convert a struct array to a set of cell arrays
> > with the field names as the variable names? Also the field names are
> > dynamic as they are coming from a data file that can change.
> > For example:
> > I have:
> > S.EngSped = 500 510 520 510 530 525
> > S.pressure = 200 100 50 90 150 120
> > S.temp = 20 40 10 90 70 50
> >
> > I want
> > EngSped = {500 510 520 510 530 525}
> > pressure = {200 100 50 90 150 120}
> > temp = {20 40 10 90 70 50}
>
> Don't do this. It will make your code slower and harder to read and could
> cause unexpected behavior that will be hard to diagnose. See question 6 in
> the Programming section of the FAQ for additional explanation.
>
> http://matlab.wikia.com/wiki/FAQ
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Yeah I kinda figured that out the hard way. But in the midst of committing Harikari I actually figured out a pretty cool trick to do what I wanted. So to go a little deeper into my problem I actually had a number of data sets that may or may not have the same variables in it that I needed to be put together in a single data set. With some work I was able to filter out the fieldnames that weren't common and load them into a struct. So now I have a struct with that still doesn't have the data as a continuous data set so I converted it to a cell array and merged the data into a continuous data set. I then converted the cell array to a struct with cell to struct. Here is the trick I found .... If I save the struct as a mat file and then reload it I end up with all my variables loaded with the data.

Subject: Struct 2 cell question

From: Steven_Lord

Date: 31 Jan, 2012 15:39:44

Message: 4 of 4



"James " <cress_james@cat.com> wrote in message
news:jfvjta$saq$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <jfuchr$hcm$1@newscl01ah.mathworks.com>...

*snip*

> Yeah I kinda figured that out the hard way. But in the midst of committing
> Harikari I actually figured out a pretty cool trick to do what I wanted.
> So to go a little deeper into my problem I actually had a number of data
> sets that may or may not have the same variables in it that I needed to be
> put together in a single data set. With some work I was able to filter out
> the fieldnames that weren't common and load them into a struct. So now I
> have a struct with that still doesn't have the data as a continuous data
> set so I converted it to a cell array and merged the data into a
> continuous data set. I then converted the cell array to a struct with cell
> to struct. Here is the trick I found .... If I save the struct as a mat
> file and then reload it I end up with all my variables loaded with the
> data.

Yes, and that "trick" is a BAD thing.

Suppose your MAT-file myMatFile.mat contains a variable named alpha that is
the scalar value 5. You would expect this code to display the value 5,
right?

function myfunction
load('myMatFile.mat');
disp(alpha)

It doesn't. Instead you get an error about calling ALPHA with too few input
arguments. You "poofed" the variable alpha into the workspace at runtime.
However, MATLAB has already parsed the function file to try to determine to
what each identifier referred and when MATLAB parsed the function file it
"decided" that alpha was a reference to the ALPHA function.

http://www.mathworks.com/help/techdoc/ref/alpha.html

Instead of "poofing" variables, do this:

function myfunction
mydata = load('myMatFile.mat');
if isfield(mydata, 'alpha')
    disp(mydata.alpha);
end

or:

function myfunction
mydata = load('myMatFile.mat');
try
    disp(mydata.alpha);
catch
    % alpha wasn't a variable in myMatFile.mat
    % handle this appropriately
end

FYI, avoiding "poofing" is item 7 on Loren's list of best practices.

http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com