Extracting data from structure arrays within a structure array

1 view (last 30 days)
I don’t know if this can be done.
So I have a 1X100 structure array called "data1" Each element IN "data" is 1X1 structure. In each of those 1X1 structure I have the following categories:
Field: id, type, time, temp Value data example: '1230', 'thermometer', a 1X24 double containing the times that the measurements were taken, and a 1X24 double (containing the measurements).
For each of the 1X1 structure elements in "data1", the "id" Field is always contains a Value that is a string of numbers or letters. The "type" Field always contains a Value that is a string of letters spelling out what the measuring device is (aka: a thermometer, a thermocouple, etc). The times and measurements Fields contain anywhere from a 1X24 double all the way up to a 1X243 double.
Each measurement of temperature was taken over a 24 hour period. Some were taken every 7-10 minutes, some were taken every hour.
What I need to do is a few things:
  1. Extract all the times and temperature measurements from this structure array for each of the different locations specified by "id"
  2. take ANY of the structure elements from data1 that contains a time and measurement field which has Values of 1X24, then append ALL the other times and temperatures to the times of THAT structure element.
  3. I would want to end up with two matrices of data:- A 24X100 matrix with all the times- A 24X100 matrix with all the corresponding temperatures.
If anyone has an easier method of doing this, please help me out. Also, if somebody knows how I could do this impossible (for me) problem, let me know
Any help is extremely appreciated. Mike
  2 Comments
Sara
Sara on 10 Jul 2014
Are you reading in the value from a file or do you load the structure?
Michael Ant
Michael Ant on 11 Jul 2014
The structure is created from a function that a coder gave me.
I dont understand the code that he wrote to create the structure.
Mike

Sign in to comment.

Accepted Answer

per isakson
per isakson on 11 Jul 2014
Edited: per isakson on 11 Jul 2014
The question is somewhat ambiguous. A small example would have been helpful.
  • "anywhere from a 1X24 double all the way up to a 1X243 double" . To concatenate vectors to an array, all vectors must have the same length, i.e the vectors with the same value of ID.
  • "A 24X100 matrix with all the times" . "100" implies that all cases have the same value of ID.
  • "easier method of doing this" and "do this impossible" . These two "this" refers to different things(?).
Anyhow, the following code solves the special case with equal length of all the vectors
Create some input data
n = 6; % length of the main structure, data1
m = 8; % length of the vectors, time and temp
sas = repmat( ...
struct('f',struct( 'id','','type','' ...
, 'time',nan(1,m) ...
, 'temp',nan(1,m)) ) ...
, 1, n );
for ii = 1 : n
sas(1,ii).f.id = sprintf( 'A%d', randi([1,3],1) );
sas(1,ii).f.type = 'thermometer';
sas(1,ii).f.time = [ 1 : m ];
sas(1,ii).f.temp = ii + 0.1*[1:m];
end
%%Concatenate the vectors
cac = arrayfun( @(s) s.f.id, sas, 'uni', false );
isi = strcmp('A2',cac) ; % select ... with id=='A2'
tmp = arrayfun( @(s) transpose( s.f.temp ), sas(isi), 'uni', false );
tim = arrayfun( @(s) transpose( s.f.time ), sas(isi), 'uni', false );
Temp = cell2mat( tmp )
Time = cell2mat( tim )
it returned
Temp =
2.1000 3.1000 6.1000
2.2000 3.2000 6.2000
2.3000 3.3000 6.3000
2.4000 3.4000 6.4000
2.5000 3.5000 6.5000
2.6000 3.6000 6.6000
2.7000 3.7000 6.7000
2.8000 3.8000 6.8000
Time =
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
The number of columns depends on the result of randi([1,3],1)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!