Efficient way to plot data from a cell array?

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 日

0 投票

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,
Unfortunately that doesn't do the trick, because the lines all end up with the same color, unlike the plot command when used as in my example which uses one color per line. Also, I've yet to find a way to collect all the handles from the plot commands using your arrayfun example.
Birdman
Birdman 2018 年 1 月 23 日
When I run the following code
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};
hold on
arrayfun(@(x) plot(x{:}(:,1),x{:}(:,2)),data)
I get the attached figure, which you can see datas are plotted in different colors.
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).
Birdman
Birdman 2018 年 1 月 23 日
You are welcome :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

質問済み:

2018 年 1 月 22 日

コメント済み:

2018 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by