How to get Matlab datatip to output subplot information?

5 Ansichten (letzte 30 Tage)
Avinash Nayak
Avinash Nayak am 9 Apr. 2024
Beantwortet: Voss am 9 Apr. 2024
I have a Matlab script which plots data in two subplots and allows me to click interactively on any data point using datatip and outputs the x,y info of that point. Running the code below, clicking on a data point using datatip and then pressing Enter will output the x,y value of that point in the Command Window. I can do this for any number of points till the loop ends or I manually close the figure window. Figure 1 and Figure 2 show me clicking on one point each from left and right subplot, respectively, and the information can be seen below at the bottom of the figures. Is there a way in which I can make Matlab also output which subplot I am clicking on ? For example, it should output x,y, and 1 or 2 in which 1 or 2 are subplot indices.
Figure 1
Figure 2
clc
clearvars
close all
figure('units','normalized','Position',[0.15 0.45 0.7 0.55]);
for j1=1:2
sbp1(j1)=subplot(1,2,j1);
plot(1:1000,rand(1,1000),'k','linewidth',2);
set(gca,'fontsize',24);
end
linkaxes(sbp1,'x');
datacursormode on;
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myupdatefcn);
for rc11=1:10000
try
pause;
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
mkc1=info_struct.Position;
fprintf('%.3f %.3f \n',mkc1(1),mkc1(2));
end
catch
break;
end
end
function output_txt = myupdatefcn(~,event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
pos = get(event_obj, 'Position');
output_txt = {['x: ' num2str(pos(1))], ['y: ' num2str(pos(2))]};
end

Antworten (1)

Voss
Voss am 9 Apr. 2024
clc
clearvars
close all
figure('units','normalized','Position',[0.15 0.45 0.7 0.55]);
for j1=1:2
sbp1(j1)=subplot(1,2,j1);
plot(1:1000,rand(1,1000),'k','linewidth',2);
set(gca,'fontsize',24);
end
linkaxes(sbp1,'x');
datacursormode on;
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,sbp1});
for rc11=1:10000
try
pause;
info_struct = getCursorInfo(dcm_obj);
if isfield(info_struct, 'Position')
mkc1=info_struct.Position;
sbp_idx = find(sbp1 == gca(),1);
fprintf('%.3f %.3f %d\n',mkc1(1),mkc1(2),sbp_idx);
end
catch
break;
end
end
function output_txt = myupdatefcn(~,event_obj,sbp1)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text
pos = get(event_obj, 'Position');
sbp_idx = find(sbp1 == gca(),1);
output_txt = {['x: ' num2str(pos(1))], ['y: ' num2str(pos(2))], sprintf('subplot: %d',sbp_idx)};
end

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by