I recently found that you can pull the data from a single
scalar field of a structure array, and store it into a
scalar array, with
a=cat(1,s.f)
You might try struct2array(), along with reshape(). You
could also write your own function to convert an array of
structures into a structure of arrays, using fields() to
extract the field names as strings, and s.('f') to access
the fields with strings.
"Markthomas " <uebermenchens@yahoo.com> wrote in message
<g6515u$5op$1@fred.mathworks.com>...
> I wish to pull out data from a structure array and put it
> into csv files but i was wondering if there was an easy way
> to do this:
>
> for i = 1:6
> timestpD = daq(i).data.timestamp.data
> speed = daq(i).data.speed.data
> x = daq(i).data.x.data
> end
>
> I have more fields than timestamp, speed and x... so i was
> looking for a better way to do this!
>
> Thanks for your help on everything
>
> MtC
lets say i create a matrix:
fields = [time, speed, x]
there is no way to do:
time = daq(i).data.<something like fields(k)>.data ??
MtC
"Alan B" <monguin61@yahoo.com> wrote in message
<g651qi$cpf$1@fred.mathworks.com>...
> I recently found that you can pull the data from a single
> scalar field of a structure array, and store it into a
> scalar array, with
>
> a=cat(1,s.f)
>
> You might try struct2array(), along with reshape(). You
> could also write your own function to convert an array of
> structures into a structure of arrays, using fields() to
> extract the field names as strings, and s.('f') to access
> the fields with strings.
>
>
> "Markthomas " <uebermenchens@yahoo.com> wrote in message
> <g6515u$5op$1@fred.mathworks.com>...
> > I wish to pull out data from a structure array and put
it
> > into csv files but i was wondering if there was an easy
way
> > to do this:
> >
> > for i = 1:6
> > timestpD = daq(i).data.timestamp.data
> > speed = daq(i).data.speed.data
> > x = daq(i).data.x.data
> > end
> >
> > I have more fields than timestamp, speed and x... so i
was
> > looking for a better way to do this!
> >
> > Thanks for your help on everything
> >
> > MtC
>
In article <g654ir$fqa$1@fred.mathworks.com>,
Markthomas <uebermenchens@yahoo.com> wrote:
>lets say i create a matrix:
>fields = [time, speed, x]
>there is no way to do:
>time = daq(i).data.<something like fields(k)>.data ??
No.
fields = {'time' 'speed' 'x'};
time = [];
for K=1:length(fields)
time = [time daq(i).data.(fields{K}).data];
end
If you examine the documentation for subsref() you will see that
what you are hoping to do is not directly possible in Matlab.
--
"There's no term to the work of a scientist." -- Walter Reisch
"Markthomas " <uebermenchens@yahoo.com> wrote in message
<g654ir$fqa$1@fred.mathworks.com>...
> lets say i create a matrix:
> fields = [time, speed, x]
>
> there is no way to do:
>
> time = daq(i).data.<something like fields(k)>.data ??
>
> MtC
>
I'm a little confused by your code - is the 'fields' matrix
filled with numerical data, or with the field names as
strings? Is the 'time' on the LHS of the assignment
statement meant to refer to the first column of the 'fields'
matrix, or to a new variable? If its the latter, you should
be able to use a cell array:
"Markthomas " <uebermenchens@yahoo.com> wrote in message
<g654ir$fqa$1@fred.mathworks.com>...
> lets say i create a matrix:
> fields = [time, speed, x]
>
> there is no way to do:
>
> time = daq(i).data.<something like fields(k)>.data ??
>
> MtC
>
"Alan B" <monguin61@yahoo.com> wrote in message
<g658gb$r1n$1@fred.mathworks.com>...
> I'm a little confused by your code - is the 'fields'
matrix
> filled with numerical data, or with the field names as
> strings? Is the 'time' on the LHS of the assignment
> statement meant to refer to the first column of
the 'fields'
> matrix, or to a new variable? If its the latter, you
should
> be able to use a cell array:
>
> fields = {'time','speed','x'};
>
> timeNew = daq(i).data.(fields{1}).data
> speedNew = daq(i).data.(fields{2}).data
>
> etc
>
>
>
>
> "Markthomas " <uebermenchens@yahoo.com> wrote in message
> <g654ir$fqa$1@fred.mathworks.com>...
> > lets say i create a matrix:
> > fields = [time, speed, x]
> >
> > there is no way to do:
> >
> > time = daq(i).data.<something like fields(k)>.data ??
> >
> > MtC
> >
>
sorry for the confusion but the fields are strings
i sould have put the '' around them!
"Markthomas " <uebermenchens@yahoo.com> wrote in message
<g654ir$fqa$1@fred.mathworks.com>...
> lets say i create a matrix:
> fields = [time, speed, x]
>
> there is no way to do:
>
> time = daq(i).data.<something like fields(k)>.data ??
>
May be something like this?:
% Generate data
for i = 1:6
daq(i).data.timestamp.data = i;
daq(i).data.speed.data = i+1;
daq(i).data.x.data = -i;
end
In article <g65e2d$s7s$1@fred.mathworks.com>,
Markthomas <uebermenchens@yahoo.com> wrote:
>Is there a way that i can save the "fields" as a header in
>csv format. I was trying to do it and i get a csv file
>that looks like:
>t,i,m,e....
I gather (perhaps incorrectly) that there is a way to do it when
you are using xlswrite() talking directly to an Excel DCOM instance
on MS Windows.
Short of that, you have to tell csvwrite() {aka dlmwrite()}
to use '' as the cell delimeter, and then everywhere that
you actually -want- a cell delimeter, put the comma in "manually".
If you are working with a mix of text and numerics this may require
converting the numerics to text in a prior step: If you
want csvwrite() or dlmwrite() to put complete strings into fields,
then you have to pass it pure text that has all of the field delimeters
already written in the text.
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g65htt$6m3$1@canopus.cc.umanitoba.ca>...
> In article <g65e2d$s7s$1@fred.mathworks.com>,
> Markthomas <uebermenchens@yahoo.com> wrote:
>
> >Is there a way that i can save the "fields" as a header
in
> >csv format. I was trying to do it and i get a csv file
> >that looks like:
>
> >t,i,m,e....
>
> I gather (perhaps incorrectly) that there is a way to do
it when
> you are using xlswrite() talking directly to an Excel
DCOM instance
> on MS Windows.
>
> Short of that, you have to tell csvwrite() {aka dlmwrite
()}
> to use '' as the cell delimeter, and then everywhere that
> you actually -want- a cell delimeter, put the comma
in "manually".
> If you are working with a mix of text and numerics this
may require
> converting the numerics to text in a prior step: If you
> want csvwrite() or dlmwrite() to put complete strings
into fields,
> then you have to pass it pure text that has all of the
field delimeters
> already written in the text.
>
> --
> This is a Usenet signature block. Please do not quote it
when replying
> to one of my postings.
> http://en.wikipedia.org/wiki/Signature_block
This cell array is already in the structure. I pulled it
out such that i can read all the information i can from the
structure. There is no other way to do this without
reformatting the cell array?
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.