plotting a variable size of dataset

6 次查看(过去 30 天)
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.

采纳的回答

the cyclist
the cyclist 2021-9-2
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

更多回答(0 个)

类别

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

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by