plotting a variable size of dataset

11 visualizzazioni (ultimi 30 giorni)
Hello all,
The question is to ease my data plotting. Right now I have 6 sets of data array. Lets call them C1, C2, C3, V1, V2, V3. So I plot using the code
plot(V1, C1, 'b', V2, C2, 'b','linewidth',3); hold on;
plot(V3, C3, 'b', V4, C4, 'b','linewidth',3);
plot(V5, C5, 'b', V6, C6, 'b','linewidth',3);
The issue however is that I dont have the same number of data sets all the time. Instead of 6 sets, sometimes I have 4, sometimes I have 10. So each times I have to add extra plot lines of comment some lines out to get the figure.
My question is how can I plot them without adding/subtracting any lines. I am thinking using a for loop to call however many data arrays I have and have them plotted.
I would really appreciate some help.
Thanks all very much.

Risposta accettata

the cyclist
the cyclist il 2 Set 2021
Instead of naming your variables dynamically, which is almost always a bad idea, store your data in cell arrays. Then the length of the cell array will be the number of calls of the loop:
rng default
% Make up three datasets, stored in cell arrays
C{1} = sort(rand(3,1));
V{1} = rand(3,1);
C{2} = sort(rand(2,1));
V{2} = rand(2,1);
C{3} = sort(rand(5,1));
V{3} = rand(5,1);
% There could be more. Even if you uncomment this to get a new dataset, the
% plotting routine doesn't need to change.
% C{4} = sort(rand(5,1));
% V{4} = rand(5,1);
% Plot, looping over the cell array
figure
hold on
for n = 1:length(C)
plot(C{n},V{n})
end
  1 Commento
SAZZAD HOSSAIN
SAZZAD HOSSAIN il 2 Set 2021
Hello,
Thanks very much, its working. Really appreciate it.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Tag

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by