WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

X Koordinaten von cursor auf GUI ausgeben

 

Jonzi
Forum-Anfänger

Forum-Anfänger


Beiträge: 16
Anmeldedatum: 28.09.10
Wohnort: ---
Version: R2009b
     Beitrag Verfasst am: 03.11.2010, 17:01     Titel: X Koordinaten von cursor auf GUI ausgeben
  Antworten mit Zitat      
Hallo @all,

ich habe folgendes Problem. Ich habe einen Code erhalten mit dem ich die Position meiner 2 Cursor auf meinem Plot auslesen kann.
Aber dies funktioniert nur wenn ich den Plot generiere und dabei bekomme ich nur die Startposition meiner cursor, die ich sowiso festgelegt habe!

Ich würde nun gerne mit einem static text feld oder was ähnlichem, die aktuellen cursor x koordinaten ausgeben und das in meiner gui.
Zu meinem Programm, durch einen toggle button wird das plot in einer axes generiert, beim erneuten drücken der toogle taste verschwindet wieder der plot.

Hier ist mal der code:

1: Für das generieren der Cursor

Code:
function NumberOfCursor=CreateCursor(fhandle, NumberOfCursor)
% scCreateCursor creates or replaces a vertical cursor
%
% Examples
% n=CreateCursor()
% n=CreateCursor(fighandle)
% n=CreateCursor(fighandle, CursorNumber)
%
% Cursors are specific to a figure. If the figure handle is not specified
% in fighandle, the current figure (returned by gcf) will be used.
% CursorNumber is any positive number. If not specified the lowest numbered
% free cursor number will be used.
%
% Returns n, the number of the cursor created in the relevant figure.
%
% A record is kept of the cursors in the figure's application data
% area. Cursor n occupies the nth element of a cell array. Each element is
% a structure containing the following fields:
%           Handles:    a list of objects associated with this cursor -
%                       one line for each axes and one or more text objects
%           IsActive:   true if this cursor is currently being moved. False
%                       otherwise. For manual cursors, IsActive is set by a
%                       button down on the cursor and cleared by a button
%                       up.
% Functions that affect the position of a cursor must explicitly update all
% the objects listed in Handles. A cursor is not an object itself.
%
% Revisions: 27.2.07 add restoration of gca
% Note: These functions presently work with 2D views only

% Keep gca to restore current axes at end
current=gca;

% Figure details
if nargin<1
    fhandle=gcf;
end
AxesList=sort(findobj(fhandle, 'type', 'axes'));


% Get cursor info
newhandles=zeros(1, length(AxesList));
Cursors=getappdata(fhandle, 'VerticalCursors');

% Deal with this cursor number

if isempty(Cursors)
    NumberOfCursor=1;
else
    if nargin<2
        FirstEmpty=0;
        if ~isempty(Cursors)
            for i=1:length(Cursors)
                if isempty(Cursors{i})
                    FirstEmpty=i;
                    break
                end
            end
        end
        if FirstEmpty==0
            NumberOfCursor=length(Cursors)+1;
            Cursors{NumberOfCursor}=[];
        else
            NumberOfCursor=FirstEmpty;
        end
    end
end


if ~isempty(Cursors) && ~isempty(Cursors{NumberOfCursor}) &&...
        isfield(Cursors{NumberOfCursor},'CursorIsActive');
    if Cursors{NumberOfCursor}.CursorIsActive==true
        disp('CreateCursor: Ignored attempt to delete the currently active cursor');
        return
    else
        delete(Cursors{NumberOfCursor}.Handles);
    end
end
Cursors{NumberOfCursor}.IsActive=false;
Cursors{NumberOfCursor}.Handles=[];
Cursors{NumberOfCursor}.Type='';

if NumberOfCursor == 1
XLim=get(AxesList(end),'XLim');
xhalf=XLim(1)+(0.3*(XLim(2)-XLim(1)));

else
    XLim=get(AxesList(end),'XLim');
    xhalf=XLim(1)+(0.8*(XLim(2)-XLim(1)));
end
for i=1:length(AxesList)
    % For each cursor there is a line object in each axes
    subplot(AxesList(i));
        YLim=get(AxesList(i),'YLim');
        newhandles(i)=line([xhalf xhalf], [YLim(1) YLim(2)]);
        Cursors{NumberOfCursor}.Type='2D';
end


% Put a label at the top and make it behave as part of the cursor
axes(AxesList(1));
YLim=get(AxesList(1),'YLim');
th=text(xhalf, YLim(2), ['C' num2str(NumberOfCursor)],...
    'Tag','Cursor',...
    'UserData', NumberOfCursor,...
    'FontSize', 7,...
    'HorizontalAlignment','center',...
    'VerticalAlignment','bottom',...
    'Color', 'k',...
    'EdgeColor',[26/255 133/255 5/255],...
    'Clipping','off',...
    'ButtonDownFcn',{@CursorButtonDownFcn});

% Set line properties en bloc
% Note UserData has the cursor number
if ispc==1
    % Windows
    EraseMode='xor';
else
    % Mac etc: 'xor' may cause problems
    EraseMode='normal';
end
set(newhandles, 'Tag', 'Cursor',...
    'Color', [26/255 133/255 5/255],...
    'UserData', NumberOfCursor,...
    'Linewidth',1,...
    'Erasemode', EraseMode,...
    'ButtonDownFcn',{@CursorButtonDownFcn});

Cursors{NumberOfCursor}.IsActive=false;
Cursors{NumberOfCursor}.Handles=[newhandles th];

setappdata(fhandle, 'VerticalCursors', Cursors);
set(gcf, 'WindowButtonMotionFcn',{@CursorWindowButtonMotionFcn});
% Restore the axes on entry as the current axes
axes(current)
return
end

%--------------------------------------------------------------------------
function CursorButtonDownFcn(hObject, EventData) %#ok<INUSD>
%--------------------------------------------------------------------------

% Make sure we have a left button click
type=get(ancestor(hObject,'figure'), 'SelectionType');
flag=strcmp(type, 'normal');
if flag==0
    % Not so, return and let matlab call the required callback,
    % e.g. the uicontextmenu, which will be in the queue
    return
end;

% Flag the active cursor
Cursors=getappdata(gcf, 'VerticalCursors');
for i=1:length(Cursors)
    if isempty(Cursors{i})
        continue
    end
    if any(Cursors{i}.Handles==hObject)
            % Set flag
            Cursors{i}.IsActive=true;
            break
    end
end
setappdata(gcf, 'VerticalCursors', Cursors);

% Set up
StoreWindowButtonDownFcn=get(gcf,'WindowButtonDownFcn');
StoreWindowButtonUpFcn=get(gcf,'WindowButtonUpFcn');
StoreWindowButtonMotionFcn=get(gcf,'WindowButtonMotionFcn');
% Store these values in the CursorButtonUpFcn persistent variables so they
% can be used/restored later
CursorButtonUpFcn({hObject,...
    StoreWindowButtonDownFcn,...
    StoreWindowButtonUpFcn,...
    StoreWindowButtonMotionFcn});
% Motion callback needs only the current cursor number
CursorButtonMotionFcn({hObject});

% Set up callbacks
set(gcf, 'WindowButtonUpFcn',{@CursorButtonUpFcn});
set(gcf, 'WindowButtonMotionFcn',{@CursorButtonMotionFcn});
return
end

%--------------------------------------------------------------------------
function CursorButtonUpFcn(hObject, EventData) %#ok<INUSD>
%--------------------------------------------------------------------------
% These persistent values are set by a call from CursorButtonDownFcn
persistent ActiveHandle;
persistent StoreWindowButtonDownFcn;
persistent StoreWindowButtonUpFcn;
persistent StoreWindowButtonMotionFcn;

% Called from CursorButtonDownFcn - hObject is a cell with values to seed
% the persistent variables
if iscell(hObject)
    ActiveHandle=hObject{1};
    StoreWindowButtonDownFcn=hObject{2};
    StoreWindowButtonUpFcn=hObject{3};
    StoreWindowButtonMotionFcn=hObject{4};
    return
end

% Called by button up in a figure window - use the stored CurrentCursor
% value
UpdateCursorPosition(ActiveHandle)

% Restore the figure's original callbacks - make sure we do this in the
% same figure that we had when the mouse button-down was detected
h=ancestor(ActiveHandle,'figure');
set(h, 'WindowButtonDownFcn', StoreWindowButtonDownFcn);
set(h, 'WindowButtonUpFcn', StoreWindowButtonUpFcn);
set(h, 'WindowButtonMotionFcn',StoreWindowButtonMotionFcn);


% Remove the active cursor flag
Cursors=getappdata(gcf, 'VerticalCursors');
for i=1:length(Cursors)
    if isempty(Cursors{i})
        continue
    end
    if any(Cursors{i}.Handles==ActiveHandle)
        Cursors{i}.IsActive=false;
        break
    end
end
setappdata(gcf, 'VerticalCursors', Cursors);

return
end

%--------------------------------------------------------------------------
function CursorButtonMotionFcn(hObject, EventData) %#ok<INUSD>
%--------------------------------------------------------------------------
% This replaces the CursorWindowButtonMotionFcn while a cursor is being
% moved
persistent ActiveHandle;

% Called from CursorButtonDownFcn
if iscell(hObject)
    ActiveHandle=hObject{1};
    return
end
%Called by button up
UpdateCursorPosition(ActiveHandle)
return
end

%--------------------------------------------------------------------------
function UpdateCursorPosition(ActiveHandle)
%--------------------------------------------------------------------------
% Get all lines in all axes in the current figure that are
% associated with the current cursor....
CursorHandles=findobj(gcf, 'Type', 'line',...
    'Tag', 'Cursor',...
    'UserData', get(ActiveHandle,'UserData'));
% ... and update them:
% Get the pointer position in the current axes
cpos=get(gca,'CurrentPoint');
if cpos(1,1)==cpos(2,1) && cpos(1,2)==cpos(2,2)
    % 2D Cursor
    % Limit to the x-axis limits
    XLim=get(gca,'XLim');
    if cpos(1)<XLim(1)
        cpos(1)=XLim(1);
    end
    if cpos(1)>XLim(2)
        cpos(1)=XLim(2);
    end
    % Set
    set(CursorHandles,'XData',[cpos(1) cpos(1)]);
else
    % TODO: Include support for 3D cursors
    return
end

% Now update the cursor Label
LabelHandle=findobj(gcf, 'Type', 'text', 'Tag', 'Cursor', 'UserData',...
    get(ActiveHandle,'UserData'));
tpos=get(LabelHandle,'position');
tpos(1)=cpos(1);
set(LabelHandle,'position',tpos);
return
end

 


2: Den Cursor in plot setzen

Code:
function SetCursorLocation(varargin)
% SetCursorPos returns the position of a cursor in x-axis units
%
% Example:
% x=SetCursorLocation(CursorNumber, Position)
% x=SetCursorLocation(fhandle, CursorNumber, Position)
%
% fhandle defaults to the current figure if not supplied or empty
%
% Sets the x-axis position for the cursor in the figure
%

switch nargin
    case 2
        fhandle=gcf;
        CursorNumber=varargin{1};
        Pos=varargin{2};
    case 3
        fhandle=gcf;
        CursorNumber=varargin{2};
        Pos=varargin{3};
    otherwise
        return
end

Cursors=getappdata(fhandle,'VerticalCursors');
if isempty(Cursors)
    % No cursors in figure
    x=[];
    return
end

try
    %Use try/catch as the selected cursor may not exist
    idx=strcmpi(get(Cursors{CursorNumber}.Handles,'type'),'line');
    set(Cursors{CursorNumber}.Handles(idx), 'XData', [Pos Pos]);
    idx=strcmpi(get(Cursors{CursorNumber}.Handles,'type'),'text');
    for i=1:length(idx)
        if idx(i)==1
            p=get(Cursors{CursorNumber}.Handles(i),'Position');
            p(1)=Pos;
            set(Cursors{CursorNumber}.Handles(i), 'Position', p);
        end
    end
catch
    return
end


return
end
 


3. X koordinaten bestimmen

Code:

function x=GetCursorLocation(varargin)
% GetCursorLocation returns the position of a cursor in x-axis units
%
% Example:
% x=GetCursorLocation(CursorNumber)
% x=GetCursorLocation(fhandle, CursorNumber)
%
% fhandle defaults to the current figure if not supplied or empty
%
% Returns x,  the x-axis position for the cursor in the figure
%
switch nargin
    case 1
        fhandle=gca;
        CursorNumber=varargin{1};
    case 2
        fhandle=varargin{1};
        CursorNumber=varargin{2};
end

Cursors=getappdata(fhandle,'VerticalCursors');
if isempty(Cursors)
    % No cursors in figure
    x=[];
    return
end

try
    %Use try/catch as the selected cursor may not exist
    pos=get(Cursors{CursorNumber}.Handles(1),'XData');
    x=pos(1);
catch
    x=[];
end

return
end
 


Falls Ihr noch fragen habt, dann meldet euch bitte!wäre super wenn ihr mir auch schon gleich tipps hättet!!!

Gruß

Jonzi
Private Nachricht senden Benutzer-Profile anzeigen


Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.