plotting a variable size of dataset
13 次查看(过去 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.
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!