How to :Data cursor displaying colorbar value?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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)) };
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)) };
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
