Plotting data from struct

392 ビュー (過去 30 日間)
Lizan
Lizan 2014 年 9 月 16 日
コメント済み: Steven Lord 2018 年 3 月 7 日
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 件のコメント
Lizan
Lizan 2014 年 9 月 16 日
編集済み: Lizan 2014 年 9 月 16 日
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?

サインインしてコメントする。

採用された回答

Michael Haderlein
Michael Haderlein 2014 年 9 月 16 日
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 件のコメント
Hasti Yavari
Hasti Yavari 2018 年 3 月 7 日
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 2018 年 3 月 7 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Iain
Iain 2014 年 9 月 16 日
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 件のコメント
Lizan
Lizan 2014 年 9 月 16 日
With
someStruct.c'
I get "Error using ' Too many input arguments".

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by