Issue in using splitapply to plot data

조회 수: 4 (최근 30일)
vanrapa
vanrapa 2020년 3월 23일
댓글: Star Strider 2020년 3월 24일
Hi,
I have an array with 14 columns. Column 3 contains groups of numbers. I would like to plot column 1 vs. column 4 for each group of column 3. I have written the following code,
G = findgroups(val(:, 3));
splitapply(@plot, val(:, 1), val(:, 4), G);
From my data, I should be getting 6 plots within the figure. But I am getting only a single plot. So what is the issue in my code?
Also how to write legend using splitapply?
Thanks.

채택된 답변

Star Strider
Star Strider 2020년 3월 23일
One approach:
D = load('val.mat');
val = D.val;
G = findgroups(val(:, 3));
figure
hold on
Y = splitapply(@(x){plot(x(:,[1 4]))}, val, G);
hold off
Another approach:
D = load('val.mat');
val = D.val;
G = findgroups(val(:, 3));
Y = splitapply(@(x){x(:,[1 4])}, val, G);
figure
hold on
for k = 1:numel(Y)
plot(Y{k})
end
hold off
grid
legend(compose('Group %d', 1:6), 'Location','eastoutside')
I prefer the second approach, however it is possible to use plot with splitapply, as the first approach demonstrates.
  댓글 수: 8
vanrapa
vanrapa 2020년 3월 24일
Channels will be in val(;,3) and it is working. Thanks
Star Strider
Star Strider 2020년 3월 24일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by