Efficient way to plot data from a cell array?

25 vues (au cours des 30 derniers jours)
Lus Kem
Lus Kem le 22 Jan 2018
Commenté : Birdman le 23 Jan 2018
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.

Réponse acceptée

Birdman
Birdman le 22 Jan 2018
Modifié(e) : Birdman le 22 Jan 2018
This should do it for you:
hold on
arrayfun(@(x) plot(x{:}(:,1),x{:}(:,2)),data)
  4 commentaires
Lus Kem
Lus Kem le 23 Jan 2018
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 le 23 Jan 2018
You are welcome :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by