function Joytest
%JOYTEST Example which shows to usage of JOYMEX.
%   JOYTEST Initializes gamepad device and shows six axes values
%   and first eight button states. In the example certain assumptions
%   are made about how axis might be inverted, see comments in the code.
    
    running = 1;
    
    % Initialize joystick (asume only one exists)
    JoyMEX('init',0);
   
    % Create status feedback figure and attach close function
    figure('CloseRequestFcn',@onClose);
    % Create plots
    subplot(2,1,1) %joystick position plot
    p1=plot(0,0,'x');   %(X,Y) axis position symbol 
    hold on;
    p2=plot(0,0,'+r');  %(RX,RY) axis position
    p3=plot(0,0,'og');  %Z axis (slider on Logitech Attack 3) position
    title(sprintf('Device 0\nAxis'))
    set(gca,'xlim',[-1 1],'ylim',[-1 1]); axis square

    subplot(2,1,2)   %button states histogram (buttons 1-8)
    b1=bar(zeros(1,8));  %create histogram plot
    title('Button States')
    set(gca,'xlim',[0 9],'ylim',[0 1]); axis square
    
    %run until user closes display window
    while(running)
        % Query postion and button state of joystick
        [position, buttons] = JoyMEX(0);
        
        % Update the plots
        % Notice the usage of minus signs to invert certain axis values.
        % Depending on the device you may want to change these 
        %
        %update joystick X,Y axis position
        set(p1,'Xdata',position(1),'Ydata',-position(2));
        %update joystick RX,RY axis position
        set(p2,'Xdata',position(4),'Ydata',-position(5));  
        %update Z axis (slider) position
        set(p3,'Xdata',0,'Ydata',position(3));   
        %update button state display
        set(b1,'Ydata',double(buttons([1:8])) );

        % Force update of plot
        drawnow
    end
    
    % Clear MEX-file to release joysticks
    clear JoyMEX
    
    % When user tries to close the figure, end the while loop and
    % dispose of the figure
    function onClose(src,evt)
       running = 0;  %exit continuous joystick reading loop
       delete(src);  %discard graphics display plot
    end
end