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.
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 Exchange에서 2-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