plotting a variable size of dataset
조회 수: 11 (최근 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!