Issue in using splitapply to plot data
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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
2020 年 3 月 23 日
2nd approach is better to include loop variables in legend
Star Strider
2020 年 3 月 23 日
You can probably include the legend in the first approach as well. I only included them in the second because I thought of it only then.
for k = 1:numel(Y)
plot(Y{k})
legendLabel{k}= ['Channel' num2str(k)]
end
legend(legendLabel)
I used the above code for legend. But even then, here, since my column 3 data contains values between 1 to 6, this code is fine. But if instead of 1 to 6, if I contain values of 1 to 5 and 8, even then the legend will show only Channel 6 because the code uses 'numel'. So I don't know how to extract the value using which the data is split into 6 groups using splitapply. I want to include that value in legend. Using 'unique(G)' doesnt seem to help.
I dont seem to know how to do that using 1st or 2nd method.
Star Strider
2020 年 3 月 24 日
If the group numbers are 1:5 and 8, then the code changes to:
GN = [1:5 8];
legend(sprintfc('Channel %d',GN))
You can avoid the loop by using the undocumented sprintfc function. Everyone has it.
The full code then becomes:
G = findgroups(val(:, 3));
figure
hold on
Y = splitapply(@(x){plot(x(:,[1 4]))}, val, G);
hold off
grid
GN = [1:5 8];
legend(sprintfc('Channel %d',GN))
vanrapa
2020 年 3 月 24 日
Thanks. Didn't know about the sprintfc before. But [1:5 8] or [1:6] is not fixed. The number of channels and the numbers of the channel will vary in each file. So I have to write code for a generic legend. So how to do that?
Star Strider
2020 年 3 月 24 日
I have no information with respect to how the channels are designated in the file. If they are in ‘val(:,3)’, this could work:
GN = unique(val(:,3),'stable');
legend(sprintfc('Channel %d',GN))
If they are not defined in the file, then it would be necessary to hard-code them individually for each file, or provide some sort of pre-defined hard-coded look-up table to define them.
vanrapa
2020 年 3 月 24 日
Channels will be in val(;,3) and it is working. Thanks
Star Strider
2020 年 3 月 24 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Legend についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
