Display value of Y axis inside GUI plot

392 views Asked by At

I have program code in matlab with GUI. I have there some axes object called "axes1" - there my plot is displayed (simple plot of x, y values).
I want that after point on line there shows value of Y axis in that point. And best if it would display dynamically - after moving curosor the values would automatically show new pointed value.
I saw some tutorials about this, but I couldn't apply their tips to program with GUI.

1

There are 1 answers

0
Benoit_11 On BEST ANSWER

Here is a very simple example of a way you could use datacursormode, an interactive cursor which enables you to select points on your figure and get their coordinates. You could easily customize the example and store the coordinates in variables and so on, but I'll let this part up to you :).

Here is the code of the GUI with comments. You can copy/paste into a new .m file to test and play around. The GUI is just an axes in which I display random data and a checkbox that you can use to toggle the activation (i.e. on or off) of the datacursormode.

function DispYData

clear
clc

global dcm_obj

%// Create figure and ui controls
hFig = figure('Position',[200 200 500 500]);

handles.Axes = axes('Position',[.2 .2 .7 .7]);

plot(1:20,rand(1,20));

%// Create datacursor. Disable for the moment.
dcm_obj = datacursormode(hFig);
set(dcm_obj,'Enable','off')

handles.checkbox = uicontrol('Style','check','Position',[20 50 120 20],'String','Activate datacursor','Callback',@(s,e) GetPos);

guidata(hFig,handles)
    function GetPos

        %// If checked, activate datacursor mode
        if get(handles.checkbox,'Value')
            set(dcm_obj,'Enable','on')            
        %// If uncheck, disable.
        else
            set(dcm_obj,'Enable','off')
        end
        guidata(hFig,handles)
    end
end

Sample screenshot of the GUI after enabling the datacursormode and selecting a point on the curve:

enter image description here

Have fun!