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

Durch Bewegung eines Objektes andere Objekte mitbewegen

 

chris_2091
Forum-Newbie

Forum-Newbie


Beiträge: 4
Anmeldedatum: 28.07.16
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 04.08.2016, 22:07     Titel: Durch Bewegung eines Objektes andere Objekte mitbewegen
  Antworten mit Zitat      
Hallo zusammen,

Ich habe schnell zwei einfache Gui's mit guide erstellt, bestehend aus jeweils einem Pushbutton und einem einfachen Slider.

Ziel ist es durch Bewegen des Button (Drag & Drop) einen Slider mit zubewegen. Der Slider soll ab einem, sagen wir 65% des Max Wertes, das aktuelle Fenster schließen und das zweite erstellte Fenster öffnen.

Der Drag & Drop Teil funktioniert ohne Probleme. Erstmal wollte ich nur die Bewegung des Sliders hinkriegen.
Hier mein Code

Code:
function varargout = test_gui(varargin)
% TEST_GUI MATLAB code for test_gui.fig
%      TEST_GUI, by itself, creates a new TEST_GUI or raises the existing
%      singleton*.
%
%      H = TEST_GUI returns the handle to a new TEST_GUI or the handle to
%      the existing singleton*.
%
%      TEST_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST_GUI.M with the given input arguments.
%
%      TEST_GUI('Property','Value',...) creates a new TEST_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help test_gui

% Last Modified by GUIDE v2.5 04-Aug-2016 21:40:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @test_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before test_gui is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to test_gui (see VARARGIN)

% Choose default command line output for test_gui
handles.output = hObject;
% in deiner Opening fcn
handles.dragging = [];
handles.orPos = [];
% figure callbacks setzen
set(hObject,'WindowButtonUpFcn',@dropObject,'WindowButtonMotionFcn',@moveObject);
% uipanel callback setzen
set(handles.pushbutton1,'ButtonDownFcn',@dragObject);
set(handles.slider1,'ButtonDownFcn',@dragObject);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_gui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
%%CALLBACKS hinzufügen
function dragObject(hObject,eventdata)
handles = guidata(gcf);
handles.dragging = hObject;
handles.orPos = get(gcf,'CurrentPoint');
guidata(gcf,handles);

function dropObject(hObject,eventdata)
handles = guidata(gcf);
        if ~isempty(handles.dragging)
            newPos = get(gcf,'CurrentPoint');
            posDiff = newPos - handles.orPos;
            set(handles.dragging,'Position',get(handles.dragging,'Position') + [posDiff(1:2) 0 0]);
            handles.dragging = [];
            guidata(gcf,handles)
        end

function moveObject(hObject,eventdata)
handles = guidata(gcf);
        if ~isempty(handles.dragging)
            newPos = get(gcf,'CurrentPoint');
            % Zusätzliche Bewegung des Sliders
                if (newPos(1,1)<=get(handles.slider1,'Max')) && (newPos(1,1)>= get(handles.slider1,'Min'))
                    Pos=newPos(1,1);
                    set(hObject,'Value',Pos);
                end      
            posDiff = newPos - handles.orPos;
            handles.orPos = newPos;
            set(handles.dragging,'Position',get(handles.dragging,'Position') + [posDiff(1:2) 0 0]);
            guidata(gcf,handles)
        end
   
%% CALLBACK-Functions
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
maximal=get(hObject,'Max');
if get(hObject,'Value')> 0.65*maximal
    close(test_gui)
    test_gui_2
else
    minimal=get(hObject,'Min');
    set(hObject,'Value',minimal);
end
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


Sicherlich fragen sich jetzt einige mach warum ich , dass nicht direkt mit dem Slider mache, allerdings sieht der Slider für meine spätere Anwendung nicht gut genug aus.
Ich habe bisher nicht heraus gefunden, wie man Bilder in den Slider einlesen kann und die zwei Pfeile am rechten und linken Rand sind auch störend. Wenn jemand dazu ein Einfall hat, würde ich mich auch sehr darüber freuen.

Ich hatte vorgehabt den Slider unsichtbar zumachen, sodass der User nur den Button sieht und diesen bewegt.
Private Nachricht senden Benutzer-Profile anzeigen E-Mail senden


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 - 2024 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.