Efficient way to plot data from a cell array?

24 次查看(过去 30 天)
Hi everyone,
I've been struggling with finding a way to efficiently plot data which is stored in a cell array.
First of all, by efficiently I really mean to make the code understandable and maintainable later.
My data is organized as data series with some data (y) stored at different temperatures (x). I have several such data series, and since each data series might have various numner of samples, I've stored them in a cell array.
So data = {ds1 ds2 ds3 ds4 ...}, where each dsX is an Nx2 matrix containing my x and y values, N varies between data series.
Example:
ds1 = [ (1:4)' randn(4,1) ]
ds2 = [ (1:0.5:5)' randn(9,1) ]
ds3 = [ (2:0.1:3)' randn(11, 1) ];
data = {ds1 ds2 ds3}
Next, what I want to do with the data is: in one figure, for each dsX, plot dsX(:,2) vs dsX(:,1).
Actually I want the output to be the same as I would get from using plot like this:
plot(ds1(:,1), ds1(:,2), ds2(:,1), ds2(:,2), ds3(:,1), ds3(:,2), ...)
Preferable I would also like the handles to the lineseries objects, just as I would by adding h = before the call to plot above.
Codewise I don't know how many data series are stored in my cell array, so I cannot use the plot command directly as shown above.
I would really appreciate any suggestions to how I can efficiently plot my data as described. I can also consider proposals for how to organize my data in the first place.

采纳的回答

Birdman
Birdman 2018-1-22
编辑:Birdman 2018-1-22
This should do it for you:
hold on
arrayfun(@(x) plot(x{:}(:,1),x{:}(:,2)),data)
  4 个评论
Lus Kem
Lus Kem 2018-1-23
Hi and thanks again!
I see... I only get blue lines running that code. (attached)
Guessing your code works due to added functionality in "newer" MATLAB versions, I'm currently on 7.11.0 (R2010b) due to various compatibility reasons. (Lots of code needs rewriting to move to a newer version of MATLAB).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by