Plotting data from struct
277 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
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
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
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])
更多回答(1 个)
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!