Just like displaying coordinates, I want to display row and column number
    3 views (last 30 days)
  
       Show older comments
    
Hi, so I am gonna plot a bunch of different graphs from a 3D matrix and I want to be able to click on the line in the graph and be able to know from which row and column the line comes from. So I tried the following tactic from enable data cursor mode:
function txt = displayCoordinates(~,info)
    x = info.Position(1);
    y = info.Position(2);
    txt = ['(' num2str(x) ', ' num2str(y) ')'];
end
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
    for j=1:4
        plot(x,squeeze(y(i,j,:)))
        hold on
        dcm = datacursormode;
        dcm.Enable = 'on';
        %dcm.DisplayStyle = 'window';
        dcm.UpdateFcn = @displayCoordinates;
    end
end
So the clicking on the line works perfectly fine. The only problem is that I can't seem to implement how to display the row and column instead of the coordinates. So I tried this, but it doesn't work.
function txt = displayCoordinates(~,info)
    txt = ['row is ' num2str(i) ', column is' num2str(j)];
end
Does anyone know how I can make this work?
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 26 Jun 2020
        x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
    for j=1:4
        plot(x,squeeze(y(i,j,:)), 'UserData', [i j])
        hold on
    end
end
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
function txt = displayCoordinates(hObject,info)
    ud = hObject.UserData;
    if numel(ud) ~= 2
      x = info.Position(1);
      y = info.Position(2);
      txt = sprintf('(x,y) = (%g, %g)', x, y);
    else
      txt = sprintf('row is %d, column is %d', ud);
    end
end
3 Comments
  Walter Roberson
      
      
 on 26 Jun 2020
				x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
    for j=1:4
        plot(x,squeeze(y(i,j,:)), 'UserData', [i j])
        hold on
    end
end
dcm = datacursormode;
dcm.Enable = 'on';
%dcm.DisplayStyle = 'window';
dcm.UpdateFcn = @displayCoordinates;
function txt = displayCoordinates(~,info)
    ud = info.Target.UserData;
    if numel(ud) ~= 2
      x = info.Position(1);
      y = info.Position(2);
      txt = sprintf('(x,y) = (%g, %g)', x, y);
    else
      txt = sprintf('row is %d, column is %d', ud);
    end
end
More Answers (1)
  Takumi
      
 on 26 Jun 2020
        
      Edited: Takumi
      
 on 26 Jun 2020
  
      "info" has two fields and one of fields that named "Target" has line data.
So I atempted  to use the find function to find an index that matches the data in the "Target" and the "Position".
x=1:2;
y(:,:,1)=[1 1 2 3; 1.5 2 2.5 3.5];
y(:,:,2)=[2 3 4 6; 2.5 3 4.5 6.5];
figure(1)
for i=1:2
    for j=1:4
        plot(x,squeeze(y(i,j,:)))
        hold on
        dcm = datacursormode;
        dcm.Enable = 'on';
        %dcm.DisplayStyle = 'window';
        dcm.UpdateFcn = @displayCoordinates;
    end
end
function txt = displayCoordinates(~,info)
    x = info.Position(1);
    y = info.Position(2);
    i = find(info.Target.XData==x);
    j = find(info.Target.YData==y);
    txt = ['row is ' num2str(i) ', column is' num2str(j)];
end
But this method is not robust...
See Also
Categories
				Find more on Spline Postprocessing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

