Concatenating data from structure array for plotting

I have a structure array with several levels of depth that I am trying to extract a specific set of data from in order to plot it. The plan is to draw individual elements from several different fields, and then combine them into a single array for plotting. I know that normally structures can be concatenated using [], but it doesn't seem to be working for this instance.
plot(x_values,[files([files.Param] == condition).Zone(1).Values(1,4)]);
Expected one output from a curly brace or dot indexing expression, but there were multiple results.
I know that the inner conditional statement is working, as I can extract it and get the appropriate logic matrix, but that means that I am looking at multiple elements of 'files' (which is exactly what I want to do), but they do not seem to be concatenating properly. Any suggestions would be appreciated.

 채택된 답변

Stephen23
Stephen23 2019년 1월 8일
편집: Stephen23 2019년 1월 8일

0 개 추천

댓글 수: 9

Ok, so, now that I've had a chance to review those pages, basically gathering the data the way I would like is not possible without using loops to call the individual pieces, because arrays work off comma separated lists, and those don't combine well with structures. Typically, your suggestion, therefore, of a more efficient way to accomplish this type of data management is to use multidimensional arrays with only a single level of structure. Does that sound about correct?
You refer to non-scalar structures in one of the posts. What are those, and how do they differ from regular structures?
In your post here you mention creating a temporary variable. I don't know that I've ever done that intentionally before, so would you be willing to show me an example?
"your suggestion, therefore, of a more efficient way to accomplish this type of data management is to use multidimensional arrays with only a single level of structure. Does that sound about correct?"
Yes. MATLAB code is usually simplest and most efficient when using arrays. Nested structures are usually not worth the bother. Structures (scalar or non) are quite handy though.
"You refer to non-scalar structures in one of the posts. What are those, and how do they differ from regular structures?"
All structures are exactly the same type of object, the only difference is the size: a scalar structure is scalar, a non-scalar structure is not scalar. I use this term because it is used in the documentation:
MATLAB does not actually have special "scalar" data classes:
For example, this is a scalar double:
1
but because all scalars are arrays that scalar double is actually just a 1x1x1x1x... double array, and it has exactly the same class as any other AxBxCx... double array. In exactly the same way this scalar structure
S.blah = 1
is exactly the same class as any non-scalar structure with size AxBxCx... the only difference is the size. When you are using a comma separated list this makes a difference because some syntaxes are easiest to use with scalar structures. Really many operations on scalar structures are just degenerate cases of comma separated lists... but I digress.
"In your post here you mention creating a temporary variable. I don't know that I've ever done that intentionally before, so would you be willing to show me an example?"
>> S(1).A.B = 1;
>> S(2).A.B = 2;
>> tmp = [S.A];
>> tmp.B
ans = 1
ans = 2
Where S.A.B would throw an error. Read more here:
Ok, yeah. I am already largely using non-scalar structures, since I have multiple structures under 'files', 'zones', and some other stuff.
Note this will not work for a non-scalar S. Read more here:
If I understand this correctly, then because I already have non-scalar structures then the temporary variable won't work. Any other suggestions before I restructure my variable to remove the nested structure?
" I am already largely using non-scalar structures, since I have multiple structures under 'files', 'zones', and some other stuff."
What you descibe "I have multiple structures under 'files'" are nested structures (which may or may not be scalar). You seem to be confusing nested structures (structures inside structures, like you described) and non-scalar structures (structures with non-scalar size).
"If I understand this correctly, then because I already have non-scalar structures then the temporary variable won't work. Any other suggestions before I restructure my variable to remove the nested structure?"
I have no idea what would work because nowhere have you given any information on the sizes of your structures. For a start, what size is the files structure? What sizes are the nested Zone structures?
The size is not specific, but should follow something like this:
files (1x5) struct
- name (1x1) char
- param (1x1) double
- zone (1x4) struct
-- name (1x1) char
-- number (1x1) double
-- Max (5x4) double
-- Average (5x1) double
What is the size of the Values field?
Please describe which data combination you want to extract from this structure. In your question you seem to want to extract this:
  1. N elements of files (determined by a logical array), followed by
  2. one element of the structure Zone, followed by
  3. one element of the array Values.
That would mean getting one value for each element of files, so it appears that you were hoping to concatenate those N values into one numeric vector. Is that correct?
Sorry, 'values' can be either 'Max' or 'Average'. I didn't refer back to my original post when I listed the sizes of the structure elements.
That would mean getting one value for each element of files, so it appears that you were hoping to concatenate those N values into one numeric vector. Is that correct?
Yes, that is correct.
As I mentioned earlier, working with nested functions is not much fun.
You could try something like this:
fun = @(s) s.Zone(1).Values(1,4);
V = arrayfun(fun,files([files.Param]==condition))
Bob Thompson
Bob Thompson 2019년 1월 9일
편집: Bob Thompson 2019년 1월 9일
That should be sufficient for this, thanks.
And in the future I know better how to avoid this whole situation, so double thanks.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

질문:

2019년 1월 8일

편집:

2022년 8월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by