필터 지우기
필터 지우기

update tool tip data using data cursor in loop

조회 수: 4 (최근 30일)
nirit
nirit 2019년 3월 6일
답변: nirit 2019년 3월 6일
Hi all.
I am plotting sevrals of plots all in one figure. I want to use custom tool tip that shows me each plot iteration origin.
but for some reason, when using the tool tip , it shows only the last iteration number on all of the plots.
my code bellow.
what am I dong wrong?
fig=figure;
hold on;
for i=1:4
x=sort(rand(1,100));
y=sin(2*pi*x*i);
txt = ['itr ',num2str(i)];
plot(x,y,'DisplayName',txt)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,'itr=',i})
end
xlabel('x');ylabel('y');
hold off
legend show
function txt = myupdatefcn(~,event_obj,txt4update,Num)
% Customizes text of data tips
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {[txt4update ,': ',num2str(Num)],...
['x: ',num2str(pos(1))],...
['y: ',num2str(pos(2))]};
end
  댓글 수: 1
Adam
Adam 2019년 3월 6일
편집: Adam 2019년 3월 6일
You are overwriting the UpdateFcn callback function each time round the loop so at the end it will take on the value of the last time round the loop. If you want it to show you a history you will need to store up that history in a variable and pass the whole variable to the UpdateFcn at the end. This can be outside the loop as setting UpdateFcn in a loop is not useful.
I'm not 100% sure what kind of end result you are looking for or what your set of plots looks like at the end.

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

채택된 답변

nirit
nirit 2019년 3월 6일
Found a solution!!
fig=figure;
hold on;
for i=1:4
x=sort(rand(1,100));
y=sin(2*pi*x*i);
txt = ['itr ',num2str(i)];
plot(x,y,'DisplayName',txt)
dcm_obj =datacursormode(fig);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,'itr='})
end
xlabel('x');ylabel('y');
hold off
legend show
function txt = myupdatefcn(~,event_obj,txt4update)
% Customizes text of data tips
pos = get(event_obj,'Position');
txt = {[txt4update ,': ', event_obj.Target.DisplayName],...
['x: ',num2str(pos(1))],...
['y: ',num2str(pos(2))]};
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by