How to :Data cursor displaying colorbar value?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi guys,
Currently I have a 2D scatter plot with 3 column vectors. They are Pj, ecc and Combo.bresult .The third column is represented by a color bar.
I am lost as to how to display the third column value(the color bar value) when using data cursor mode. Currrently it only shows the x and y coordinate.
Thanks !
採用された回答
Walter Roberson
2017 年 5 月 6 日
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
function output_txt = myfunction(~, event_obj, Pj, ecc, bresult)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (Pj - cursor_pos(1)).^2 + (ecc - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('Pj: %g', Pj(item_idx)), sprintf('ecc: %g', ecc(item_idx)), sprintf('br: %g', bresult(item_idx)) };
10 件のコメント
Dean Kueh
2017 年 5 月 7 日
Hi there, thanks for responding. However, I am getting an error saying "Error in custom datatip string function"
I've also edited the code to suit my results
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1)) );
function output_txt = myfunction(~, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (1./Pj(1,:) - cursor_pos(1)).^2 + (ecc(1,:) - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('Pj: %g', 1./Pj(1,:)(item_idx)), sprintf('ecc: %g', ecc(1,:)(item_idx)), sprintf('br: %g', Combo.bresult(:,1)(item_idx)) };
I went to data cursor and "Select text update function"
The function definition line
function output_txt = myfunction(~, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
is not valid. You need to give only variables or ~ on that line.
You have already computed 1./Pj and Combo.bresult by way of the
@(hObject, event_obj) myupdatefcn(hObject, event_obj, 1./Pj(1,:), ecc(1,:), Combo.bresult(:,1))
so receive those values as variables like the way I showed and just use the variables instead of continually recalculating.
Dean Kueh
2017 年 5 月 7 日
I've tried it and it still doesn't work..
Is it because my results are not in a column 1 matrix in each variable?
There are multiple columns within Pj and ecc so I have to select the coulumn that I need.
Just some minor function name problems.
Example:
Pj = rand(1,30); ecc = rand(1,30); Combo.bresult = randi(5,1,30);
pointsize = 20;
scatter(Pj, ecc, pointsize, Combo.bresult);
colorbar;
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
function output_txt = myupdatefcn(~, event_obj, Pj, ecc, bresult)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (Pj - cursor_pos(1)).^2 + (ecc - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('Pj: %g', Pj(item_idx)), sprintf('ecc: %g', ecc(item_idx)), sprintf('br: %g', bresult(item_idx)) };
Dean Kueh
2017 年 5 月 7 日
I tried and still getting the same error..
what am i doing wrong? Here is my code on plotting the figure.
%sigma ci
figure;
scatter(ecc(1,:),1./Pj(1,:), sz, Combo.bresult(:,1),'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, Pj, ecc, Combo.bresult) );
hold on
plot(x_ti,y_ti_plot,x_ci,y_ci_plot,x_ts,y_ts_plot,x_cs,y_cs_plot);
caxis([0 3]);
hold off
axis([-850 -100 3e-8 15e-8]);
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
sz = 20;
%sigma ci
figure;
X = ecc(1,:);
Y = 1./Pj(1,:);
Z = Combo.bresult(:,1)
scatter(X, Y, sz, Z, 'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z) );
hold on
plot(x_ti, y_ti_plot, x_ci, y_ci_plot, x_ts, y_ts_plot, x_cs, y_cs_plot);
caxis([0 3]);
hold off
axis([-850 -100 3e-8 15e-8]);
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
function output_txt = myupdatefcn(~, event_obj, X, Y, Z)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (X - cursor_pos(1)).^2 + (Y - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('ecc: %g', X(item_idx)), sprintf('1/Pj: %g', Y(item_idx)), sprintf('br: %g', Z(item_idx)) };
Dean Kueh
2017 年 5 月 7 日
Thanks a bunch man, I appreciate your help but its still giving me the same error.
"Error in custom data string function"
Example, tested:
N = 50;
ecc = randn(1,N);
Pj = exp(randn(1,N));
Combo.bresult = cos(sin(ecc .* Pj * 1i)) .';
sz = 20;
%sigma ci
figure;
X = ecc(1,:);
Y = 1./Pj(1,:);
Z = Combo.bresult(:,1);
scatter(X, Y, sz, Z, 'filled')
xlabel('Eccentricity');
ylabel('1/P_{j}');
dcm_obj = datacursormode();
set(dcm_obj,'UpdateFcn',@(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z) );
hold on
%{
plot(x_ti, y_ti_plot, x_ci, y_ci_plot, x_ts, y_ts_plot, x_cs, y_cs_plot);
caxis([0 3]);
%}
hold off
%{
axis([-850 -100 3e-8 15e-8]);
%}
title('Reliability index of P vs e (\sigma_{ci})');
Pj_dc = Pj(1,:) ;
ecc_dc = ecc(1,:);
Combo.bresult_dc = Combo.bresult(:,1);
function output_txt = myupdatefcn(~, event_obj, X, Y, Z)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (X - cursor_pos(1)).^2 + (Y - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf('ecc: %g', X(item_idx)), sprintf('1/Pj: %g', Y(item_idx)), sprintf('br: %g', Z(item_idx)) };
omg it worked!
Do I have to edit the function file for every different graph that I plot? I have another 4 of the same plots but different color bar columns
That is different columns within the Combo.bresult vector matrix.
You can pass the text formats to use into mypdatefcn to generalize it
set(dcm_obj, 'UpdateFcn', @(hObject, event_obj) myupdatefcn(hObject, event_obj, X, Y, Z, {'ecc: %g', '1/Pj: %g', 'br: %g'}) );
and
function output_txt = myupdatefcn(~, event_obj, X, Y, Z, formats)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
%find the closest data point to the cursor
cursor_pos = event_obj.Position;
dist2_to_dots = (X - cursor_pos(1)).^2 + (Y - cursor_pos(2)).^2;
[~, item_idx] = min(dist2_to_dots);
output_txt = {sprintf(formats{1}, X(item_idx)), formats{2}, Y(item_idx)), sprintf(formats{3}, Z(item_idx)) };
However, note that you can only have one active dcm object per figure, so if you want to be able to point to different lines and have different text labels come up depending on what was being pointed to, then you need to code a single update function that handles everything simultaneously.
One approach to handle multiple plots simultaneously would be to set the UserData property of each drawn object to have the associated extra data and the text formats to use; then the data cursor update function would have to figure out which object it was near, and retrieve the appropriate information to update the display. See related https://www.mathworks.com/matlabcentral/answers/219797-check-if-mouse-is-above-an-axes#answer_180771
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Discrete Data Plots についてさらに検索
参考
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)
