Plotting data from struct

247 views (last 30 days)
Lizan
Lizan on 16 Sep 2014
Commented: Steven Lord on 7 Mar 2018
Hi,
I am plotting data from a struct, for example
plot(someStruct.a, someStruct.c)
The figure I obtain I can hardly see my data points. They are tiny dots of different colours when I write;
plot(someStruct.a, someStruct.c,'o-')
It seems to treat the data one by one and does not plot them all as instructed in code. Any idea how to make MATLAB plot the data above with one type of style, colour and size?
  1 Comment
Lizan
Lizan on 16 Sep 2014
Edited: Lizan on 16 Sep 2014
And it doesn't seem to plot the correct numbers for example
someStruct.a =
1
2
3
someStruct.c
-0.3
-0.6
-0.8
It seems as if the points (1,-0.3), (2,-0.6) and (3,-0.8) does not appear but instead (1,-0.8) and so on?

Sign in to comment.

Accepted Answer

Michael Haderlein
Michael Haderlein on 16 Sep 2014
If you have something like you suggest to have:
someStruct.a=1:10;
someStruct.c=rand(1,10);
plot(someStruct.a,someStruct.c)
then it's just working. However, I guess you have something different. Is someStruct an array and a,c just one scalar? I mean something like this:
someStruct(1).a=1;
someStruct(2).a=2;
someStruct(3).a=3;
someStruct(1).c=1;
someStruct(2).c=2;
someStruct(3).c=5;
plot(someStruct.a,someStruct.c)
There you can't see a lot. In this case, the solution is simply
plot([someStruct.a],[someStruct.c])
  3 Comments
Hasti Yavari
Hasti Yavari on 7 Mar 2018
This was the answer to my question too. However I don't understand the solution. Why does this allow me to plot the struct and not just the dot?
Steven Lord
Steven Lord on 7 Mar 2018
In this case when you type:
plot(someStruct.a, someStruct.c)
you're passing six inputs into plot, not two. You can see this more clearly by displaying the size of each input to a function. [In this case, displayAllInputs takes the place of plot.]
fh = @(x) fprintf('Input argument is of size %s.\n', mat2str(size(x)));
displayAllInputs = @(varargin) cellfun(fh, varargin);
Now let's build the non-scalar struct array.
someStruct(1).a=1;
someStruct(2).a=2;
someStruct(3).a=3;
someStruct(1).c=1;
someStruct(2).c=2;
someStruct(3).c=5;
When you call it without the square brackets, you'll see that displayAllInputs is called with six inputs.
displayAllInputs(someStruct.a, someStruct.c)
When you call it with the square brackets, you'll see that displayAllInputs is called with two inputs.
displayAllInputs([someStruct.a], [someStruct.c])
For more information read about comma-separated lists on this documentation page.

Sign in to comment.

More Answers (1)

Iain
Iain on 16 Sep 2014
plot(someStruct.a, someStruct.c,'k-o')
That's the answer to the question you're asking.
I suspect that ACTUALLY, you want this:
plot(someStruct.a', someStruct.c','k-o')
  1 Comment
Lizan
Lizan on 16 Sep 2014
With
someStruct.c'
I get "Error using ' Too many input arguments".

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!