Plotting data from struct

조회 수: 394 (최근 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 CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by